2020-01-16 04:06:25 +08:00
|
|
|
package record
|
|
|
|
|
2020-01-18 05:43:55 +08:00
|
|
|
import "reflect"
|
2020-01-16 04:06:25 +08:00
|
|
|
|
2020-01-18 05:43:55 +08:00
|
|
|
var payloadTypeRegistry = make(map[string]reflect.Type)
|
2020-01-16 04:06:25 +08:00
|
|
|
|
2020-01-17 23:37:35 +08:00
|
|
|
// Record represents a data type that can be used as the payload of an Envelope.
|
|
|
|
// The Record interface defines the methods used to marshal and unmarshal a Record
|
|
|
|
// type to a byte slice.
|
|
|
|
//
|
|
|
|
// Record types may be "registered" as the default for a given Envelope.PayloadType
|
|
|
|
// using the RegisterPayloadType function. Once a Record type has been registered,
|
|
|
|
// an instance of that type will be created and used to unmarshal the payload of
|
|
|
|
// any Envelope with the registered PayloadType when the Envelope is opened using
|
|
|
|
// the ConsumeEnvelope function.
|
|
|
|
//
|
|
|
|
// To use an unregistered Record type instead, use ConsumeTypedEnvelope and pass in
|
|
|
|
// an instance of the Record type that you'd like the Envelope's payload to be
|
|
|
|
// unmarshaled into.
|
2020-01-16 04:06:25 +08:00
|
|
|
type Record interface {
|
|
|
|
MarshalRecord() ([]byte, error)
|
|
|
|
|
|
|
|
UnmarshalRecord([]byte) error
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:43:55 +08:00
|
|
|
// DefaultRecord contains the payload of an Envelope whose PayloadType field
|
|
|
|
// does not match any registered Record type. The Contents field contains
|
|
|
|
// the unprocessed Envelope payload.
|
|
|
|
type DefaultRecord struct {
|
|
|
|
Contents []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultRecord) MarshalRecord() ([]byte, error) {
|
|
|
|
return r.Contents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultRecord) UnmarshalRecord(data []byte) error {
|
2020-01-18 05:50:45 +08:00
|
|
|
r.Contents = data
|
2020-01-18 05:43:55 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:37:35 +08:00
|
|
|
// RegisterPayloadType associates a binary payload type identifier with a concrete
|
|
|
|
// Record type. This is used to automatically unmarshal Record payloads from Envelopes
|
|
|
|
// when using ConsumeEnvelope, and to automatically marshal Records and determine the
|
|
|
|
// correct PayloadType when calling MakeEnvelopeWithRecord.
|
|
|
|
//
|
2020-01-18 05:43:55 +08:00
|
|
|
// To register a Record type, provide the payload type identifier and an
|
|
|
|
// empty instance of the Record type.
|
|
|
|
//
|
|
|
|
// Registration should be done in the init function of the package where the
|
|
|
|
// Record type is defined:
|
2020-01-17 23:37:35 +08:00
|
|
|
//
|
|
|
|
// package hello_record
|
|
|
|
//
|
|
|
|
// var HelloRecordPayloadType = []byte("/libp2p/hello-record")
|
|
|
|
//
|
|
|
|
// func init() {
|
|
|
|
// RegisterPayloadType(HelloRecordPayloadType, &HelloRecord{})
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// type HelloRecord struct { } // etc..
|
|
|
|
//
|
2020-01-16 04:06:25 +08:00
|
|
|
func RegisterPayloadType(payloadType []byte, prototype Record) {
|
2020-01-17 23:48:37 +08:00
|
|
|
payloadTypeRegistry[string(payloadType)] = getValueType(prototype)
|
2020-01-16 04:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalRecordPayload(payloadType []byte, payloadBytes []byte) (Record, error) {
|
2020-01-18 05:50:45 +08:00
|
|
|
rec := blankRecordForPayloadType(payloadType)
|
|
|
|
err := rec.UnmarshalRecord(payloadBytes)
|
2020-01-16 04:06:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:50:45 +08:00
|
|
|
func blankRecordForPayloadType(payloadType []byte) Record {
|
2020-01-17 23:48:37 +08:00
|
|
|
valueType, ok := payloadTypeRegistry[string(payloadType)]
|
2020-01-16 04:06:25 +08:00
|
|
|
if !ok {
|
2020-01-18 05:50:45 +08:00
|
|
|
return &DefaultRecord{}
|
2020-01-16 04:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
val := reflect.New(valueType)
|
|
|
|
asRecord := val.Interface().(Record)
|
2020-01-18 05:50:45 +08:00
|
|
|
return asRecord
|
2020-01-16 04:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func payloadTypeForRecord(rec Record) ([]byte, bool) {
|
|
|
|
valueType := getValueType(rec)
|
|
|
|
|
2020-01-17 23:48:37 +08:00
|
|
|
for k, t := range payloadTypeRegistry {
|
2020-01-16 04:06:25 +08:00
|
|
|
if t.AssignableTo(valueType) {
|
|
|
|
return []byte(k), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return []byte{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getValueType(i interface{}) reflect.Type {
|
|
|
|
valueType := reflect.TypeOf(i)
|
|
|
|
if valueType.Kind() == reflect.Ptr {
|
|
|
|
valueType = valueType.Elem()
|
|
|
|
}
|
|
|
|
return valueType
|
|
|
|
}
|