github.com/diamondburned/arikawa/v2@v2.1.0/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  		*u = *NullUint
    60  		return nil
    61  	}
    62  
    63  	v, err := strconv.ParseUint(s, 10, 64)
    64  
    65  	u.Val = uint(v)
    66  	u.Init = true
    67  
    68  	return err
    69  }
    70  
    71  // ================================ NullableInt ================================
    72  
    73  // NullableInt is a nullable version of an integer (int).
    74  type NullableInt = *NullableIntData
    75  
    76  type NullableIntData struct {
    77  	Val  int
    78  	Init bool
    79  }
    80  
    81  // NullInt serializes to JSON null.
    82  var NullInt = &NullableIntData{}
    83  
    84  // NewInt creates a new non-null NullableInt using the value of the passed int.
    85  func NewNullableInt(v int) NullableInt {
    86  	return &NullableIntData{
    87  		Val:  v,
    88  		Init: true,
    89  	}
    90  }
    91  
    92  func (i NullableIntData) MarshalJSON() ([]byte, error) {
    93  	if !i.Init {
    94  		return []byte("null"), nil
    95  	}
    96  	return []byte(strconv.FormatUint(uint64(i.Val), 10)), nil
    97  }
    98  
    99  func (i *NullableIntData) UnmarshalJSON(json []byte) error {
   100  	s := string(json)
   101  
   102  	if s == "null" {
   103  		*i = *NullInt
   104  		return nil
   105  	}
   106  
   107  	v, err := strconv.ParseUint(s, 10, 64)
   108  
   109  	i.Val = int(v)
   110  	i.Init = true
   111  
   112  	return err
   113  }