Documentation
¶
Index ¶
- func Prototype(ptrType interface{}, schemaType schema.Type) ipld.NodePrototype
- func PrototypeNoSchema(ptrType interface{}) ipld.NodePrototype
- func PrototypeOnlySchema(schemaType schema.Type) ipld.NodePrototype
- func Unwrap(node ipld.Node) (ptr interface{})
- func WrapNoSchema(ptr interface{}) ipld.Node
- type TypedAssembler
- type TypedPrototype
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Prototype ¶
func Prototype(ptrType interface{}, schemaType schema.Type) ipld.NodePrototype
Prototype implements an ipld.NodePrototype given a Go pointer type and an IPLD schema type.
In this form, it is assumed that the Go type and IPLD schema type are compatible. TODO: check upfront and panic otherwise
func PrototypeNoSchema ¶
func PrototypeNoSchema(ptrType interface{}) ipld.NodePrototype
PrototypeNoSchema implements an ipld.NodePrototype given a Go pointer type.
In this form, no IPLD schema is used; it is entirely inferred from the Go type.
Go types map to schema types in simple ways: Go string to schema String, Go []byte to schema Bytes, Go struct to schema Map, and so on.
A Go struct field is optional when its type is a pointer. Nullable fields are not supported in this mode.
func PrototypeOnlySchema ¶
func PrototypeOnlySchema(schemaType schema.Type) ipld.NodePrototype
PrototypeOnlySchema implements an ipld.NodePrototype given an IPLD schema type.
In this form, Go values are constructed with types inferred from the IPLD schema, like a reverse of PrototypeNoSchema.
Example ¶
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"
"github.com/polydawn/refmt/json"
)
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.PrototypeOnlySchema(schemaType)
n, 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)
}
nr := n.(schema.TypedNode).Representation()
dagjson.Marshal(nr, json.NewEncoder(os.Stdout, json.EncodeOptions{}), true)
}
Output: {"Name":"Michael","Friends":["Sarah","Alex"]}
func Unwrap ¶
Unwrap takes an ipld.Node implemented by one of the Wrap* or Prototype* APIs, and returns a pointer to the inner Go value.
Unwrap returns the input node if the node isn't implemented by this package.
func WrapNoSchema ¶
WrapNoSchema implements an ipld.Node given a pointer to a Go value.
Same rules as PrototypeNoSchema apply.
Types ¶
type TypedAssembler ¶
type TypedAssembler interface {
ipld.NodeAssembler
Representation() ipld.NodeAssembler
}
type TypedPrototype ¶
type TypedPrototype interface {
ipld.NodePrototype
Representation() ipld.NodePrototype
}