Skip to content

Commit

Permalink
WIP: Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoric committed Apr 2, 2024
1 parent 2cf683c commit 43ba918
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
27 changes: 16 additions & 11 deletions deserialize/deserialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func MakeMapDeserializer[T any](options Options) (MapDeserializer[T], error) {
unmarshaler: options.Unmarshaler,
})
}
func MakeMapDeserializerFromReflect(options Options, typ reflect.Type, tags *tags.Tags) (MapDeserializer[any], error) {
func MakeMapDeserializerFromReflect(options Options, typ reflect.Type) (MapDeserializer[any], error) {
tagName := options.MainTagName
if tagName == "" {
return nil, errors.New("missing option MainTagName")
Expand All @@ -203,7 +203,9 @@ func MakeMapDeserializerFromReflect(options Options, typ reflect.Type, tags *tag
allowNested: true,
unmarshaler: options.Unmarshaler,
}
reflectDeserializer, err := makeFieldDeserializerFromReflect("", typ, staticOptions, tags, placeholder, false)

noTags := tags.Empty()
reflectDeserializer, err := makeFieldDeserializerFromReflect(options.RootPath, typ, staticOptions, &noTags, placeholder, false)

if err != nil {
return nil, err
Expand Down Expand Up @@ -271,7 +273,7 @@ func MakeKVDeserializerFromReflect(options Options, typ reflect.Type) (KVListDes

deserializer := func(value kvlist.KVList) (*any, error) {
normalized := make(jsonPkg.JSON)
err := deListMap[any](normalized, value, innerOptions)
err := deListMapReflect(typ, normalized, value, innerOptions)
if err != nil {
return nil, fmt.Errorf("error attempting to deserialize from a list of entries:\n\t * %w", err)
}
Expand Down Expand Up @@ -374,15 +376,13 @@ func (me kvListDeserializer[T]) DeserializeKVList(value kvlist.KVList) (*T, erro

// Convert a `map[string][]string` (as provided e.g. by the query parser) into a `Dict`
// (as consumed by this parsing mechanism).
func deListMap[T any](outMap map[string]any, inMap map[string][]string, options staticOptions) error {
var fakeValue *T
reflectedT := reflect.TypeOf(fakeValue).Elem()
if reflectedT.Kind() != reflect.Struct {
return fmt.Errorf("cannot implement a MapListDeserializer without a struct, got %s", reflectedT.Name())
func deListMapReflect(typ reflect.Type, outMap map[string]any, inMap map[string][]string, options staticOptions) error {
if typ.Kind() != reflect.Struct {
return fmt.Errorf("cannot implement a MapListDeserializer without a struct, got %s", typ.Name())
}

for i := 0; i < reflectedT.NumField(); i++ {
field := reflectedT.Field(i)
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tags, err := tagsPkg.Parse(field.Tag)
if err != nil {
// This probably cannot happen as we have already failed in makeStructDeserializerFromReflect.
Expand Down Expand Up @@ -431,14 +431,19 @@ func deListMap[T any](outMap map[string]any, inMap map[string][]string, options
case 1: // One value, we can fit it into a single entry of outMap.
outMap[*publicFieldName] = inMap[*publicFieldName][0]
default:
return fmt.Errorf("cannot fit %d elements into a single entry of field %s.%s", length, reflectedT.Name(), field.Name)
return fmt.Errorf("cannot fit %d elements into a single entry of field %s.%s", length, typ.Name(), field.Name)
}
default:
panic("This should not happen")
}
}
return nil
}
func deListMap[T any](outMap map[string]any, inMap map[string][]string, options staticOptions) error {
var placeholder T
reflectedT := reflect.TypeOf(placeholder)
return deListMapReflect(reflectedT, outMap, inMap, options)
}

// A type of deserializers using reflection to perform any conversions.
type reflectDeserializer func(slot *reflect.Value, data shared.Value) error
Expand Down
4 changes: 1 addition & 3 deletions deserialize/deserialize_reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import (

"github.com/pasqal-io/godasse/deserialize"
jsonPkg "github.com/pasqal-io/godasse/deserialize/json"
"github.com/pasqal-io/godasse/deserialize/tags"
"gotest.tools/v3/assert"
)

func twoWaysReflect[Input any, Output any](t *testing.T, sample Input) (*Output, error) {
var placeholderOutput Output
typeOutput := reflect.TypeOf(placeholderOutput)
noTags := tags.Empty()
deserializer, err := deserialize.MakeMapDeserializerFromReflect(deserialize.Options{
Unmarshaler: jsonPkg.Driver{},
MainTagName: "json",
RootPath: "",
}, typeOutput, &noTags)
}, typeOutput)
if err != nil {
t.Error(err)
return nil, err //nolint:wrapcheck
Expand Down

0 comments on commit 43ba918

Please sign in to comment.