Add support for additional types in niljson

Enhanced the niljson package to include support for byte slices and float32 types. Updated the test cases to validate these new types and modified method names to be more consistent.
This commit is contained in:
Winni Neessen 2024-09-01 16:41:53 +02:00
parent 03f6fd14d1
commit 4224968915
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 54 additions and 20 deletions

View file

@ -4,7 +4,9 @@
package niljson package niljson
import "encoding/json" import (
"encoding/json"
)
// Variable is a generic variable type that can be null. // Variable is a generic variable type that can be null.
type Variable[T any] struct { type Variable[T any] struct {
@ -12,11 +14,16 @@ type Variable[T any] struct {
notNil bool notNil bool
} }
// Get the value of the Variable // Value returns the value of the Variable
func (v *Variable[T]) Get() T { func (v *Variable[T]) Value() T {
return v.value return v.value
} }
// Get is an alias method for Value()
func (v *Variable[T]) Get() T {
return v.Value()
}
// NotNil returns true when a Variable is not nil // NotNil returns true when a Variable is not nil
func (v *Variable[T]) NotNil() bool { func (v *Variable[T]) NotNil() bool {
return v.notNil return v.notNil
@ -37,12 +44,18 @@ func (v *Variable[T]) Reset() {
// 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]
// NilByteSlice is a []byte type that can be nil
type NilByteSlice = Variable[[]byte]
// NilInt is an int type that can be nil // NilInt is an int type that can be nil
type NilInt = Variable[int] type NilInt = Variable[int]
// NilInt64 is an int64 type that can be nil // NilInt64 is an int64 type that can be nil
type NilInt64 = Variable[int64] type NilInt64 = Variable[int64]
// NilFloat32 is an float32 type that can be nil
type NilFloat32 = Variable[float32]
// NilFloat64 is an float64 type that can be nil // NilFloat64 is an float64 type that can be nil
type NilFloat64 = Variable[float64] type NilFloat64 = Variable[float64]

View file

@ -5,24 +5,38 @@
package niljson package niljson
import ( import (
"bytes"
"encoding/json" "encoding/json"
"testing" "testing"
) )
func TestVariable_UnmarshalJSON(t *testing.T) { func TestVariable_UnmarshalJSON(t *testing.T) {
jsonBytes := []byte(`{"string":"test", "int":123, "int64": 12345678901234, "float":123.456, "nil":null, "bool":true}`) jsonBytes := []byte(`{"string":"test", "int":123, "int64": 12345678901234, "float32": 1.6,
"float64":123.456, "nil":null, "bool":true, "bytes": "Ynl0ZXM="}`)
type JSONType struct { type JSONType struct {
Bool NilBoolean `json:"bool"` Bool NilBoolean `json:"bool"`
Float64 NilFloat64 `json:"float"` ByteSlice NilByteSlice `json:"bytes"`
Int NilInt `json:"int"` Float32 NilFloat32 `json:"float32"`
Int64 NilInt64 `json:"int64"` Float64 NilFloat64 `json:"float64"`
NullString NilString `json:"nil"` Int NilInt `json:"int"`
String NilString `json:"string"` Int64 NilInt64 `json:"int64"`
NullString NilString `json:"nil"`
String NilString `json:"string"`
} }
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { if err := json.Unmarshal(jsonBytes, &jt); err != nil {
t.Errorf("failed to unmarshal json with nil types: %v", err) t.Errorf("failed to unmarshal json with nil types: %v", err)
} }
if jt.Bool.IsNil() {
t.Errorf("expected not nil bool")
}
if jt.ByteSlice.IsNil() {
t.Errorf("expected not nil byte slice")
}
if jt.Float32.IsNil() {
t.Errorf("expected not nil float32")
}
if jt.Float64.IsNil() { if jt.Float64.IsNil() {
t.Errorf("expected not nil float64") t.Errorf("expected not nil float64")
} }
@ -38,20 +52,27 @@ func TestVariable_UnmarshalJSON(t *testing.T) {
if jt.NullString.NotNil() { if jt.NullString.NotNil() {
t.Errorf("expected nil string") t.Errorf("expected nil string")
} }
if !jt.Bool.Get() {
t.Errorf("expected bool to be true, got %t", jt.Bool.Get()) if !jt.Bool.Value() {
t.Errorf("expected bool to be true, got %t", jt.Bool.Value())
} }
if jt.Float64.Get() != 123.456 { if !bytes.Equal(jt.ByteSlice.Value(), []byte("bytes")) {
t.Errorf("expected float64 to be 123.456, got %f", jt.Float64.Get()) t.Errorf("expected byte slice to be %q, got %q", "bytes", jt.ByteSlice.Value())
} }
if jt.Int.Get() != 123 { if jt.Float32.Value() != 1.6 {
t.Errorf("expected int to be 123, got %d", jt.Int.Get()) t.Errorf("expected float64 to be 1.6, got %f", jt.Float32.Value())
} }
if jt.Int64.Get() != 12345678901234 { if jt.Float64.Value() != 123.456 {
t.Errorf("expected int to be 12345678901234, got %d", jt.Int64.Get()) t.Errorf("expected float64 to be 123.456, got %f", jt.Float64.Value())
} }
if jt.String.Get() != "test" { if jt.Int.Value() != 123 {
t.Errorf("expected string to be 'test', got %s", jt.String.Get()) t.Errorf("expected int to be 123, got %d", jt.Int.Value())
}
if jt.Int64.Value() != 12345678901234 {
t.Errorf("expected int to be 12345678901234, got %d", jt.Int64.Value())
}
if jt.String.Value() != "test" {
t.Errorf("expected string to be 'test', got %s", jt.String.Value())
} }
jt.Bool.Reset() jt.Bool.Reset()