mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-02-04 05:40:10 +08:00
cleanup DefaultRecord code a bit
- 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.
This commit is contained in:
parent
a26c845a76
commit
ae3bc7bdfb
@ -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
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user