Documentation ¶
Overview ¶
Package bindnode provides an ld.Node implementation via Go reflection.
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 LD schema type. Note that the result is also an ld.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" ld "gitlab.dms3.io/ld/go-ld-prime" "gitlab.dms3.io/ld/go-ld-prime/codec/dagjson" "gitlab.dms3.io/ld/go-ld-prime/fluent/qp" "gitlab.dms3.io/ld/go-ld-prime/node/bindnode" "gitlab.dms3.io/ld/go-ld-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 ld.MapAssembler) { qp.MapEntry(ma, "Name", qp.String("Michael")) qp.MapEntry(ma, "Friends", qp.List(-1, func(la ld.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 ld.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 ¶
Wrap implements a schema.TypedNode given a non-nil pointer to a Go value and an LD schema type. Note that the result is also an ld.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 (WithSchema) ¶
package main import ( "os" "gitlab.dms3.io/ld/go-ld-prime/codec/dagjson" "gitlab.dms3.io/ld/go-ld-prime/node/bindnode" "gitlab.dms3.io/ld/go-ld-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.