github.com/diamondburned/arikawa@v1.3.14/utils/json/option/number.go (about)

     1  package option
     2  
     3  import "strconv"
     4  
     5  // ================================ Uint ================================
     6  
     7  // Uint is the option type for unsigned integers (uint).
     8  type Uint *uint
     9  
    10  // ZeroUint is a Uint with 0 as value.
    11  var ZeroUint = NewUint(0)
    12  
    13  // NewUint creates a new Uint using the value of the passed uint.
    14  func NewUint(u uint) Uint { return &u }
    15  
    16  // ================================ Int ================================
    17  
    18  // Int is the option type for integers (int).
    19  type Int *int
    20  
    21  // ZeroInt is an Int with 0 as value.
    22  var ZeroInt = NewInt(0)
    23  
    24  // NewInt creates a new Int using the value of the passed int.
    25  func NewInt(i int) Int { return &i }
    26  
    27  // ================================ NullableUint ================================
    28  
    29  // NullableUint is a nullable version of an unsigned integer (uint).
    30  type NullableUint = *NullableUintData
    31  
    32  type NullableUintData struct {
    33  	Val  uint
    34  	Init bool
    35  }
    36  
    37  // NullUint serializes to JSON null.
    38  var NullUint = &NullableUintData{}
    39  
    40  // NewUint creates a new non-null NullableUint using the value of the passed uint.
    41  func NewNullableUint(v uint) NullableUint {
    42  	return &NullableUintData{
    43  		Val:  v,
    44  		Init: true,
    45  	}
    46  }
    47  
    48  func (u NullableUintData) MarshalJSON() ([]byte, error) {
    49  	if !u.Init {
    50  		return []byte("null"), nil
    51  	}
    52  	return []byte(strconv.FormatUint(uint64(u.Val), 10)), nil
    53  }
    54  
    55  func (u *NullableUintData) UnmarshalJSON(json []byte) error {
    56  	s := string(json)
    57  
    58  	if s == "null" {
    59  		return nil
    60  	}
    61  
    62  	v, err := strconv.ParseUint(s, 10, 64)
    63  
    64  	u.Val = uint(v)
    65  	u.Init = true
    66  
    67  	return err
    68  }
    69  
    70  // ================================ NullableInt ================================
    71  
    72  // NullableInt is a nullable version of an integer (int).
    73  type NullableInt = *NullableIntData
    74  
    75  type NullableIntData struct {
    76  	Val  int
    77  	Init bool
    78  }
    79  
    80  // NullInt serializes to JSON null.
    81  var NullInt = &NullableIntData{}
    82  
    83  // NewInt creates a new non-null NullableInt using the value of the passed int.
    84  func NewNullableInt(v int) NullableInt {
    85  	return &NullableIntData{
    86  		Val:  v,
    87  		Init: true,
    88  	}
    89  }
    90  
    91  func (i NullableIntData) MarshalJSON() ([]byte, error) {
    92  	if !i.Init {
    93  		return []byte("null"), nil
    94  	}
    95  	return []byte(strconv.FormatUint(uint64(i.Val), 10)), nil
    96  }
    97  
    98  func (i *NullableIntData) UnmarshalJSON(json []byte) error {
    99  	s := string(json)
   100  
   101  	if s == "null" {
   102  		return nil
   103  	}
   104  
   105  	v, err := strconv.ParseUint(s, 10, 64)
   106  
   107  	i.Val = int(v)
   108  	i.Init = true
   109  
   110  	return err
   111  }