Skip to content

Commit

Permalink
Merge pull request #5 from pasqal-io/yoric/kv
Browse files Browse the repository at this point in the history
[FIX] KVList was broken
  • Loading branch information
David Teller authored Dec 28, 2023
2 parents 738a573 + f8280e0 commit 66a8c5a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions deserialize/deserialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 44 additions & 1 deletion deserialize/deserialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand All @@ -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) {
Expand Down

0 comments on commit 66a8c5a

Please sign in to comment.