diff --git a/common/types/provider.go b/common/types/provider.go index 5157cd1f1..c5ff05fdb 100644 --- a/common/types/provider.go +++ b/common/types/provider.go @@ -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 @@ -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 diff --git a/common/types/provider_test.go b/common/types/provider_test.go index 56f15290c..96b20caf6 100644 --- a/common/types/provider_test.go +++ b/common/types/provider_test.go @@ -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) @@ -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()