Skip to content

Commit 7ee0d52

Browse files
yashmehrotramoshloop
authored andcommitted
chore: use toJSON instead of marshalJSON
1 parent 1f4a810 commit 7ee0d52

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

celext/cel_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ func TestCelJSON(t *testing.T) {
119119
Input string
120120
Output any
121121
}{
122-
{Input: `dyn([{'name': 'John', 'age': 30}]).marshalJSON()`, Output: `[{"age":30,"name":"John"}]`},
123-
{Input: `[{'name': 'John'}].marshalJSON()`, Output: `[{"name":"John"}]`},
124-
{Input: `dyn({'name': 'John'}).marshalJSON()`, Output: `{"name":"John"}`},
125-
{Input: `{'name': 'John'}.marshalJSON()`, Output: `{"name":"John"}`},
122+
{Input: `dyn([{'name': 'John', 'age': 30}]).toJSON()`, Output: `[{"age":30,"name":"John"}]`},
123+
{Input: `[{'name': 'John'}].toJSON()`, Output: `[{"name":"John"}]`},
124+
{Input: `dyn({'name': 'John'}).toJSON()`, Output: `{"name":"John"}`},
125+
{Input: `{'name': 'John'}.toJSON()`, Output: `{"name":"John"}`},
126126
}
127127

128128
for i, td := range testData {

celext/celfuncs.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ import (
1212
)
1313

1414
var (
15-
listType = reflect.TypeOf(&structpb.ListValue{})
16-
mapType = reflect.TypeOf(&structpb.Struct{})
17-
mapStrDyn = cel.MapType(cel.StringType, cel.DynType)
18-
listStrDyn = cel.ListType(cel.DynType)
15+
listType = reflect.TypeOf(&structpb.ListValue{})
16+
mapType = reflect.TypeOf(&structpb.Struct{})
17+
mapStrDyn = cel.MapType(cel.StringType, cel.DynType)
1918
)
2019

2120
var customCelFuncs = []cel.EnvOption{
2221
k8sHealth(),
2322
k8sIsHealthy(),
2423
k8sCPUAsMillicores(),
2524
k8sMemoryAsBytes(),
26-
marshalJSON(),
25+
toJSON(),
2726
parseJSON(),
2827
}
2928

celext/json.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// Reference: https://github.com/tektoncd/triggers/blob/main/pkg/interceptors/cel/triggers.go
1313

14-
func marshalJSON() cel.EnvOption {
14+
func toJSON() cel.EnvOption {
1515
valToJSONString := func(val ref.Val) ref.Val {
1616
var typeDesc reflect.Type
1717

@@ -21,7 +21,7 @@ func marshalJSON() cel.EnvOption {
2121
case types.ListType:
2222
typeDesc = listType
2323
default:
24-
return types.ValOrErr(val, "unexpected type:%v passed to marshalJSON", val.Type())
24+
return types.ValOrErr(val, "unexpected type:%v passed to toJSON", val.Type())
2525
}
2626

2727
nativeVal, err := val.ConvertToNative(typeDesc)
@@ -37,10 +37,8 @@ func marshalJSON() cel.EnvOption {
3737
return types.String(marshaledVal)
3838
}
3939

40-
return cel.Function("marshalJSON",
41-
cel.MemberOverload("marshalJSON_map", []*cel.Type{mapStrDyn}, cel.StringType,
42-
cel.UnaryBinding(valToJSONString)),
43-
cel.MemberOverload("marshalJSON_list", []*cel.Type{listStrDyn}, cel.StringType,
40+
return cel.Function("toJSON",
41+
cel.MemberOverload("toJSON_dyn", []*cel.Type{cel.DynType}, cel.StringType,
4442
cel.UnaryBinding(valToJSONString)),
4543
)
4644
}

0 commit comments

Comments
 (0)