-
Notifications
You must be signed in to change notification settings - Fork 50
/
examples_test.go
109 lines (95 loc) · 3.31 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ipld_test
import (
"fmt"
"os"
"strings"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"
)
// Example_createDataAndMarshal shows how you can feed data into a NodeBuilder,
// and also how to then hand that to an Encoder.
//
// Often you'll encoding implicitly through a LinkSystem.Store call instead,
// but you can do it directly, too.
func Example_createDataAndMarshal() {
np := basicnode.Prototype.Any // Pick a prototype: this is how we decide what implementation will store the in-memory data.
nb := np.NewBuilder() // Create a builder.
ma, _ := nb.BeginMap(2) // Begin assembling a map.
ma.AssembleKey().AssignString("hey")
ma.AssembleValue().AssignString("it works!")
ma.AssembleKey().AssignString("yes")
ma.AssembleValue().AssignBool(true)
ma.Finish() // Call 'Finish' on the map assembly to let it know no more data is coming.
n := nb.Build() // Call 'Build' to get the resulting Node. (It's immutable!)
dagjson.Encode(n, os.Stdout)
// Output:
// {"hey":"it works!","yes":true}
}
// Example_unmarshalData shows how you can use a Decoder
// and a NodeBuilder (or NodePrototype) together to do unmarshalling.
//
// Often you'll do this implicitly through a LinkSystem.Load call instead,
// but you can do it directly, too.
func Example_unmarshalData() {
serial := strings.NewReader(`{"hey":"it works!","yes": true}`)
np := basicnode.Prototype.Any // Pick a style for the in-memory data.
nb := np.NewBuilder() // Create a builder.
dagjson.Decode(nb, serial) // Hand the builder to decoding -- decoding will fill it in!
n := nb.Build() // Call 'Build' to get the resulting Node. (It's immutable!)
fmt.Printf("the data decoded was a %s kind\n", n.Kind())
fmt.Printf("the length of the node is %d\n", n.Length())
// Output:
// the data decoded was a map kind
// the length of the node is 2
}
func ExampleLoadSchema() {
ts, err := ipld.LoadSchema("sample.ipldsch", strings.NewReader(`
type Root struct {
foo Int
bar nullable String
}
`))
if err != nil {
panic(err)
}
typeRoot := ts.TypeByName("Root").(*schema.TypeStruct)
for _, field := range typeRoot.Fields() {
fmt.Printf("field name=%q nullable=%t type=%v\n",
field.Name(), field.IsNullable(), field.Type().Name())
}
// Output:
// field name="foo" nullable=false type=Int
// field name="bar" nullable=true type=String
}
// Example_goValueWithSchema shows how to combine a Go value with an IPLD
// schema, which can then be used as an IPLD node.
//
// For more examples and documentation, see the node/bindnode package.
func Example_goValueWithSchema() {
type Person struct {
Name string
Age int
Friends []string
}
ts, err := ipld.LoadSchemaBytes([]byte(`
type Person struct {
name String
age Int
friends [String]
} representation tuple
`))
if err != nil {
panic(err)
}
schemaType := ts.TypeByName("Person")
person := &Person{Name: "Alice", Age: 34, Friends: []string{"Bob"}}
node := bindnode.Wrap(person, schemaType)
fmt.Printf("%#v\n", person)
dagjson.Encode(node.Representation(), os.Stdout)
// Output:
// &ipld_test.Person{Name:"Alice", Age:34, Friends:[]string{"Bob"}}
// ["Alice",34,["Bob"]]
}