github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/option_bool.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
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
    23  )
    24  
    25  // OptionBool is a structure that can store a Bool or a missing value
    26  // Note that encoding rules are slightly different from other "option" fields
    27  // This implementation was adopted from https://github.com/Joystream/parity-codec-go/blob/develop/noreflect/codec.go
    28  type OptionBool struct {
    29  	option
    30  	value Bool
    31  }
    32  
    33  // NewOptionBool creates an OptionBool with a value
    34  func NewOptionBool(value Bool) OptionBool {
    35  	return OptionBool{option{true}, value}
    36  }
    37  
    38  // NewOptionBoolEmpty creates an OptionBool without a value
    39  func NewOptionBoolEmpty() OptionBool {
    40  	return OptionBool{option{false}, false}
    41  }
    42  
    43  // Encode implements encoding for OptionBool as per Rust implementation
    44  func (o OptionBool) Encode(encoder scale.Encoder) error {
    45  	var err error
    46  	if !o.hasValue {
    47  		err = encoder.PushByte(0)
    48  	} else {
    49  		if o.value {
    50  			err = encoder.PushByte(1)
    51  		} else {
    52  			err = encoder.PushByte(2)
    53  		}
    54  	}
    55  	if err != nil {
    56  		return err
    57  	}
    58  	return nil
    59  }
    60  
    61  // Decode implements decoding for OptionBool as per Rust implementation
    62  func (o *OptionBool) Decode(decoder scale.Decoder) error {
    63  	b, _ := decoder.ReadOneByte()
    64  	switch b {
    65  	case 0:
    66  		o.hasValue = false
    67  		o.value = false
    68  	case 1:
    69  		o.hasValue = true
    70  		o.value = true
    71  	case 2:
    72  		o.hasValue = true
    73  		o.value = false
    74  	default:
    75  		return fmt.Errorf("unknown byte prefix for encoded OptionBool: %d", b)
    76  	}
    77  	return nil
    78  }
    79  
    80  // SetSome sets a value
    81  func (o *OptionBool) SetSome(value Bool) {
    82  	o.hasValue = true
    83  	o.value = value
    84  }
    85  
    86  // SetNone removes a value and marks it as missing
    87  func (o *OptionBool) SetNone() {
    88  	o.hasValue = false
    89  	o.value = Bool(false)
    90  }
    91  
    92  // Unwrap returns a flag that indicates whether a value is present and the stored value
    93  func (o OptionBool) Unwrap() (ok bool, value Bool) {
    94  	return o.hasValue, o.value
    95  }