mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-02-05 05:50:08 +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 {
|
func (r *DefaultRecord) UnmarshalRecord(data []byte) error {
|
||||||
r.Contents = make([]byte, len(data))
|
r.Contents = data
|
||||||
copy(r.Contents, data)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,26 +65,23 @@ func RegisterPayloadType(payloadType []byte, prototype Record) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalRecordPayload(payloadType []byte, payloadBytes []byte) (Record, error) {
|
func unmarshalRecordPayload(payloadType []byte, payloadBytes []byte) (Record, error) {
|
||||||
rec, err := blankRecordForPayloadType(payloadType)
|
rec := blankRecordForPayloadType(payloadType)
|
||||||
if err != nil {
|
err := rec.UnmarshalRecord(payloadBytes)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = rec.UnmarshalRecord(payloadBytes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return rec, nil
|
return rec, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func blankRecordForPayloadType(payloadType []byte) (Record, error) {
|
func blankRecordForPayloadType(payloadType []byte) Record {
|
||||||
valueType, ok := payloadTypeRegistry[string(payloadType)]
|
valueType, ok := payloadTypeRegistry[string(payloadType)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return &DefaultRecord{}, nil
|
return &DefaultRecord{}
|
||||||
}
|
}
|
||||||
|
|
||||||
val := reflect.New(valueType)
|
val := reflect.New(valueType)
|
||||||
asRecord := val.Interface().(Record)
|
asRecord := val.Interface().(Record)
|
||||||
return asRecord, nil
|
return asRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
func payloadTypeForRecord(rec Record) ([]byte, bool) {
|
func payloadTypeForRecord(rec Record) ([]byte, bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user