2024-09-01 16:01:58 +02:00
|
|
|
// SPDX-FileCopyrightText: 2024 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package niljson
|
|
|
|
|
2024-09-01 16:41:53 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
2024-09-01 16:01:58 +02:00
|
|
|
|
|
|
|
// Variable is a generic variable type that can be null.
|
|
|
|
type Variable[T any] struct {
|
|
|
|
value T
|
|
|
|
notNil bool
|
|
|
|
}
|
|
|
|
|
2024-09-01 16:41:53 +02:00
|
|
|
// Value returns the value of the Variable
|
|
|
|
func (v *Variable[T]) Value() T {
|
2024-09-01 16:01:58 +02:00
|
|
|
return v.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotNil returns true when a Variable is not nil
|
|
|
|
func (v *Variable[T]) NotNil() bool {
|
|
|
|
return v.notNil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil returns true when a Variable is nil
|
|
|
|
func (v *Variable[T]) IsNil() bool {
|
|
|
|
return !v.notNil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the value to the Variable to a zero value and sets it to be nil
|
|
|
|
func (v *Variable[T]) Reset() {
|
|
|
|
var newVal T
|
|
|
|
v.value = newVal
|
|
|
|
v.notNil = false
|
|
|
|
}
|
|
|
|
|
2024-09-02 12:13:54 +02:00
|
|
|
// NewVariable returns a new Variable of generic type
|
|
|
|
func NewVariable[T any](value T) Variable[T] {
|
|
|
|
return Variable[T]{
|
|
|
|
notNil: true,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-01 16:01:58 +02:00
|
|
|
// NilBoolean is an boolean type that can be nil
|
|
|
|
type NilBoolean = Variable[bool]
|
|
|
|
|
2024-09-01 16:41:53 +02:00
|
|
|
// NilByteSlice is a []byte type that can be nil
|
|
|
|
type NilByteSlice = Variable[[]byte]
|
|
|
|
|
2024-09-01 16:01:58 +02:00
|
|
|
// NilInt is an int type that can be nil
|
|
|
|
type NilInt = Variable[int]
|
|
|
|
|
|
|
|
// NilInt64 is an int64 type that can be nil
|
|
|
|
type NilInt64 = Variable[int64]
|
|
|
|
|
2024-09-01 19:24:45 +02:00
|
|
|
// NilUInt is an uint type that can be nil
|
|
|
|
type NilUInt = Variable[uint]
|
|
|
|
|
|
|
|
// NilUInt8 is an uint8 type that can be nil
|
|
|
|
type NilUInt8 = Variable[uint8]
|
|
|
|
|
|
|
|
// NilUInt16 is an uint16 type that can be nil
|
|
|
|
type NilUInt16 = Variable[uint16]
|
|
|
|
|
|
|
|
// NilUInt32 is an uint32 type that can be nil
|
|
|
|
type NilUInt32 = Variable[uint32]
|
|
|
|
|
|
|
|
// NilUInt64 is an uint64 type that can be nil
|
|
|
|
type NilUInt64 = Variable[uint64]
|
|
|
|
|
2024-09-01 16:41:53 +02:00
|
|
|
// NilFloat32 is an float32 type that can be nil
|
|
|
|
type NilFloat32 = Variable[float32]
|
|
|
|
|
2024-09-01 16:01:58 +02:00
|
|
|
// NilFloat64 is an float64 type that can be nil
|
|
|
|
type NilFloat64 = Variable[float64]
|
|
|
|
|
|
|
|
// NilString is a string type that can be nil
|
|
|
|
type NilString = Variable[string]
|
|
|
|
|
2024-09-02 12:13:54 +02:00
|
|
|
// UnmarshalJSON satisfies the json.Unmarshaler interface for generic Variable types
|
2024-09-01 16:01:58 +02:00
|
|
|
func (v *Variable[T]) UnmarshalJSON(data []byte) error {
|
|
|
|
if string(data) != "null" {
|
|
|
|
v.value = *new(T)
|
|
|
|
v.notNil = true
|
|
|
|
return json.Unmarshal(data, &v.value)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-02 12:13:54 +02:00
|
|
|
|
|
|
|
// MarshalJSON satisfies the json.Marshaler interface for generic Variable types
|
|
|
|
func (v *Variable[T]) MarshalJSON() ([]byte, error) {
|
|
|
|
if !v.notNil {
|
|
|
|
return json.Marshal(nil)
|
|
|
|
}
|
|
|
|
return json.Marshal(v.value)
|
|
|
|
}
|