Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema: add TypedPrototype #195

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}