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
31
niljson.go
31
niljson.go
|
@ -12,11 +12,20 @@ import (
|
||||||
type Variable[T any] struct {
|
type Variable[T any] struct {
|
||||||
value T
|
value T
|
||||||
notNil bool
|
notNil bool
|
||||||
|
present bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the value of the Variable
|
// IsNil returns true when a Variable is nil
|
||||||
func (v *Variable[T]) Value() T {
|
func (v *Variable[T]) IsNil() bool {
|
||||||
return v.value
|
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
|
// NotNil returns true when a Variable is not nil
|
||||||
|
@ -24,9 +33,9 @@ func (v *Variable[T]) NotNil() bool {
|
||||||
return v.notNil
|
return v.notNil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsNil returns true when a Variable is nil
|
// Ommited returns true if a value was omitted in the JSON
|
||||||
func (v *Variable[T]) IsNil() bool {
|
func (v *Variable[T]) Ommited() bool {
|
||||||
return !v.notNil
|
return !v.present
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset resets the value to the Variable to a zero value and sets it to be nil
|
// 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
|
v.notNil = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVariable returns a new Variable of generic type
|
// Value returns the value of the Variable
|
||||||
func NewVariable[T any](value T) Variable[T] {
|
func (v *Variable[T]) Value() T {
|
||||||
return Variable[T]{
|
return v.value
|
||||||
notNil: true,
|
|
||||||
value: value,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NilBoolean is an boolean type that can be nil
|
// 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
|
// UnmarshalJSON satisfies the json.Unmarshaler interface for generic Variable types
|
||||||
func (v *Variable[T]) UnmarshalJSON(data []byte) error {
|
func (v *Variable[T]) UnmarshalJSON(data []byte) error {
|
||||||
|
v.present = true
|
||||||
if string(data) != "null" {
|
if string(data) != "null" {
|
||||||
v.value = *new(T)
|
v.value = *new(T)
|
||||||
v.notNil = true
|
v.notNil = true
|
||||||
|
|
Loading…
Reference in a new issue