diff --git a/cel/cel_test.go b/cel/cel_test.go index cbe89c3e7..57e16f7e6 100644 --- a/cel/cel_test.go +++ b/cel/cel_test.go @@ -17,6 +17,7 @@ package cel import ( "bytes" "context" + "encoding/json" "errors" "fmt" "os" @@ -239,6 +240,43 @@ func TestAbbrevsDisambiguation(t *testing.T) { } } +func TestConvertToNativeJSONStructure(t *testing.T) { + env, err := NewEnv() + if err != nil { + t.Fatalf("NewEnv() failed: %v", err) + } + ast, issues := env.Compile(`{ + "parts": [{"kind": "text"}] + }`) + if issues != nil && issues.Err() != nil { + t.Fatal(issues.Err()) + } + + prg, err := env.Program(ast) + if err != nil { + t.Fatal(err) + } + + result, _, err := prg.Eval(map[string]any{}) + if err != nil { + t.Fatal(err) + } + + native, err := result.ConvertToNative(types.JSONValueType) + if err != nil { + t.Fatal(err) + } + + jsonBytes, err := json.Marshal(native) + if err != nil { + t.Fatalf("json.Marshal failed: %v", err) + } + want := `{"parts":[{"kind":"text"}]}` + if string(jsonBytes) != want { + t.Errorf("json.Marshal() failed, got : %s, wanted ", jsonBytes) + } +} + func TestCustomEnvError(t *testing.T) { env, err := NewCustomEnv(StdLib(), StdLib()) if err != nil { diff --git a/common/types/bool.go b/common/types/bool.go index 1f9e10739..5f1e4573e 100644 --- a/common/types/bool.go +++ b/common/types/bool.go @@ -69,7 +69,7 @@ func (b Bool) ConvertToNative(typeDesc reflect.Type) (any, error) { case boolWrapperType: // Convert the bool to a wrapperspb.BoolValue. return wrapperspb.Bool(bool(b)), nil - case jsonValueType: + case JSONValueType: // Return the bool as a new structpb.Value. return structpb.NewBoolValue(bool(b)), nil default: diff --git a/common/types/bool_test.go b/common/types/bool_test.go index bb211e13e..6522b34e5 100644 --- a/common/types/bool_test.go +++ b/common/types/bool_test.go @@ -76,7 +76,7 @@ func TestBoolConvertToNative_Error(t *testing.T) { } func TestBoolConvertToNative_Json(t *testing.T) { - val, err := True.ConvertToNative(jsonValueType) + val, err := True.ConvertToNative(JSONValueType) pbVal := &structpb.Value{Kind: &structpb.Value_BoolValue{BoolValue: true}} if err != nil { t.Error(err) diff --git a/common/types/bytes.go b/common/types/bytes.go index b59e1fc20..88da05315 100644 --- a/common/types/bytes.go +++ b/common/types/bytes.go @@ -79,7 +79,7 @@ func (b Bytes) ConvertToNative(typeDesc reflect.Type) (any, error) { case byteWrapperType: // Convert the bytes to a wrapperspb.BytesValue. return wrapperspb.Bytes([]byte(b)), nil - case jsonValueType: + case JSONValueType: // CEL follows the proto3 to JSON conversion by encoding bytes to a string via base64. // The encoding below matches the golang 'encoding/json' behavior during marshaling, // which uses base64.StdEncoding. diff --git a/common/types/bytes_test.go b/common/types/bytes_test.go index 498a168fc..326f18091 100644 --- a/common/types/bytes_test.go +++ b/common/types/bytes_test.go @@ -97,7 +97,7 @@ func TestBytesConvertToNative_Error(t *testing.T) { } func TestBytesConvertToNative_Json(t *testing.T) { - val, err := Bytes("123").ConvertToNative(jsonValueType) + val, err := Bytes("123").ConvertToNative(JSONValueType) if err != nil { t.Error(err) } diff --git a/common/types/double.go b/common/types/double.go index 1e7de9d6e..02abfee2d 100644 --- a/common/types/double.go +++ b/common/types/double.go @@ -89,7 +89,7 @@ func (d Double) ConvertToNative(typeDesc reflect.Type) (any, error) { case floatWrapperType: // Convert to a wrapperspb.FloatValue (with truncation). return wrapperspb.Float(float32(d)), nil - case jsonValueType: + case JSONValueType: // Note, there are special cases for proto3 to json conversion that // expect the floating point value to be converted to a NaN, // Infinity, or -Infinity string values, but the jsonpb string diff --git a/common/types/double_test.go b/common/types/double_test.go index 239e74d31..47e875f29 100644 --- a/common/types/double_test.go +++ b/common/types/double_test.go @@ -171,7 +171,7 @@ func TestDoubleConvertToNative_Float64(t *testing.T) { } func TestDoubleConvertToNative_Json(t *testing.T) { - val, err := Double(-1.4).ConvertToNative(jsonValueType) + val, err := Double(-1.4).ConvertToNative(JSONValueType) pbVal := structpb.NewNumberValue(-1.4) if err != nil { t.Error(err) @@ -179,7 +179,7 @@ func TestDoubleConvertToNative_Json(t *testing.T) { t.Errorf("Got '%v', expected -1.4", val) } - val, err = Double(math.NaN()).ConvertToNative(jsonValueType) + val, err = Double(math.NaN()).ConvertToNative(JSONValueType) if err != nil { t.Error(err) } else { @@ -189,14 +189,14 @@ func TestDoubleConvertToNative_Json(t *testing.T) { } } - val, err = Double(math.Inf(-1)).ConvertToNative(jsonValueType) + val, err = Double(math.Inf(-1)).ConvertToNative(JSONValueType) pbVal = structpb.NewNumberValue(math.Inf(-1)) if err != nil { t.Error(err) } else if !proto.Equal(val.(proto.Message), pbVal) { t.Errorf("Got '%v', expected -Infinity", val) } - val, err = Double(math.Inf(0)).ConvertToNative(jsonValueType) + val, err = Double(math.Inf(0)).ConvertToNative(JSONValueType) pbVal = structpb.NewNumberValue(math.Inf(0)) if err != nil { t.Error(err) diff --git a/common/types/duration.go b/common/types/duration.go index be58d567e..220714773 100644 --- a/common/types/duration.go +++ b/common/types/duration.go @@ -106,7 +106,7 @@ func (d Duration) ConvertToNative(typeDesc reflect.Type) (any, error) { case durationValueType: // Unwrap the CEL value to its underlying proto value. return dpb.New(d.Duration), nil - case jsonValueType: + case JSONValueType: // CEL follows the proto3 to JSON conversion. // Note, using jsonpb would wrap the result in extra double quotes. v := d.ConvertToType(StringType) diff --git a/common/types/duration_test.go b/common/types/duration_test.go index d52170539..f0e4509e8 100644 --- a/common/types/duration_test.go +++ b/common/types/duration_test.go @@ -166,7 +166,7 @@ func TestDurationConvertToNative_Any(t *testing.T) { } func TestDurationConvertToNative_Error(t *testing.T) { - val, err := Duration{Duration: duration(7506, 1000)}.ConvertToNative(jsonValueType) + val, err := Duration{Duration: duration(7506, 1000)}.ConvertToNative(JSONValueType) if err != nil { t.Errorf("Got error: '%v', expected value", err) } @@ -178,7 +178,7 @@ func TestDurationConvertToNative_Error(t *testing.T) { } func TestDurationConvertToNative_Json(t *testing.T) { - val, err := Duration{Duration: duration(7506, 1000)}.ConvertToNative(jsonValueType) + val, err := Duration{Duration: duration(7506, 1000)}.ConvertToNative(JSONValueType) if err != nil { t.Error(err) } diff --git a/common/types/int.go b/common/types/int.go index 0ac1997b7..60d5a7160 100644 --- a/common/types/int.go +++ b/common/types/int.go @@ -120,7 +120,7 @@ func (i Int) ConvertToNative(typeDesc reflect.Type) (any, error) { case int64WrapperType: // Convert the value to a wrapperspb.Int64Value. return wrapperspb.Int64(int64(i)), nil - case jsonValueType: + case JSONValueType: // The proto-to-JSON conversion rules would convert all 64-bit integer values to JSON // decimal strings. Because CEL ints might come from the automatic widening of 32-bit // values in protos, the JSON type is chosen dynamically based on the value. diff --git a/common/types/int_test.go b/common/types/int_test.go index 6529843c2..560c3dfb2 100644 --- a/common/types/int_test.go +++ b/common/types/int_test.go @@ -159,7 +159,7 @@ func TestIntConvertToNative_Any(t *testing.T) { } func TestIntConvertToNative_Error(t *testing.T) { - val, err := Int(1).ConvertToNative(jsonStructType) + val, err := Int(1).ConvertToNative(JSONStructType) if err == nil { t.Errorf("Got '%v', expected error", val) } @@ -223,7 +223,7 @@ func TestIntConvertToNative_Int64(t *testing.T) { func TestIntConvertToNative_Json(t *testing.T) { // Value can be represented accurately as a JSON number. - val, err := Int(maxIntJSON).ConvertToNative(jsonValueType) + val, err := Int(maxIntJSON).ConvertToNative(JSONValueType) if err != nil { t.Error(err) } else if !proto.Equal(val.(proto.Message), @@ -232,7 +232,7 @@ func TestIntConvertToNative_Json(t *testing.T) { } // Value converts to a JSON decimal string. - val, err = Int(maxIntJSON + 1).ConvertToNative(jsonValueType) + val, err = Int(maxIntJSON + 1).ConvertToNative(JSONValueType) if err != nil { t.Error(err) } else if !proto.Equal(val.(proto.Message), structpb.NewStringValue("9007199254740992")) { diff --git a/common/types/json_list_test.go b/common/types/json_list_test.go index 9b9079c30..0d8cff093 100644 --- a/common/types/json_list_test.go +++ b/common/types/json_list_test.go @@ -35,7 +35,7 @@ func TestJsonListValueAdd(t *testing.T) { structpb.NewNumberValue(2), structpb.NewNumberValue(3)}}) list := listA.Add(listB).(traits.Lister) - nativeVal, err := list.ConvertToNative(jsonListValueType) + nativeVal, err := list.ConvertToNative(JSONListType) if err != nil { t.Error(err) } @@ -50,7 +50,7 @@ func TestJsonListValueAdd(t *testing.T) { } listC := NewStringList(reg, []string{"goodbye", "world"}) list = list.Add(listC).(traits.Lister) - nativeVal, err = list.ConvertToNative(jsonListValueType) + nativeVal, err = list.ConvertToNative(JSONListType) if err != nil { t.Error(err) } @@ -101,7 +101,7 @@ func TestJsonListValueConvertToNative_Json(t *testing.T) { list := NewJSONList(newTestRegistry(t), &structpb.ListValue{Values: []*structpb.Value{ structpb.NewStringValue("hello"), structpb.NewNumberValue(1)}}) - listVal, err := list.ConvertToNative(jsonListValueType) + listVal, err := list.ConvertToNative(JSONListType) if err != nil { t.Error(err) } @@ -109,7 +109,7 @@ func TestJsonListValueConvertToNative_Json(t *testing.T) { t.Error("List did not convert to its underlying representation.") } - val, err := list.ConvertToNative(jsonValueType) + val, err := list.ConvertToNative(JSONValueType) if err != nil { t.Error(err) } diff --git a/common/types/json_struct_test.go b/common/types/json_struct_test.go index 00c9856d8..3dc94bcb2 100644 --- a/common/types/json_struct_test.go +++ b/common/types/json_struct_test.go @@ -41,7 +41,7 @@ func TestJsonStructConvertToNative_Json(t *testing.T) { "first": structpb.NewStringValue("hello"), "second": structpb.NewNumberValue(1)}} mapVal := NewJSONStruct(newTestRegistry(t), structVal) - val, err := mapVal.ConvertToNative(jsonValueType) + val, err := mapVal.ConvertToNative(JSONValueType) if err != nil { t.Error(err) } @@ -50,7 +50,7 @@ func TestJsonStructConvertToNative_Json(t *testing.T) { t.Errorf("Got '%v', expected '%v'", val, structVal) } - strVal, err := mapVal.ConvertToNative(jsonStructType) + strVal, err := mapVal.ConvertToNative(JSONStructType) if err != nil { t.Error(err) } diff --git a/common/types/json_value.go b/common/types/json_value.go index 13a4efe7a..90acfe7df 100644 --- a/common/types/json_value.go +++ b/common/types/json_value.go @@ -22,8 +22,9 @@ import ( // JSON type constants representing the reflected types of protobuf JSON values. var ( - jsonValueType = reflect.TypeOf(&structpb.Value{}) - jsonListValueType = reflect.TypeOf(&structpb.ListValue{}) - jsonStructType = reflect.TypeOf(&structpb.Struct{}) - jsonNullType = reflect.TypeOf(structpb.NullValue_NULL_VALUE) + // JSONValueType describes the protobuf native type for a JSON value. + JSONValueType = reflect.TypeFor[*structpb.Value]() + JSONListType = reflect.TypeFor[*structpb.ListValue]() + JSONStructType = reflect.TypeFor[*structpb.Struct]() + JSONNullType = reflect.TypeFor[structpb.NullValue]() ) diff --git a/common/types/list.go b/common/types/list.go index 8c023f891..324c0f969 100644 --- a/common/types/list.go +++ b/common/types/list.go @@ -153,6 +153,9 @@ func (l *baseList) Contains(elem ref.Val) ref.Val { // ConvertToNative implements the ref.Val interface method. func (l *baseList) ConvertToNative(typeDesc reflect.Type) (any, error) { + if typeDesc == reflect.TypeFor[any]() { + typeDesc = reflect.TypeFor[[]any]() + } // If the underlying list value is assignable to the reflected type return it. if reflect.TypeOf(l.value).AssignableTo(typeDesc) { return l.value, nil @@ -164,19 +167,19 @@ func (l *baseList) ConvertToNative(typeDesc reflect.Type) (any, error) { // Attempt to convert the list to a set of well known protobuf types. switch typeDesc { case anyValueType: - json, err := l.ConvertToNative(jsonListValueType) + json, err := l.ConvertToNative(JSONListType) if err != nil { return nil, err } return anypb.New(json.(proto.Message)) - case jsonValueType, jsonListValueType: + case JSONValueType, JSONListType: jsonValues, err := l.ConvertToNative(reflect.TypeOf([]*structpb.Value{})) if err != nil { return nil, err } jsonList := &structpb.ListValue{Values: jsonValues.([]*structpb.Value)} - if typeDesc == jsonListValueType { + if typeDesc == JSONListType { return jsonList, nil } return structpb.NewListValue(jsonList), nil diff --git a/common/types/list_test.go b/common/types/list_test.go index ba6c498f2..d99cdd004 100644 --- a/common/types/list_test.go +++ b/common/types/list_test.go @@ -127,7 +127,7 @@ func TestBaseListConvertToNative_Any(t *testing.T) { func TestBaseListConvertToNative_Json(t *testing.T) { list := NewDynamicList(newTestRegistry(t), []float64{1.0, 2.0}) - val, err := list.ConvertToNative(jsonListValueType) + val, err := list.ConvertToNative(JSONListType) if err != nil { t.Error(err) } @@ -359,7 +359,7 @@ func TestConcatListConvertToNative_Json(t *testing.T) { listA := NewDynamicList(reg, []float32{1.0, 2.0}) listB := NewDynamicList(reg, []string{"3"}) list := listA.Add(listB) - jsonVal, err := list.ConvertToNative(jsonValueType) + jsonVal, err := list.ConvertToNative(JSONValueType) if err != nil { t.Fatalf("Got error '%v', expected value", err) } @@ -379,7 +379,7 @@ func TestConcatListConvertToNative_Json(t *testing.T) { // Test proto3 to JSON conversion. listC := NewDynamicList(reg, []*dpb.Duration{{Seconds: 100}}) listConcat := listA.Add(listC) - jsonVal, err = listConcat.ConvertToNative(jsonValueType) + jsonVal, err = listConcat.ConvertToNative(JSONValueType) if err != nil { t.Fatal(err) } @@ -607,7 +607,7 @@ func TestStringListConvertToNative_ListInterface(t *testing.T) { func TestStringListConvertToNative_Error(t *testing.T) { reg := newTestRegistry(t) list := NewStringList(reg, []string{"h", "e", "l", "p"}) - _, err := list.ConvertToNative(jsonStructType) + _, err := list.ConvertToNative(JSONStructType) if err == nil { t.Error("Conversion of list to unsupported type did not error.") } @@ -616,7 +616,7 @@ func TestStringListConvertToNative_Error(t *testing.T) { func TestStringListConvertToNative_Json(t *testing.T) { reg := newTestRegistry(t) list := NewStringList(reg, []string{"h", "e", "l", "p"}) - jsonVal, err := list.ConvertToNative(jsonValueType) + jsonVal, err := list.ConvertToNative(JSONValueType) if err != nil { t.Errorf("Got '%v', expected '%v'", err, jsonVal) } @@ -634,7 +634,7 @@ func TestStringListConvertToNative_Json(t *testing.T) { t.Errorf("got json '%v', expected %v", jsonTxt, outList) } - jsonList, err := list.ConvertToNative(jsonListValueType) + jsonList, err := list.ConvertToNative(JSONListType) if err != nil { t.Errorf("Got '%v', expected '%v'", err, jsonList) } @@ -681,7 +681,7 @@ func TestValueListAdd(t *testing.T) { func TestValueListConvertToNative_Json(t *testing.T) { reg := newTestRegistry(t) list := NewRefValList(reg, []ref.Val{String("hello"), String("world")}) - jsonVal, err := list.ConvertToNative(jsonListValueType) + jsonVal, err := list.ConvertToNative(JSONListType) if err != nil { t.Errorf("Got '%v', expected '%v'", err, jsonVal) } diff --git a/common/types/map.go b/common/types/map.go index d162500b7..e4d6f7657 100644 --- a/common/types/map.go +++ b/common/types/map.go @@ -156,6 +156,9 @@ func (m *baseMap) Contains(index ref.Val) ref.Val { func (m *baseMap) ConvertToNative(typeDesc reflect.Type) (any, error) { // If the map is already assignable to the desired type return it, e.g. interfaces and // maps with the same key value types. + if typeDesc == reflect.TypeFor[any]() { + typeDesc = reflect.TypeFor[map[any]any]() + } if reflect.TypeOf(m.value).AssignableTo(typeDesc) { return m.value, nil } @@ -164,19 +167,19 @@ func (m *baseMap) ConvertToNative(typeDesc reflect.Type) (any, error) { } switch typeDesc { case anyValueType: - json, err := m.ConvertToNative(jsonStructType) + json, err := m.ConvertToNative(JSONStructType) if err != nil { return nil, err } return anypb.New(json.(proto.Message)) - case jsonValueType, jsonStructType: + case JSONValueType, JSONStructType: jsonEntries, err := m.ConvertToNative(reflect.TypeOf(map[string]*structpb.Value{})) if err != nil { return nil, err } jsonMap := &structpb.Struct{Fields: jsonEntries.(map[string]*structpb.Value)} - if typeDesc == jsonStructType { + if typeDesc == JSONStructType { return jsonMap, nil } return structpb.NewStructValue(jsonMap), nil @@ -703,12 +706,12 @@ func (m *protoMap) ConvertToNative(typeDesc reflect.Type) (any, error) { // maps with the same key value types. switch typeDesc { case anyValueType: - json, err := m.ConvertToNative(jsonStructType) + json, err := m.ConvertToNative(JSONStructType) if err != nil { return nil, err } return anypb.New(json.(proto.Message)) - case jsonValueType, jsonStructType: + case JSONValueType, JSONStructType: jsonEntries, err := m.ConvertToNative(reflect.TypeOf(map[string]*structpb.Value{})) if err != nil { @@ -716,7 +719,7 @@ func (m *protoMap) ConvertToNative(typeDesc reflect.Type) (any, error) { } jsonMap := &structpb.Struct{ Fields: jsonEntries.(map[string]*structpb.Value)} - if typeDesc == jsonStructType { + if typeDesc == JSONStructType { return jsonMap, nil } return structpb.NewStructValue(jsonMap), nil diff --git a/common/types/map_test.go b/common/types/map_test.go index ff2cc3588..a45a56c85 100644 --- a/common/types/map_test.go +++ b/common/types/map_test.go @@ -151,7 +151,7 @@ func TestDynamicMapConvertToNative_Json(t *testing.T) { reg := newTestRegistry(t) mapVal := NewDynamicMap(reg, map[string]map[string]float32{ "nested": {"1": -1.0}}) - json, err := mapVal.ConvertToNative(jsonValueType) + json, err := mapVal.ConvertToNative(JSONValueType) if err != nil { t.Error(err) } @@ -304,7 +304,7 @@ func TestStringMapConvertToNative(t *testing.T) { if !reflect.DeepEqual(val, mapVal) { t.Errorf("got not-equal, wanted equal for %v == %v", val, mapVal) } - jsonVal, err := mapVal.ConvertToNative(jsonStructType) + jsonVal, err := mapVal.ConvertToNative(JSONStructType) if err != nil { t.Fatalf("mapVal.ConvertToNative(jsonStructType) failed: %v", err) } @@ -919,7 +919,7 @@ func TestProtoMapConvertToNative(t *testing.T) { if mapVal3.Equal(mapVal) != True || mapVal.Equal(mapVal3) != True { t.Errorf("mapVal3.Equal(mapVal) returned false, wanted true") } - convMap, err = mapVal.ConvertToNative(jsonValueType) + convMap, err = mapVal.ConvertToNative(JSONValueType) if err != nil { t.Fatalf("mapVal.ConvertToNative() failed: %v", err) } @@ -944,7 +944,7 @@ func TestProtoMapConvertToNative(t *testing.T) { if mapVal6.Equal(mapVal) != True || mapVal.Equal(mapVal6) != True { t.Errorf("mapVal6.Equal(mapVal) returned false, wanted true") } - _, err = mapVal.ConvertToNative(jsonListValueType) + _, err = mapVal.ConvertToNative(JSONListType) if err == nil { t.Fatalf("mapVal.ConvertToNative() succeeded for invalid type") } diff --git a/common/types/null.go b/common/types/null.go index 2c0297fe6..671e1ee5c 100644 --- a/common/types/null.go +++ b/common/types/null.go @@ -45,7 +45,7 @@ func (n Null) ConvertToNative(typeDesc reflect.Type) (any, error) { switch typeDesc.Kind() { case reflect.Int32: switch typeDesc { - case jsonNullType: + case JSONNullType: return structpb.NullValue_NULL_VALUE, nil case nullReflectType: return n, nil @@ -55,18 +55,18 @@ func (n Null) ConvertToNative(typeDesc reflect.Type) (any, error) { case anyValueType: // Convert to a JSON-null before packing to an Any field since the enum value for JSON // null cannot be packed directly. - pb, err := n.ConvertToNative(jsonValueType) + pb, err := n.ConvertToNative(JSONValueType) if err != nil { return nil, err } return anypb.New(pb.(proto.Message)) - case jsonValueType: + case JSONValueType: return structpb.NewNullValue(), nil case boolWrapperType, byteWrapperType, doubleWrapperType, floatWrapperType, int32WrapperType, int64WrapperType, stringWrapperType, uint32WrapperType, uint64WrapperType, durationValueType, timestampValueType, protoIfaceType: return nil, nil - case jsonListValueType, jsonStructType: + case JSONListType, JSONStructType: // skip handling default: if typeDesc.Implements(protoIfaceType) { diff --git a/common/types/null_test.go b/common/types/null_test.go index 782f8ff36..97ec24c10 100644 --- a/common/types/null_test.go +++ b/common/types/null_test.go @@ -35,11 +35,11 @@ func TestNullConvertToNative(t *testing.T) { err error }{ { - goType: jsonValueType, + goType: JSONValueType, out: structpb.NewNullValue(), }, { - goType: jsonNullType, + goType: JSONNullType, out: structpb.NullValue_NULL_VALUE, }, { @@ -69,11 +69,11 @@ func TestNullConvertToNative(t *testing.T) { err: errors.New("type conversion error from 'null_type' to 'int'"), }, { - goType: jsonListValueType, + goType: JSONListType, err: errors.New("type conversion error from 'null_type' to '*structpb.ListValue'"), }, { - goType: jsonStructType, + goType: JSONStructType, err: errors.New("type conversion error from 'null_type' to '*structpb.Struct'"), }, } diff --git a/common/types/object.go b/common/types/object.go index 776f6954a..c44eaa942 100644 --- a/common/types/object.go +++ b/common/types/object.go @@ -71,7 +71,7 @@ func (o *protoObj) ConvertToNative(typeDesc reflect.Type) (any, error) { return srcPB, nil } return anypb.New(srcPB) - case jsonValueType: + case JSONValueType: // Marshal the proto to JSON first, and then rehydrate as protobuf.Value as there is no // support for direct conversion from proto.Message to protobuf.Value. bytes, err := protojson.Marshal(srcPB) diff --git a/common/types/object_test.go b/common/types/object_test.go index 14171a90c..10d05333e 100644 --- a/common/types/object_test.go +++ b/common/types/object_test.go @@ -97,9 +97,9 @@ func TestProtoObjectConvertToNative(t *testing.T) { } // JSON - jsonVal, err := objVal.ConvertToNative(jsonValueType) + jsonVal, err := objVal.ConvertToNative(JSONValueType) if err != nil { - t.Fatalf("objVal.ConvertToNative(%v) failed: %v", jsonValueType, err) + t.Fatalf("objVal.ConvertToNative(%v) failed: %v", JSONValueType, err) } jsonBytes, err := protojson.Marshal(jsonVal.(proto.Message)) jsonTxt := string(jsonBytes) diff --git a/common/types/string.go b/common/types/string.go index 8aad4701c..5f5a43358 100644 --- a/common/types/string.go +++ b/common/types/string.go @@ -72,7 +72,7 @@ func (s String) ConvertToNative(typeDesc reflect.Type) (any, error) { case anyValueType: // Primitives must be wrapped before being set on an Any field. return anypb.New(wrapperspb.String(string(s))) - case jsonValueType: + case JSONValueType: // Convert to a protobuf representation of a JSON String. return structpb.NewStringValue(string(s)), nil case stringWrapperType: diff --git a/common/types/string_test.go b/common/types/string_test.go index 37958535f..158f2bb74 100644 --- a/common/types/string_test.go +++ b/common/types/string_test.go @@ -78,7 +78,7 @@ func TestStringConvertToNative_Error(t *testing.T) { } func TestStringConvertToNative_Json(t *testing.T) { - val, err := String("hello").ConvertToNative(jsonValueType) + val, err := String("hello").ConvertToNative(JSONValueType) pbVal := structpb.NewStringValue("hello") if err != nil { t.Error(err) diff --git a/common/types/timestamp.go b/common/types/timestamp.go index f7be58591..060caf6bb 100644 --- a/common/types/timestamp.go +++ b/common/types/timestamp.go @@ -91,7 +91,7 @@ func (t Timestamp) ConvertToNative(typeDesc reflect.Type) (any, error) { case anyValueType: // Pack the underlying time as a tpb.Timestamp into an Any value. return anypb.New(tpb.New(t.Time)) - case jsonValueType: + case JSONValueType: // CEL follows the proto3 to JSON conversion which formats as an RFC 3339 encoded JSON // string. v := t.ConvertToType(StringType) diff --git a/common/types/timestamp_test.go b/common/types/timestamp_test.go index cd7d996f4..a7ff89d4c 100644 --- a/common/types/timestamp_test.go +++ b/common/types/timestamp_test.go @@ -277,7 +277,7 @@ func TestTimestampConvertToNative(t *testing.T) { if !proto.Equal(val.(proto.Message), want.(proto.Message)) { t.Errorf("Got '%v', expected '%v'", val, want) } - val, err = ts.ConvertToNative(jsonValueType) + val, err = ts.ConvertToNative(JSONValueType) if err != nil { t.Error(err) } diff --git a/common/types/uint.go b/common/types/uint.go index a93405a13..91d5369da 100644 --- a/common/types/uint.go +++ b/common/types/uint.go @@ -100,7 +100,7 @@ func (i Uint) ConvertToNative(typeDesc reflect.Type) (any, error) { case anyValueType: // Primitives must be wrapped before being set on an Any field. return anypb.New(wrapperspb.UInt64(uint64(i))) - case jsonValueType: + case JSONValueType: // JSON can accurately represent 32-bit uints as floating point values. if i.isJSONSafe() { return structpb.NewNumberValue(float64(i)), nil diff --git a/common/types/uint_test.go b/common/types/uint_test.go index f07832cac..2484fbf36 100644 --- a/common/types/uint_test.go +++ b/common/types/uint_test.go @@ -155,7 +155,7 @@ func TestUintConvertToNative_Error(t *testing.T) { func TestUintConvertToNative_Json(t *testing.T) { // Value can be represented accurately as a JSON number. - val, err := Uint(maxIntJSON).ConvertToNative(jsonValueType) + val, err := Uint(maxIntJSON).ConvertToNative(JSONValueType) if err != nil { t.Error(err) } else if !proto.Equal(val.(proto.Message), @@ -164,7 +164,7 @@ func TestUintConvertToNative_Json(t *testing.T) { } // Value converts to a JSON decimal string - val, err = Uint(maxIntJSON + 1).ConvertToNative(jsonValueType) + val, err = Uint(maxIntJSON + 1).ConvertToNative(JSONValueType) if err != nil { t.Error(err) } else if !proto.Equal(val.(proto.Message), structpb.NewStringValue("9007199254740992")) {