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