github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/example_vec_test.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types_test
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  
    23  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    24  )
    25  
    26  func ExampleExampleVec_simple() {
    27  	ingredients := []string{"salt", "sugar"}
    28  
    29  	encoded, err := EncodeToHexString(ingredients)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	fmt.Println(encoded)
    34  
    35  	var decoded []string
    36  	err = DecodeFromHexString(encoded, &decoded)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	fmt.Println(decoded)
    41  	// Output: 0x081073616c74147375676172
    42  	// [salt sugar]
    43  }
    44  
    45  func ExampleExampleVec_struct() {
    46  	type Votes struct {
    47  		Options     [2]string
    48  		Yay         []string
    49  		Nay         []string
    50  		Outstanding []string
    51  	}
    52  
    53  	votes := Votes{
    54  		Options:     [2]string{"no deal", "muddle through"},
    55  		Yay:         []string{"Alice"},
    56  		Nay:         nil,
    57  		Outstanding: []string{"Bob", "Carol"},
    58  	}
    59  
    60  	encoded, err := EncodeToBytes(votes)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	var decoded Votes
    65  	err = DecodeFromBytes(encoded, &decoded)
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  
    70  	fmt.Println(reflect.DeepEqual(votes, decoded))
    71  	// Output: true
    72  }
    73  
    74  // type MyOption struct {
    75  // 	Woohoo *bool
    76  // }
    77  
    78  // type MyOptionNoPoointer struct {
    79  // 	Woohoo bool
    80  // }
    81  
    82  // func NewMyOption(b bool) MyOption {
    83  // 	return MyOption{&b}
    84  // }
    85  
    86  // func Example2() {
    87  // 	myopt := NewMyOption(true)
    88  // 	// myopt := NewBool(true)
    89  
    90  // 	encoded, err := EncodeToHexString(myopt)
    91  // 	if err != nil {
    92  // 		panic(err)
    93  // 	}
    94  // 	fmt.Println(encoded)
    95  
    96  // 	var decoded MyOption
    97  // 	err = DecodeFromHexString(encoded, &decoded)
    98  // 	if err != nil {
    99  // 		panic(err)
   100  // 	}
   101  // 	fmt.Println(decoded)
   102  // 	// Output: 0x081073616c74147375676172
   103  // 	// [salt sugar]
   104  // }