Skip to content

Commit

Permalink
[FIX] Further improving support for non-deserializable struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoric committed Feb 29, 2024
1 parent 90c86c6 commit 8fe2c62
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 4 additions & 3 deletions deserialize/deserialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,13 +948,14 @@ func makeFlatFieldDeserializer(fieldPath string, fieldType reflect.Type, options
var unmarshaler *func(any) (any, error)
if options.unmarshaler.ShouldUnmarshal(fieldType) {
u := func(source any) (any, error) {
result := reflect.New(fieldType).Interface()
err := options.unmarshaler.Unmarshal(source, &result)
ptrResult := reflect.New(fieldType)
anyResult := ptrResult.Interface()
err := options.unmarshaler.Unmarshal(source, &anyResult)
if err != nil {
err = fmt.Errorf("invalid data at, expected to be able to parse a %s:\n\t * %w", typeName, err)
return nil, err
}
return result, nil
return ptrResult.Elem().Interface(), nil
}
unmarshaler = &u
}
Expand Down
7 changes: 3 additions & 4 deletions deserialize/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var dictionary = reflect.TypeOf(make(JSON, 0))

// The interface for `json.Unmarshaler`.
var unmarshaler = reflect.TypeOf(new(json.Unmarshaler)).Elem()
var textUnmarshaler = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()

// Determine whether we should call the driver to unmarshal values
// of this type from []byte.
Expand All @@ -94,10 +95,8 @@ func (u Driver) ShouldUnmarshal(typ reflect.Type) bool {
if typ.ConvertibleTo(dictionary) {
return true
}
if reflect.PointerTo(typ).ConvertibleTo(unmarshaler) {
return true
}
return false
ptr := reflect.PointerTo(typ)
return ptr.ConvertibleTo(unmarshaler) || ptr.ConvertibleTo(textUnmarshaler)
}

// Perform unmarshaling.
Expand Down

0 comments on commit 8fe2c62

Please sign in to comment.