github.com/diamondburned/arikawa/v2@v2.1.0/utils/json/enum/enum.go (about)

     1  package enum
     2  
     3  import "strconv"
     4  
     5  // Null is the value used to represent JSON null.
     6  // It should never be used as a value, as it won't get serialized as such.
     7  const Null = -1
     8  
     9  // Enum is a nullable version of a uint8.
    10  // Enum values should only consist of positive values, as negative values are reserved for internal constants, such as
    11  // Null.
    12  // This also mean that only 7 of the 8 Bit will be available for storage.
    13  type Enum int8
    14  
    15  // Int8ToJSON converts the passed Enum to a byte slice with it's JSON representation.
    16  func ToJSON(i Enum) []byte {
    17  	if i == Null {
    18  		return []byte("null")
    19  	} else {
    20  		return []byte(strconv.Itoa(int(i)))
    21  	}
    22  }
    23  
    24  // Int8FromJSON decodes the Enum stored as JSON src the passed byte slice.
    25  func FromJSON(b []byte) (Enum, error) {
    26  	s := string(b)
    27  
    28  	if s == "null" {
    29  		return Null, nil
    30  	} else {
    31  		i, err := strconv.ParseUint(s, 10, 7)
    32  		return Enum(i), err
    33  	}
    34  }