github.com/ipld/go-ipld-prime@v0.21.0/node/bindnode/example_test.go (about)

     1  package bindnode_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/ipld/go-ipld-prime"
     8  	"github.com/ipld/go-ipld-prime/codec/dagjson"
     9  	"github.com/ipld/go-ipld-prime/datamodel"
    10  	"github.com/ipld/go-ipld-prime/fluent/qp"
    11  	"github.com/ipld/go-ipld-prime/node/bindnode"
    12  	"github.com/ipld/go-ipld-prime/schema"
    13  )
    14  
    15  func ExampleWrap_withSchema() {
    16  	ts, err := ipld.LoadSchemaBytes([]byte(`
    17  		type Person struct {
    18  			Name    String
    19  			Age     optional Int
    20  			Friends optional [String]
    21  		}
    22  	`))
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  	schemaType := ts.TypeByName("Person")
    27  
    28  	type Person struct {
    29  		Name    string
    30  		Age     *int64   // optional
    31  		Friends []string // optional; no need for a pointer as slices are nilable
    32  	}
    33  	person := &Person{
    34  		Name:    "Michael",
    35  		Friends: []string{"Sarah", "Alex"},
    36  	}
    37  	node := bindnode.Wrap(person, schemaType)
    38  
    39  	nodeRepr := node.Representation()
    40  	dagjson.Encode(nodeRepr, os.Stdout)
    41  
    42  	// Output:
    43  	// {"Friends":["Sarah","Alex"],"Name":"Michael"}
    44  }
    45  
    46  func ExampleWrap_noSchema() {
    47  	type Person struct {
    48  		Name    string
    49  		Age     int64 // TODO: optional to match other examples
    50  		Friends []string
    51  	}
    52  	person := &Person{
    53  		Name:    "Michael",
    54  		Friends: []string{"Sarah", "Alex"},
    55  	}
    56  	node := bindnode.Wrap(person, nil)
    57  
    58  	nodeRepr := node.Representation()
    59  	dagjson.Encode(nodeRepr, os.Stdout)
    60  
    61  	// Output:
    62  	// {"Age":0,"Friends":["Sarah","Alex"],"Name":"Michael"}
    63  }
    64  
    65  func ExamplePrototype_onlySchema() {
    66  	ts, err := ipld.LoadSchemaBytes([]byte(`
    67  		type Person struct {
    68  			Name    String
    69  			Age     optional Int
    70  			Friends [String]
    71  		}
    72  	`))
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  
    77  	schemaType := ts.TypeByName("Person")
    78  	proto := bindnode.Prototype(nil, schemaType)
    79  
    80  	node, err := qp.BuildMap(proto, -1, func(ma datamodel.MapAssembler) {
    81  		qp.MapEntry(ma, "Name", qp.String("Michael"))
    82  		qp.MapEntry(ma, "Friends", qp.List(-1, func(la datamodel.ListAssembler) {
    83  			qp.ListEntry(la, qp.String("Sarah"))
    84  			qp.ListEntry(la, qp.String("Alex"))
    85  		}))
    86  	})
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  
    91  	nodeRepr := node.(schema.TypedNode).Representation()
    92  	dagjson.Encode(nodeRepr, os.Stdout)
    93  
    94  	// Output:
    95  	// {"Friends":["Sarah","Alex"],"Name":"Michael"}
    96  }
    97  
    98  func ExamplePrototype_union() {
    99  	ts, err := ipld.LoadSchemaBytes([]byte(`
   100  		type StringOrInt union {
   101  			| String "hasString"
   102  			| Int    "hasInt"
   103  		} representation keyed
   104  	`))
   105  	if err != nil {
   106  		panic(err)
   107  	}
   108  	schemaType := ts.TypeByName("StringOrInt")
   109  
   110  	type CustomIntType int64
   111  	type StringOrInt struct {
   112  		String *string
   113  		Int    *CustomIntType // We can use custom types, too.
   114  	}
   115  
   116  	proto := bindnode.Prototype((*StringOrInt)(nil), schemaType)
   117  
   118  	node, err := qp.BuildMap(proto.Representation(), -1, func(ma datamodel.MapAssembler) {
   119  		qp.MapEntry(ma, "hasInt", qp.Int(123))
   120  	})
   121  	if err != nil {
   122  		panic(err)
   123  	}
   124  
   125  	fmt.Print("Type level DAG-JSON: ")
   126  	dagjson.Encode(node, os.Stdout)
   127  	fmt.Println()
   128  
   129  	fmt.Print("Representation level DAG-JSON: ")
   130  	nodeRepr := node.(schema.TypedNode).Representation()
   131  	dagjson.Encode(nodeRepr, os.Stdout)
   132  	fmt.Println()
   133  
   134  	// Inspect what the underlying Go value contains.
   135  	union := bindnode.Unwrap(node).(*StringOrInt)
   136  	switch {
   137  	case union.String != nil:
   138  		fmt.Printf("Go StringOrInt.String: %v\n", *union.String)
   139  	case union.Int != nil:
   140  		fmt.Printf("Go StringOrInt.Int: %v\n", *union.Int)
   141  	}
   142  
   143  	// Output:
   144  	// Type level DAG-JSON: {"Int":123}
   145  	// Representation level DAG-JSON: {"hasInt":123}
   146  	// Go StringOrInt.Int: 123
   147  }