Skip to content

Commit 1f51886

Browse files
Use bytes interface value for zero native type struct fields instead of dynamic list (#981)
* Use bytes interface value for zero native type struct fields instead of dynamic list If a struct has a byte array/slice field and it happens to be the zero value, a dynamic list object was returned. This caused issues with functions taking `Bytes` via arguments, as the type checker will accept such functions, but they might fail at runtime. To work around this, the raw bytes array/slice is returned in case of an zero value. * Remove unused special handling cases for retrieving field values For most types, it is good enough to use the raw interface values for null types. Hence the logic of getFieldValue has been adjusted to remove some special handling code.
1 parent c9164bc commit 1f51886

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

ext/native.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (tp *nativeTypeProvider) FindStructFieldType(typeName, fieldName string) (*
279279
GetFrom: func(obj any) (any, error) {
280280
refVal := reflect.Indirect(reflect.ValueOf(obj))
281281
refField := valueFieldByName(tp.options.parseStructTags, refVal, fieldName)
282-
return getFieldValue(tp, refField), nil
282+
return getFieldValue(refField), nil
283283
},
284284
}, true
285285
}
@@ -704,21 +704,16 @@ func (t *nativeType) hasField(fieldName string) (reflect.StructField, bool) {
704704
}
705705

706706
func adaptFieldValue(adapter types.Adapter, refField reflect.Value) ref.Val {
707-
return adapter.NativeToValue(getFieldValue(adapter, refField))
707+
return adapter.NativeToValue(getFieldValue(refField))
708708
}
709709

710-
func getFieldValue(adapter types.Adapter, refField reflect.Value) any {
710+
func getFieldValue(refField reflect.Value) any {
711711
if refField.IsZero() {
712712
switch refField.Kind() {
713-
case reflect.Array, reflect.Slice:
714-
return types.NewDynamicList(adapter, []ref.Val{})
715-
case reflect.Map:
716-
return types.NewDynamicMap(adapter, map[ref.Val]ref.Val{})
717713
case reflect.Struct:
718714
if refField.Type() == timestampType {
719-
return types.Timestamp{Time: time.Unix(0, 0)}
715+
return time.Unix(0, 0)
720716
}
721-
return reflect.New(refField.Type()).Elem().Interface()
722717
case reflect.Pointer:
723718
return reflect.New(refField.Type().Elem()).Interface()
724719
}

0 commit comments

Comments
 (0)