github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/example_vec_any_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/pkg/scale"
    24  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    25  )
    26  
    27  // MyVal is a custom type that is used to hold arbitrarily encoded data. In this example, we encode uint8s with a 0x00
    28  // and strings with 0x01 as the first byte.
    29  type MyVal struct {
    30  	Value interface{}
    31  }
    32  
    33  func (a *MyVal) Decode(decoder scale.Decoder) error {
    34  	b, err := decoder.ReadOneByte()
    35  
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	if b == 0 {
    41  		var u uint8
    42  		err = decoder.Decode(&u)
    43  		a.Value = u
    44  	} else if b == 1 {
    45  		var s string
    46  		err = decoder.Decode(&s)
    47  		a.Value = s
    48  	}
    49  
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func (a MyVal) Encode(encoder scale.Encoder) error {
    58  	var err1, err2 error
    59  
    60  	switch v := a.Value.(type) {
    61  	case uint8:
    62  		err1 = encoder.PushByte(0)
    63  		err2 = encoder.Encode(v)
    64  	case string:
    65  		err1 = encoder.PushByte(1)
    66  		err2 = encoder.Encode(v)
    67  	default:
    68  		return fmt.Errorf("unknown type %T", v)
    69  	}
    70  
    71  	if err1 != nil {
    72  		return err1
    73  	}
    74  	if err2 != nil {
    75  		return err2
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func ExampleExampleVecAny() {
    82  	myValSlice := []MyVal{{uint8(12)}, {"Abc"}}
    83  
    84  	encoded, err := EncodeToBytes(myValSlice)
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	fmt.Println(encoded)
    89  
    90  	var decoded []MyVal
    91  	err = DecodeFromBytes(encoded, &decoded)
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  
    96  	fmt.Println(reflect.DeepEqual(myValSlice, decoded))
    97  	// Output: [8 0 12 1 12 65 98 99]
    98  	// true
    99  }