Skip to content

Commit

Permalink
schema: add TypedPrototype
Browse files Browse the repository at this point in the history
Moving from bindnode, and adding a Type method for consistency with
TypedNode.

Fixes #181.
  • Loading branch information
mvdan committed Jun 14, 2021
1 parent 2fc002c commit ccb9eea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
6 changes: 3 additions & 3 deletions node/bindnode/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
}
Expand Down
14 changes: 5 additions & 9 deletions node/bindnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions schema/typedNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ccb9eea

Please sign in to comment.