mirror of
https://github.com/wneessen/niljson.git
synced 2024-11-22 05:30:49 +01:00
Refactor Variable type and add omitted status
Reorganized methods to ensure a logical method order and added the `present` field to track omitted values in JSON. This improves clarity and functionality by correctly indicating when a value was omitted.
This commit is contained in:
parent
ab766b23fa
commit
61e2f10196
1 changed files with 21 additions and 14 deletions
35
niljson.go
35
niljson.go
|
@ -10,13 +10,22 @@ import (
|
|||
|
||||
// Variable is a generic variable type that can be null.
|
||||
type Variable[T any] struct {
|
||||
value T
|
||||
notNil bool
|
||||
value T
|
||||
notNil bool
|
||||
present bool
|
||||
}
|
||||
|
||||
// Value returns the value of the Variable
|
||||
func (v *Variable[T]) Value() T {
|
||||
return v.value
|
||||
// IsNil returns true when a Variable is nil
|
||||
func (v *Variable[T]) IsNil() bool {
|
||||
return !v.notNil
|
||||
}
|
||||
|
||||
// NewVariable returns a new Variable of generic type
|
||||
func NewVariable[T any](value T) Variable[T] {
|
||||
return Variable[T]{
|
||||
notNil: true,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
// NotNil returns true when a Variable is not nil
|
||||
|
@ -24,9 +33,9 @@ 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
|
||||
// Ommited returns true if a value was omitted in the JSON
|
||||
func (v *Variable[T]) Ommited() bool {
|
||||
return !v.present
|
||||
}
|
||||
|
||||
// Reset resets the value to the Variable to a zero value and sets it to be nil
|
||||
|
@ -36,12 +45,9 @@ func (v *Variable[T]) Reset() {
|
|||
v.notNil = false
|
||||
}
|
||||
|
||||
// NewVariable returns a new Variable of generic type
|
||||
func NewVariable[T any](value T) Variable[T] {
|
||||
return Variable[T]{
|
||||
notNil: true,
|
||||
value: value,
|
||||
}
|
||||
// Value returns the value of the Variable
|
||||
func (v *Variable[T]) Value() T {
|
||||
return v.value
|
||||
}
|
||||
|
||||
// NilBoolean is an boolean type that can be nil
|
||||
|
@ -82,6 +88,7 @@ type NilString = Variable[string]
|
|||
|
||||
// UnmarshalJSON satisfies the json.Unmarshaler interface for generic Variable types
|
||||
func (v *Variable[T]) UnmarshalJSON(data []byte) error {
|
||||
v.present = true
|
||||
if string(data) != "null" {
|
||||
v.value = *new(T)
|
||||
v.notNil = true
|
||||
|
|
Loading…
Reference in a new issue