Skip to content

Commit

Permalink
Always use interface value when retrieving field values
Browse files Browse the repository at this point in the history
Instead of trying to specially handle zero field values, the code
instead depends on the values to be properly handled further down the
stack.
  • Loading branch information
patrickpichler committed Jul 18, 2024
1 parent dc9c0d2 commit 9e63be1
Showing 1 changed file with 2 additions and 24 deletions.
26 changes: 2 additions & 24 deletions ext/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (tp *nativeTypeProvider) FindStructFieldType(typeName, fieldName string) (*
GetFrom: func(obj any) (any, error) {
refVal := reflect.Indirect(reflect.ValueOf(obj))
refField := valueFieldByName(tp.options.parseStructTags, refVal, fieldName)
return getFieldValue(tp, refField), nil
return refField.Interface(), nil
},
}, true
}
Expand Down Expand Up @@ -704,29 +704,7 @@ func (t *nativeType) hasField(fieldName string) (reflect.StructField, bool) {
}

func adaptFieldValue(adapter types.Adapter, refField reflect.Value) ref.Val {
return adapter.NativeToValue(getFieldValue(adapter, refField))
}

func getFieldValue(adapter types.Adapter, refField reflect.Value) any {
if refField.IsZero() {
switch refField.Kind() {
case reflect.Array, reflect.Slice:
if refField.Type().Elem() == reflect.TypeOf(byte(0)) {
return refField.Interface()
}
return types.NewDynamicList(adapter, []ref.Val{})
case reflect.Map:
return types.NewDynamicMap(adapter, map[ref.Val]ref.Val{})
case reflect.Struct:
if refField.Type() == timestampType {
return types.Timestamp{Time: time.Unix(0, 0)}
}
return reflect.New(refField.Type()).Elem().Interface()
case reflect.Pointer:
return reflect.New(refField.Type().Elem()).Interface()
}
}
return refField.Interface()
return adapter.NativeToValue(refField.Interface())
}

func simplePkgAlias(pkgPath string) string {
Expand Down

0 comments on commit 9e63be1

Please sign in to comment.