github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/example_enum_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 // PhaseEnum is an enum example. Since Go has no enums, it is implemented as a struct with flags for each 28 // potential value and a corresponding value if needed. This enum represents phases, the values are `ApplyExtrinsic` 29 // which can be a uint32 and `Finalization`. By implementing Encode and Decode methods on this struct that satisfy the 30 // scale.Encodeable and scale.Decodeable interfaces, we encode our enum struct to correspond to the scale codec 31 // (see https://substrate.dev/docs/en/overview/low-level-data-format for a description). 32 type PhaseEnum struct { 33 IsApplyExtrinsic bool 34 AsApplyExtrinsic uint32 35 IsFinalization bool 36 } 37 38 func (m *PhaseEnum) Decode(decoder scale.Decoder) error { 39 b, err := decoder.ReadOneByte() 40 41 if err != nil { 42 return err 43 } 44 45 if b == 0 { 46 m.IsApplyExtrinsic = true 47 err = decoder.Decode(&m.AsApplyExtrinsic) 48 } else if b == 1 { 49 m.IsFinalization = true 50 } 51 52 if err != nil { 53 return err 54 } 55 56 return nil 57 } 58 59 func (m PhaseEnum) Encode(encoder scale.Encoder) error { 60 var err1, err2 error 61 if m.IsApplyExtrinsic { 62 err1 = encoder.PushByte(0) 63 err2 = encoder.Encode(m.AsApplyExtrinsic) 64 } else if m.IsFinalization { 65 err1 = encoder.PushByte(1) 66 } 67 68 if err1 != nil { 69 return err1 70 } 71 if err2 != nil { 72 return err2 73 } 74 75 return nil 76 } 77 78 func ExampleExampleEnum_applyExtrinsic() { 79 applyExtrinsic := PhaseEnum{ 80 IsApplyExtrinsic: true, 81 AsApplyExtrinsic: 1234, 82 } 83 84 enc, err := EncodeToHexString(applyExtrinsic) 85 if err != nil { 86 panic(err) 87 } 88 89 var dec PhaseEnum 90 err = DecodeFromHexString(enc, &dec) 91 if err != nil { 92 panic(err) 93 } 94 95 fmt.Println(reflect.DeepEqual(applyExtrinsic, dec)) 96 } 97 98 func ExampleExampleEnum_finalization() { 99 finalization := PhaseEnum{ 100 IsFinalization: true, 101 } 102 103 enc, err := EncodeToHexString(finalization) 104 if err != nil { 105 panic(err) 106 } 107 108 var dec PhaseEnum 109 err = DecodeFromHexString(enc, &dec) 110 if err != nil { 111 panic(err) 112 } 113 114 fmt.Println(reflect.DeepEqual(finalization, dec)) 115 }