Skip to content

Commit

Permalink
DefaultTypeAdapter: Add support for custom string types
Browse files Browse the repository at this point in the history
The default type adapter already supports some custom scalar types but
not strings, which are relatively common. This change adds support for
that.
  • Loading branch information
alvaroaleman committed Jan 29, 2024
1 parent ba58735 commit 952d36c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions common/types/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ func nativeToValue(a Adapter, value any) (ref.Val, bool) {
case reflect.Float64:
doubleType := reflect.TypeOf(float64(0))
return Double(refValue.Convert(doubleType).Interface().(float64)), true
case reflect.String:
stringType := reflect.TypeOf("")
return String(refValue.Convert(stringType).Interface().(string)), true
}
}
return nil, false
Expand Down
2 changes: 2 additions & 0 deletions common/types/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ func TestNativeToValue_Primitive(t *testing.T) {
expectNativeToValue(t, testUint64(3), Uint(3))
expectNativeToValue(t, testFloat32(4.5), Double(4.5))
expectNativeToValue(t, testFloat64(-5.1), Double(-5.1))
expectNativeToValue(t, testString("foo"), String("foo"))

// Null conversion test.
expectNativeToValue(t, nil, NullValue)
Expand Down Expand Up @@ -876,6 +877,7 @@ type testUint32 uint32
type testUint64 uint64
type testFloat32 float32
type testFloat64 float64
type testString string

func newTestRegistry(t *testing.T, types ...proto.Message) *Registry {
t.Helper()
Expand Down

0 comments on commit 952d36c

Please sign in to comment.