Skip to content

Commit

Permalink
try unmarshal first
Browse files Browse the repository at this point in the history
  • Loading branch information
damienfamed75 committed Jun 10, 2020
1 parent 96fb3ac commit 3512f27
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ func fillStruct(extra Map, a interface{}) error {
if val, ok := extra.GetOk(t.tag); ok {
field := reflect.ValueOf(a).Elem().Field(t.index)
if field.IsValid() && field.CanSet() {
if field.CanAddr() && field.CanInterface() {
bb, err := json.Marshal(val)
if err != nil {
return err
}
if err := json.Unmarshal(bb, field.Addr().Interface()); err != nil {
return err
}
}

fieldType := field.Type()
valType := reflect.TypeOf(val)
if !valType.AssignableTo(fieldType) {
if !valType.AssignableTo(fieldType) || valType.ConvertibleTo(fieldType) {
return fmt.Errorf("%s can't go in %s: %w", valType, fieldType, ErrMismatchingTypes)
}

Expand Down

0 comments on commit 3512f27

Please sign in to comment.