-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bindnode): add a BindnodeRegistry utility
For centralising bindnode setup - register a type & schema once and not have to worry about the TypedPrototype or direct bindnode calls after that. It also caches bindnode Options that may go along with the type. This could have been a global registry, but there exists a possibility of the same type wanting to be used by different users in the same application instance but with different schemas and/or Options. So instead, the question of it being a global is left to the user.
- Loading branch information
Showing
1 changed file
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package registry | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"reflect" | ||
|
||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/codec" | ||
"github.com/ipld/go-ipld-prime/datamodel" | ||
"github.com/ipld/go-ipld-prime/node/bindnode" | ||
"github.com/ipld/go-ipld-prime/schema" | ||
) | ||
|
||
type prototypeData struct { | ||
proto schema.TypedPrototype | ||
options []bindnode.Option | ||
} | ||
|
||
// BindnodeRegistry holds TypedPrototype and bindnode options for Go types and | ||
// will use that data for conversion operations. | ||
type BindnodeRegistry map[reflect.Type]prototypeData | ||
|
||
// NewRegistry creates a new BindnodeRegistry | ||
func NewRegistry() BindnodeRegistry { | ||
return make(BindnodeRegistry) | ||
} | ||
|
||
func typeOf(ptrValue interface{}) reflect.Type { | ||
val := reflect.ValueOf(ptrValue).Type() | ||
for val.Kind() == reflect.Ptr { | ||
val = val.Elem() | ||
} | ||
return val | ||
} | ||
|
||
// lookup of cached TypedPrototype (and therefore Type) for a Go type, if not | ||
// found, initial parse and setup and caching of the TypedPrototype will happen | ||
func (br BindnodeRegistry) prototypeDataFor(ptrType interface{}) prototypeData { | ||
typ := typeOf(ptrType) | ||
proto, ok := br[typ] | ||
if !ok { | ||
panic(fmt.Sprintf("bindnode utils: type has not been registered: %s", typ.Name())) | ||
} | ||
return proto | ||
} | ||
|
||
// RegisterType registers ptrType with schema such that it can be wrapped and | ||
// unwrapped without needing the schema, Type, or TypedPrototype. | ||
// Typically the typeName will match the Go type name, but it can be whatever | ||
// is defined in the schema for the type being registered. | ||
// Registering the same type twice on this registry will cause a panic. Use | ||
// IsRegistered() if this is a possibility that should be avoided. | ||
// This call may also panic if the schema is invalid or the type doesn't match | ||
// the schema. | ||
func (br BindnodeRegistry) RegisterType(ptrType interface{}, schema string, typeName string, options ...bindnode.Option) { | ||
typ := typeOf(ptrType) | ||
if _, ok := br[typ]; ok { | ||
panic(fmt.Sprintf("bindnode utils: type already registered: %s", typ.Name())) | ||
} | ||
typeSystem, err := ipld.LoadSchemaBytes([]byte(schema)) | ||
if err != nil { | ||
panic(fmt.Sprintf("bindnode utils: failed to load schema: %s", err.Error())) | ||
} | ||
schemaType := typeSystem.TypeByName(typeName) | ||
if schemaType == nil { | ||
panic(fmt.Sprintf("bindnode utils: schema for [%T] does not contain that named type [%s]", ptrType, typ.Name())) | ||
} | ||
br[typ] = prototypeData{ | ||
bindnode.Prototype(ptrType, schemaType, options...), | ||
options, | ||
} | ||
} | ||
|
||
// IsRegistered can be used to determine if the type has already been registered | ||
// within this registry. | ||
// Using RegisterType on an already registered type will cause a panic, so where | ||
// this may be the case, IsRegistered can be used to check. | ||
func (br BindnodeRegistry) IsRegistered(ptrType interface{}) bool { | ||
_, ok := br[typeOf(ptrType)] | ||
return ok | ||
} | ||
|
||
// TypeFromReader deserializes bytes using the given codec from a Reader and | ||
// instantiates the Go type that's provided as a pointer via the ptrValue | ||
// argument. | ||
func (br BindnodeRegistry) TypeFromReader(r io.Reader, ptrValue interface{}, decoder codec.Decoder) (interface{}, error) { | ||
protoData := br.prototypeDataFor(ptrValue) | ||
node, err := ipld.DecodeStreamingUsingPrototype(r, decoder, protoData.proto) | ||
if err != nil { | ||
return nil, err | ||
} | ||
typ := bindnode.Unwrap(node) | ||
return typ, nil | ||
} | ||
|
||
// TypeFromBytes deserializes bytes using the given codec from its bytes and | ||
// instantiates the Go type that's provided as a pointer via the ptrValue | ||
// argument. | ||
func (br BindnodeRegistry) TypeFromBytes(byts []byte, ptrValue interface{}, decoder codec.Decoder) (interface{}, error) { | ||
protoData := br.prototypeDataFor(ptrValue) | ||
node, err := ipld.DecodeUsingPrototype(byts, decoder, protoData.proto) | ||
if err != nil { | ||
return nil, err | ||
} | ||
typ := bindnode.Unwrap(node) | ||
return typ, nil | ||
} | ||
|
||
// TypeFromNode converts an datamodel.Node into an appropriate Go type that's | ||
// provided as a pointer via the ptrValue argument. | ||
func (br BindnodeRegistry) TypeFromNode(node datamodel.Node, ptrValue interface{}) (interface{}, error) { | ||
protoData := br.prototypeDataFor(ptrValue) | ||
if tn, ok := node.(schema.TypedNode); ok { | ||
node = tn.Representation() | ||
} | ||
builder := protoData.proto.Representation().NewBuilder() | ||
err := builder.AssignNode(node) | ||
if err != nil { | ||
return nil, err | ||
} | ||
typ := bindnode.Unwrap(builder.Build()) | ||
return typ, nil | ||
} | ||
|
||
// TypeToNode converts a Go type that's provided as a pointer via the ptrValue | ||
// argument to an schema.TypedNode. | ||
func (br BindnodeRegistry) TypeToNode(ptrValue interface{}) schema.TypedNode { | ||
protoData := br.prototypeDataFor(ptrValue) | ||
return bindnode.Wrap(ptrValue, protoData.proto.Type(), protoData.options...) | ||
} | ||
|
||
// TypeToWriter is a utility method that serializes a Go type that's provided as | ||
// a pointer via the ptrValue argument through the given codec to a Writer. | ||
func (br BindnodeRegistry) TypeToWriter(ptrValue interface{}, w io.Writer, encoder codec.Encoder) error { | ||
return ipld.EncodeStreaming(w, br.TypeToNode(ptrValue), encoder) | ||
} | ||
|
||
// TypeToBytes is a utility method that serializes a Go type that's provided as | ||
// a pointer via the ptrValue argument through the given codec and returns the | ||
// bytes. | ||
func (br BindnodeRegistry) TypeToBytes(ptrValue interface{}, encoder codec.Encoder) ([]byte, error) { | ||
return ipld.Encode(br.TypeToNode(ptrValue), encoder) | ||
} |