Merge pull request #10 from wneessen/omitted
Some checks failed
Codecov workflow / run (push) Failing after 5s
golangci-lint / lint (push) Failing after 5s
Govulncheck Security Scan / test (push) Failing after 25s
REUSE Compliance Check / test (push) Failing after 3s
Scorecard supply-chain security / Scorecard analysis (push) Failing after 1s
SonarQube / Build and analyze (push) Failing after 2s

Add omitted functionallity
This commit is contained in:
Winni Neessen 2024-09-11 11:38:00 +02:00 committed by GitHub
commit 34dd908590
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 72 additions and 43 deletions

View file

@ -8,42 +8,6 @@ import (
"encoding/json" "encoding/json"
) )
// Variable is a generic variable type that can be null.
type Variable[T any] struct {
value T
notNil bool
}
// Value returns the value of the Variable
func (v *Variable[T]) Value() T {
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
}
// NewVariable returns a new Variable of generic type
func NewVariable[T any](value T) Variable[T] {
return Variable[T]{
notNil: true,
value: value,
}
}
// NilBoolean is an boolean type that can be nil // NilBoolean is an boolean type that can be nil
type NilBoolean = Variable[bool] type NilBoolean = Variable[bool]
@ -80,14 +44,46 @@ type NilFloat64 = Variable[float64]
// NilString is a string type that can be nil // NilString is a string type that can be nil
type NilString = Variable[string] type NilString = Variable[string]
// UnmarshalJSON satisfies the json.Unmarshaler interface for generic Variable types // Variable is a generic variable type that can be null.
func (v *Variable[T]) UnmarshalJSON(data []byte) error { type Variable[T any] struct {
if string(data) != "null" { value T
v.value = *new(T) notNil bool
v.notNil = true present bool
return json.Unmarshal(data, &v.value)
} }
return nil
// NewVariable returns a new Variable of generic type
func NewVariable[T any](value T) Variable[T] {
return Variable[T]{
notNil: true,
value: value,
}
}
// IsNil returns true when a Variable is nil
func (v *Variable[T]) IsNil() bool {
return !v.notNil
}
// NotNil returns true when a Variable is not nil
func (v *Variable[T]) NotNil() bool {
return v.notNil
}
// Omitted returns true if a value was omitted in the JSON
func (v *Variable[T]) Omitted() bool {
return !v.present
}
// 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
}
// Value returns the value of the Variable
func (v *Variable[T]) Value() T {
return v.value
} }
// MarshalJSON satisfies the json.Marshaler interface for generic Variable types // MarshalJSON satisfies the json.Marshaler interface for generic Variable types
@ -97,3 +93,14 @@ func (v *Variable[T]) MarshalJSON() ([]byte, error) {
} }
return json.Marshal(v.value) return json.Marshal(v.value)
} }
// 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
return json.Unmarshal(data, &v.value)
}
return nil
}

View file

@ -602,6 +602,28 @@ func TestVariable_MarshalJSON_UInt64(t *testing.T) {
} }
} }
func TestVariable_Omitted(t *testing.T) {
type JSONType struct {
NilValue NilBoolean `json:"nilvalue"`
Omitted NilBoolean `json:"omitted,omitempty"`
}
var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil {
t.Errorf(ErrUnmarshalFailed, err)
}
if jt.NilValue.NotNil() {
t.Error(ErrExpectedNil)
}
if jt.NilValue.Omitted() {
t.Error("expected nil value to be not omitted")
}
if !jt.Omitted.Omitted() {
t.Error("expected omitted value to be omitted")
}
}
func ExampleVariable_UnmarshalJSON() { func ExampleVariable_UnmarshalJSON() {
type JSONType struct { type JSONType struct {
Bool NilBoolean `json:"bool"` Bool NilBoolean `json:"bool"`