github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/option_uint.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 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale"
    20  
    21  // OptionU8 is a structure that can store a U8 or a missing value
    22  type OptionU8 struct {
    23  	option
    24  	value U8
    25  }
    26  
    27  // NewOptionU8 creates an OptionU8 with a value
    28  func NewOptionU8(value U8) OptionU8 {
    29  	return OptionU8{option{true}, value}
    30  }
    31  
    32  // NewOptionU8Empty creates an OptionU8 without a value
    33  func NewOptionU8Empty() OptionU8 {
    34  	return OptionU8{option: option{false}}
    35  }
    36  
    37  func (o OptionU8) Encode(encoder scale.Encoder) error {
    38  	return encoder.EncodeOption(o.hasValue, o.value)
    39  }
    40  
    41  func (o *OptionU8) Decode(decoder scale.Decoder) error {
    42  	return decoder.DecodeOption(&o.hasValue, &o.value)
    43  }
    44  
    45  // SetSome sets a value
    46  func (o *OptionU8) SetSome(value U8) {
    47  	o.hasValue = true
    48  	o.value = value
    49  }
    50  
    51  // SetNone removes a value and marks it as missing
    52  func (o *OptionU8) SetNone() {
    53  	o.hasValue = false
    54  	o.value = U8(0)
    55  }
    56  
    57  // Unwrap returns a flag that indicates whether a value is present and the stored value
    58  func (o OptionU8) Unwrap() (ok bool, value U8) {
    59  	return o.hasValue, o.value
    60  }
    61  
    62  // OptionU16 is a structure that can store a U16 or a missing value
    63  type OptionU16 struct {
    64  	option
    65  	value U16
    66  }
    67  
    68  // NewOptionU16 creates an OptionU16 with a value
    69  func NewOptionU16(value U16) OptionU16 {
    70  	return OptionU16{option{true}, value}
    71  }
    72  
    73  // NewOptionU16Empty creates an OptionU16 without a value
    74  func NewOptionU16Empty() OptionU16 {
    75  	return OptionU16{option: option{false}}
    76  }
    77  
    78  func (o OptionU16) Encode(encoder scale.Encoder) error {
    79  	return encoder.EncodeOption(o.hasValue, o.value)
    80  }
    81  
    82  func (o *OptionU16) Decode(decoder scale.Decoder) error {
    83  	return decoder.DecodeOption(&o.hasValue, &o.value)
    84  }
    85  
    86  // SetSome sets a value
    87  func (o *OptionU16) SetSome(value U16) {
    88  	o.hasValue = true
    89  	o.value = value
    90  }
    91  
    92  // SetNone removes a value and marks it as missing
    93  func (o *OptionU16) SetNone() {
    94  	o.hasValue = false
    95  	o.value = U16(0)
    96  }
    97  
    98  // Unwrap returns a flag that indicates whether a value is present and the stored value
    99  func (o OptionU16) Unwrap() (ok bool, value U16) {
   100  	return o.hasValue, o.value
   101  }
   102  
   103  // OptionU32 is a structure that can store a U32 or a missing value
   104  type OptionU32 struct {
   105  	option
   106  	value U32
   107  }
   108  
   109  // NewOptionU32 creates an OptionU32 with a value
   110  func NewOptionU32(value U32) OptionU32 {
   111  	return OptionU32{option{true}, value}
   112  }
   113  
   114  // NewOptionU32Empty creates an OptionU32 without a value
   115  func NewOptionU32Empty() OptionU32 {
   116  	return OptionU32{option: option{false}}
   117  }
   118  
   119  func (o OptionU32) Encode(encoder scale.Encoder) error {
   120  	return encoder.EncodeOption(o.hasValue, o.value)
   121  }
   122  
   123  func (o *OptionU32) Decode(decoder scale.Decoder) error {
   124  	return decoder.DecodeOption(&o.hasValue, &o.value)
   125  }
   126  
   127  // SetSome sets a value
   128  func (o *OptionU32) SetSome(value U32) {
   129  	o.hasValue = true
   130  	o.value = value
   131  }
   132  
   133  // SetNone removes a value and marks it as missing
   134  func (o *OptionU32) SetNone() {
   135  	o.hasValue = false
   136  	o.value = U32(0)
   137  }
   138  
   139  // Unwrap returns a flag that indicates whether a value is present and the stored value
   140  func (o OptionU32) Unwrap() (ok bool, value U32) {
   141  	return o.hasValue, o.value
   142  }
   143  
   144  // OptionU64 is a structure that can store a U64 or a missing value
   145  type OptionU64 struct {
   146  	option
   147  	value U64
   148  }
   149  
   150  // NewOptionU64 creates an OptionU64 with a value
   151  func NewOptionU64(value U64) OptionU64 {
   152  	return OptionU64{option{true}, value}
   153  }
   154  
   155  // NewOptionU64Empty creates an OptionU64 without a value
   156  func NewOptionU64Empty() OptionU64 {
   157  	return OptionU64{option: option{false}}
   158  }
   159  
   160  func (o OptionU64) Encode(encoder scale.Encoder) error {
   161  	return encoder.EncodeOption(o.hasValue, o.value)
   162  }
   163  
   164  func (o *OptionU64) Decode(decoder scale.Decoder) error {
   165  	return decoder.DecodeOption(&o.hasValue, &o.value)
   166  }
   167  
   168  // SetSome sets a value
   169  func (o *OptionU64) SetSome(value U64) {
   170  	o.hasValue = true
   171  	o.value = value
   172  }
   173  
   174  // SetNone removes a value and marks it as missing
   175  func (o *OptionU64) SetNone() {
   176  	o.hasValue = false
   177  	o.value = U64(0)
   178  }
   179  
   180  // Unwrap returns a flag that indicates whether a value is present and the stored value
   181  func (o OptionU64) Unwrap() (ok bool, value U64) {
   182  	return o.hasValue, o.value
   183  }