Skip to content

Commit

Permalink
DefaultTypeAdapter: Add support for missing custom scalars
Browse files Browse the repository at this point in the history
The default type adapter already supports some custom scalar types but
not all, this change adds the missing ones. The most only used one is
likely string.
  • Loading branch information
alvaroaleman committed Jan 29, 2024
1 parent ba58735 commit 9002cb9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions common/types/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,12 +590,33 @@ func nativeToValue(a Adapter, value any) (ref.Val, bool) {
return NewDynamicMap(a, v), true
// type aliases of primitive types cannot be asserted as that type, but rather need
// to be downcast to int32 before being converted to a CEL representation.
case reflect.Bool:
boolTupe := reflect.TypeOf(false)
return Bool(refValue.Convert(boolTupe).Interface().(bool)), true
case reflect.Int:
intType := reflect.TypeOf(int(0))
return Int(refValue.Convert(intType).Interface().(int)), true
case reflect.Int8:
intType := reflect.TypeOf(int8(0))
return Int(refValue.Convert(intType).Interface().(int8)), true
case reflect.Int16:
intType := reflect.TypeOf(int16(0))
return Int(refValue.Convert(intType).Interface().(int16)), true
case reflect.Int32:
intType := reflect.TypeOf(int32(0))
return Int(refValue.Convert(intType).Interface().(int32)), true
case reflect.Int64:
intType := reflect.TypeOf(int64(0))
return Int(refValue.Convert(intType).Interface().(int64)), true
case reflect.Uint:
uintType := reflect.TypeOf(uint(0))
return Uint(refValue.Convert(uintType).Interface().(uint)), true
case reflect.Uint8:
uintType := reflect.TypeOf(uint8(0))
return Uint(refValue.Convert(uintType).Interface().(uint8)), true
case reflect.Uint16:
uintType := reflect.TypeOf(uint16(0))
return Uint(refValue.Convert(uintType).Interface().(uint16)), true
case reflect.Uint32:
uintType := reflect.TypeOf(uint32(0))
return Uint(refValue.Convert(uintType).Interface().(uint32)), true
Expand All @@ -608,6 +629,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
15 changes: 15 additions & 0 deletions common/types/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,19 @@ func TestNativeToValue_Primitive(t *testing.T) {
expectNativeToValue(t, &rBytes, rBytes)

// Extensions to core types.
expectNativeToValue(t, testInt(1), Int(1))
expectNativeToValue(t, testInt8(1), Int(1))
expectNativeToValue(t, testInt16(1), Int(1))
expectNativeToValue(t, testInt32(1), Int(1))
expectNativeToValue(t, testInt64(-100), Int(-100))
expectNativeToValue(t, testUint(1), Uint(1))
expectNativeToValue(t, testUint8(1), Uint(1))
expectNativeToValue(t, testUint16(1), Uint(1))
expectNativeToValue(t, testUint32(2), Uint(2))
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 @@ -870,12 +877,20 @@ func BenchmarkTypeProviderCopy(b *testing.B) {
type nonConvertible struct {
Field string
}
type testBool bool
type testInt int
type testInt8 int8
type testInt16 int16
type testInt32 int32
type testInt64 int64
type testUint uint
type testUint8 uint8
type testUint16 uint16
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 9002cb9

Please sign in to comment.