Documentation
¶
Overview ¶
Package bindnode provides an ipld.Node implementation via Go reflection.
This package is EXPERIMENTAL; its behavior and API might change as it's still in development.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Prototype ¶
func Prototype(ptrType interface{}, schemaType schema.Type) schema.TypedPrototype
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.
If either the Go type or schema type are nil, we infer the missing type from the other provided type. For example, we can infer an unnamed Go struct type for a schema struct tyep, and we can infer a schema Int type for a Go int64 type. The inferring logic is still a work in progress and subject to change.
When supplying a non-nil ptrType, Prototype only obtains the Go pointer type from it, so its underlying value will typically be nil. For example:
proto := bindnode.Prototype((*goType)(nil), schemaType)
Example (OnlySchema) ¶
package main
import (
"os"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/fluent/qp"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"
)
func main() {
ts := schema.TypeSystem{}
ts.Init()
ts.Accumulate(schema.SpawnString("String"))
ts.Accumulate(schema.SpawnInt("Int"))
ts.Accumulate(schema.SpawnStruct("Person",
[]schema.StructField{
schema.SpawnStructField("Name", "String", false, false),
schema.SpawnStructField("Age", "Int", true, false),
schema.SpawnStructField("Friends", "List_String", false, false),
},
schema.SpawnStructRepresentationMap(nil),
))
ts.Accumulate(schema.SpawnList("List_String", "String", false))
schemaType := ts.TypeByName("Person")
proto := bindnode.Prototype(nil, schemaType)
node, err := qp.BuildMap(proto, -1, func(ma ipld.MapAssembler) {
qp.MapEntry(ma, "Name", qp.String("Michael"))
qp.MapEntry(ma, "Friends", qp.List(-1, func(la ipld.ListAssembler) {
qp.ListEntry(la, qp.String("Sarah"))
qp.ListEntry(la, qp.String("Alex"))
}))
})
if err != nil {
panic(err)
}
nodeRepr := node.(schema.TypedNode).Representation()
dagjson.Encode(nodeRepr, os.Stdout)
}
Output: {"Friends":["Sarah","Alex"],"Name":"Michael"}
func Unwrap ¶
Unwrap takes an ipld.Node implemented by Prototype or Wrap, and returns a pointer to the inner Go value.
Unwrap returns nil if the node isn't implemented by this package.
func Wrap ¶ added in v0.11.0
Wrap implements a schema.TypedNode given a non-nil pointer to a Go value and an IPLD schema type. Note that the result is also an ipld.Node.
Wrap is meant to be used when one already has a Go value with data. As such, ptrVal must not be nil.
Similar to Prototype, if schemaType is non-nil it is assumed to be compatible with the Go type, and otherwise it's inferred from the Go type.
Example (NoSchema) ¶
package main
import (
"os"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/node/bindnode"
)
func main() {
type Person struct {
Name string
Age int64 // TODO: optional to match other examples
Friends []string
}
person := &Person{
Name: "Michael",
Friends: []string{"Sarah", "Alex"},
}
node := bindnode.Wrap(person, nil)
nodeRepr := node.Representation()
dagjson.Encode(nodeRepr, os.Stdout)
}
Output: {"Age":0,"Friends":["Sarah","Alex"],"Name":"Michael"}
Example (WithSchema) ¶
package main
import (
"os"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"
)
func main() {
ts := schema.TypeSystem{}
ts.Init()
ts.Accumulate(schema.SpawnString("String"))
ts.Accumulate(schema.SpawnInt("Int"))
ts.Accumulate(schema.SpawnStruct("Person",
[]schema.StructField{
schema.SpawnStructField("Name", "String", false, false),
schema.SpawnStructField("Age", "Int", true, false),
schema.SpawnStructField("Friends", "List_String", false, false),
},
schema.SpawnStructRepresentationMap(nil),
))
ts.Accumulate(schema.SpawnList("List_String", "String", false))
schemaType := ts.TypeByName("Person")
type Person struct {
Name string
Age *int64 // optional
Friends []string
}
person := &Person{
Name: "Michael",
Friends: []string{"Sarah", "Alex"},
}
node := bindnode.Wrap(person, schemaType)
nodeRepr := node.Representation()
dagjson.Encode(nodeRepr, os.Stdout)
}
Output: {"Friends":["Sarah","Alex"],"Name":"Michael"}
Types ¶
This section is empty.