From ccb9eea4f7e666d1292a2526fa43225a24342305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 9 Jun 2021 22:36:29 +0100 Subject: [PATCH] schema: add TypedPrototype Moving from bindnode, and adding a Type method for consistency with TypedNode. Fixes #181. --- node/bindnode/api.go | 6 +++--- node/bindnode/node.go | 14 +++++--------- schema/typedNode.go | 13 +++++++++++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/node/bindnode/api.go b/node/bindnode/api.go index 239c457..c967074 100644 --- a/node/bindnode/api.go +++ b/node/bindnode/api.go @@ -8,8 +8,8 @@ import ( "github.com/ipld/go-ipld-prime/schema" ) -// Prototype implements a TypedPrototype given a Go pointer type and an IPLD -// schema type. Note that the result is also an ipld.NodePrototype. +// Prototype implements a schema.TypedPrototype given a Go pointer type and an +// IPLD schema type. Note that the result is also an ipld.NodePrototype. // // If both the Go type and schema type are supplied, it is assumed that they are // compatible with one another. @@ -23,7 +23,7 @@ import ( // from it, so its underlying value will typically be nil. For example: // // proto := bindnode.Prototype((*goType)(nil), schemaType) -func Prototype(ptrType interface{}, schemaType schema.Type) TypedPrototype { +func Prototype(ptrType interface{}, schemaType schema.Type) schema.TypedPrototype { if ptrType == nil && schemaType == nil { panic("either ptrType or schemaType must not be nil") } diff --git a/node/bindnode/node.go b/node/bindnode/node.go index 3099d8f..26f93a8 100644 --- a/node/bindnode/node.go +++ b/node/bindnode/node.go @@ -12,9 +12,9 @@ import ( // Assert that we implement all the interfaces as expected. // Grouped by the interfaces to implement, roughly. var ( - _ ipld.NodePrototype = (*_prototype)(nil) - _ TypedPrototype = (*_prototype)(nil) - _ ipld.NodePrototype = (*_prototypeRepr)(nil) + _ ipld.NodePrototype = (*_prototype)(nil) + _ schema.TypedPrototype = (*_prototype)(nil) + _ ipld.NodePrototype = (*_prototypeRepr)(nil) _ ipld.Node = (*_node)(nil) _ schema.TypedNode = (*_node)(nil) @@ -52,12 +52,8 @@ func (w *_prototype) NewBuilder() ipld.NodeBuilder { }} } -// TODO: consider these Typed interfaces for the schema package - -type TypedPrototype interface { - ipld.NodePrototype - - Representation() ipld.NodePrototype +func (w *_prototype) Type() schema.Type { + return w.schemaType } func (w *_prototype) Representation() ipld.NodePrototype { diff --git a/schema/typedNode.go b/schema/typedNode.go index bb27642..730805c 100644 --- a/schema/typedNode.go +++ b/schema/typedNode.go @@ -71,3 +71,16 @@ type TypedNode interface { type TypedLinkNode interface { LinkTargetNodePrototype() ipld.NodePrototype } + +// TypedPrototype is a superset of the ipld.Nodeprototype interface, and has +// additional behaviors, much like TypedNode for ipld.Node. +type TypedPrototype interface { + ipld.NodePrototype + + // Type returns a reference to the reified schema.Type value. + Type() Type + + // Representation returns an ipld.NodePrototype for the representation + // form of the prototype. + Representation() ipld.NodePrototype +} -- GitLab