diff --git a/README.md b/README.md index 1ef1f41..851bd59 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![Go badge](https://github.com/pasqal-io/godasse/actions/workflows/go.yml/badge.svg)![Golangci-lint](https://github.com/pasqal-io/godasse/actions/workflows/golangci-lint.yml/badge.svg) + # About Go Deserializer for Acceptable Safety Side-Engine (or Godasse) is an alternative deserialization mechanism for Go. diff --git a/deserialize/deserialize.go b/deserialize/deserialize.go index 6f33899..2dada42 100644 --- a/deserialize/deserialize.go +++ b/deserialize/deserialize.go @@ -189,12 +189,12 @@ func MakeKVListDeserializer[T any](options Options) (KVListDeserializer[T], erro return nil, err } deserializer := func(value kvlist.KVList) (*T, error) { - normalized := new(jsonPkg.JSON) - err := deListMap[T](*normalized, value, innerOptions) + normalized := make(jsonPkg.JSON) + err := deListMap[T](normalized, value, innerOptions) if err != nil { return nil, fmt.Errorf("error attempting to deserialize from a list of entries:\n\t * %w", err) } - return wrapped.deserializer(*normalized) + return wrapped.deserializer(normalized) } return kvListDeserializer[T]{ deserializer: deserializer, diff --git a/deserialize/deserialize_test.go b/deserialize/deserialize_test.go index a978555..4b8b7d7 100644 --- a/deserialize/deserialize_test.go +++ b/deserialize/deserialize_test.go @@ -10,6 +10,7 @@ import ( "github.com/pasqal-io/godasse/deserialize" jsonPkg "github.com/pasqal-io/godasse/deserialize/json" + "github.com/pasqal-io/godasse/deserialize/kvlist" "github.com/pasqal-io/godasse/validation" "gotest.tools/v3/assert" ) @@ -283,7 +284,7 @@ func TestValidationFailureArray(t *testing.T) { func TestKVListDoesNotSupportNesting(t *testing.T) { options := deserialize.QueryOptions("") //nolint:exhaustruct _, err := deserialize.MakeKVListDeserializer[PrimitiveTypesStruct](options) - assert.Equal(t, err, nil, "KVList parsing supports simple structurs") + assert.NilError(t, err, "KVList parsing supports simple structurs") _, err = deserialize.MakeKVListDeserializer[SimpleArrayStruct](options) assert.Equal(t, err.Error(), "this type of extractor does not support arrays/slices", "KVList parsing does not support nesting") @@ -292,6 +293,48 @@ func TestKVListDoesNotSupportNesting(t *testing.T) { assert.Equal(t, err.Error(), "this type of extractor does not support nested structs", "KVList parsing does not support nesting") } +func TestKVListSimple(t *testing.T) { + options := deserialize.QueryOptions("") //nolint:exhaustruct + deserializer, err := deserialize.MakeKVListDeserializer[PrimitiveTypesStruct](options) + assert.NilError(t, err) + + var entry kvlist.KVList = make(kvlist.KVList) + entry["SomeBool"] = []string{"true"} + entry["SomeString"] = []string{"blue"} + entry["SomeFloat32"] = []string{"3.14"} + entry["SomeFloat64"] = []string{"1.69"} + entry["SomeInt"] = []string{"-1"} + entry["SomeInt8"] = []string{"-8"} + entry["SomeInt16"] = []string{"-16"} + entry["SomeInt32"] = []string{"-32"} + entry["SomeInt64"] = []string{"-64"} + entry["SomeUint8"] = []string{"8"} + entry["SomeUint16"] = []string{"16"} + entry["SomeUint32"] = []string{"32"} + entry["SomeUint64"] = []string{"64"} + + sample := PrimitiveTypesStruct{ + SomeBool: true, + SomeString: "blue", + SomeFloat32: 3.14, + SomeFloat64: 1.69, + SomeInt: -1, + SomeInt8: -8, + SomeInt16: -16, + SomeInt32: -32, + SomeInt64: -64, + SomeUint8: 8, + SomeUint16: 16, + SomeUint32: 32, + SomeUint64: 64, + } + + deserialized, err := deserializer.DeserializeKVList(entry) + assert.NilError(t, err) + + assert.Equal(t, *deserialized, sample, "We should have extracted the expected value") +} + // Test that if we place a string instead of a primitive type, this string // will be parsed. func TestConversionsSuccess(t *testing.T) {