Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NilByteSlice ¶
NilByteSlice is a []byte type that can be nil
type Variable ¶
type Variable[T any] struct { // contains filtered or unexported fields }
Variable is a generic variable type that can be null.
func NewVariable ¶ added in v0.0.4
NewVariable returns a new Variable of generic type
func (*Variable[T]) MarshalJSON ¶ added in v0.0.4
MarshalJSON satisfies the json.Marshaler interface for generic Variable types
Example ¶
type JSONType struct { Bool NilBoolean `json:"bool"` ByteSlice NilByteSlice `json:"bytes"` Float32 NilFloat32 `json:"float32,omitempty"` Float64 NilFloat64 `json:"float64"` Int NilInt `json:"int"` Int64 NilInt64 `json:"int64"` NullString NilString `json:"nilvalue,omitempty"` String NilString `json:"string"` UInt NilUInt `json:"uint"` UInt8 NilUInt8 `json:"uint8"` } example := &JSONType{ Bool: NewVariable(false), ByteSlice: NewVariable([]byte("bytes")), Float64: NewVariable(123.456), Int: NewVariable(123), Int64: NewVariable(int64(12345678901234)), String: NewVariable("test"), UInt: NewVariable(uint(123)), } data, err := json.Marshal(example) if err != nil { fmt.Printf("failed to marshal JSON: %s", err) os.Exit(1) } fmt.Println(string(data))
Output: {"bool":false,"bytes":"Ynl0ZXM=","float32":null,"float64":123.456,"int":123,"int64":12345678901234,"nilvalue":null,"string":"test","uint":123,"uint8":null}
func (*Variable[T]) Omitted ¶ added in v0.1.0
Omitted returns true if a value was omitted in the JSON
func (*Variable[T]) Reset ¶
func (v *Variable[T]) Reset()
Reset resets the value to the Variable to a zero value and sets it to be nil
func (*Variable[T]) UnmarshalJSON ¶
UnmarshalJSON satisfies the json.Unmarshaler interface for generic Variable types
Example ¶
type JSONType struct { Bool NilBoolean `json:"bool"` ByteSlice NilByteSlice `json:"bytes"` Float32 NilFloat32 `json:"float32,omitempty"` Float64 NilFloat64 `json:"float64"` Int NilInt `json:"int"` Int64 NilInt64 `json:"int64"` NullString NilString `json:"nilvalue,omitempty"` String NilString `json:"string"` } data := []byte(`{ "bytes": "Ynl0ZXM=", "bool": true, "float32": null, "float64":0, "int": 123, "int64": 12345678901234, "nilvalue": null, "string":"test" }`) var example JSONType var output string if err := json.Unmarshal(data, &example); err != nil { fmt.Println("failed to unmarshal JSON:", err) os.Exit(1) } if example.Bool.NotNil() { output += fmt.Sprintf("Bool is: %t, ", example.Bool.Value()) } if example.Float32.IsNil() { output += "Float 32 is nil, " } if example.Float64.NotNil() { output += fmt.Sprintf("Float 64 is: %f, ", example.Float64.Value()) } if example.String.NotNil() { output += fmt.Sprintf("String is: %s", example.String.Value()) } fmt.Println(output)
Output: Bool is: true, Float 32 is nil, Float 64 is: 0.000000, String is: test
Click to show internal directories.
Click to hide internal directories.