Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
cleanup DefaultRecord code a bit
Browse files Browse the repository at this point in the history
- removes unused error return from blankRecordForPayloadType
- just references instead of copying in DefaultRecord.UnmarshalRecord
  I figure this is likely safe, since we'll be unmarshalling from the
  payload of an Envelope, which shouldn't get altered after it's
  created.
  • Loading branch information
yusefnapora committed Jan 17, 2020
1 parent a26c845 commit ae3bc7b
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func (r *DefaultRecord) MarshalRecord() ([]byte, error) {
}

func (r *DefaultRecord) UnmarshalRecord(data []byte) error {
r.Contents = make([]byte, len(data))
copy(r.Contents, data)
r.Contents = data
return nil
}

Expand Down Expand Up @@ -66,26 +65,23 @@ func RegisterPayloadType(payloadType []byte, prototype Record) {
}

func unmarshalRecordPayload(payloadType []byte, payloadBytes []byte) (Record, error) {
rec, err := blankRecordForPayloadType(payloadType)
if err != nil {
return nil, err
}
err = rec.UnmarshalRecord(payloadBytes)
rec := blankRecordForPayloadType(payloadType)
err := rec.UnmarshalRecord(payloadBytes)
if err != nil {
return nil, err
}
return rec, nil
}

func blankRecordForPayloadType(payloadType []byte) (Record, error) {
func blankRecordForPayloadType(payloadType []byte) Record {
valueType, ok := payloadTypeRegistry[string(payloadType)]
if !ok {
return &DefaultRecord{}, nil
return &DefaultRecord{}
}

val := reflect.New(valueType)
asRecord := val.Interface().(Record)
return asRecord, nil
return asRecord
}

func payloadTypeForRecord(rec Record) ([]byte, bool) {
Expand Down

0 comments on commit ae3bc7b

Please sign in to comment.