Commit ccb9eea4 authored by Daniel Martí's avatar Daniel Martí

schema: add TypedPrototype

Moving from bindnode, and adding a Type method for consistency with
TypedNode.

Fixes #181.
parent 2fc002c2
......@@ -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")
}
......
......@@ -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 {
......
......@@ -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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment