Skip to content

Commit

Permalink
feat: add new coll methods
Browse files Browse the repository at this point in the history
* upgraded existing functions to support more types

* added tests
* exposed new sort method

* fix: lint error
  • Loading branch information
adityathebe authored Jun 24, 2024
1 parent 98bfa1f commit ba34fc0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 39 deletions.
1 change: 1 addition & 0 deletions funcs/cel_exports.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 28 additions & 29 deletions funcs/coll_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions kubernetes/quantity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
)

func _k8sCPUAsMillicores(objVal string) int64 {
var cpu int64
if strings.HasSuffix(objVal, "m") {
cpu = conv.ToInt64(strings.ReplaceAll(objVal, "m", ""))
} else if strings.HasSuffix(objVal, "n") {
cpu = conv.ToInt64(strings.ReplaceAll(objVal, "n", "")) / (1000 * 1000)
} else {
cpu = int64(conv.ToFloat64(objVal) * 1000)
return conv.ToInt64(strings.ReplaceAll(objVal, "m", ""))
}
return cpu

if strings.HasSuffix(objVal, "n") {
return conv.ToInt64(strings.ReplaceAll(objVal, "n", "")) / (1000 * 1000)
}

return int64(conv.ToFloat64(objVal) * 1000)
}

func k8sCPUAsMillicores() cel.EnvOption {
Expand Down
39 changes: 36 additions & 3 deletions tests/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,16 @@ func TestCelJQ(t *testing.T) {
func TestCelLists(t *testing.T) {
runTests(t, []Test{
{nil, "['a','b', 'c'].join(',')", "a,b,c"},

{nil, "['a', ['b','c'], 'd'].flatten().join(',')", "a,b,c,d"},
{nil, "['b', 'a', 'c'].sort().join(',')", "a,b,c"},
{nil, "['b', 'a', 'c'].sort().join(',')", "a,b,c"},
{nil, "['a', 'a', 'b', 'c'].uniq().join(',')", "a,b,c"},
{nil, "{'a': 1, 'b': 2}.keys().join(',')", "a,b"},
{nil, "{'a': 1, 'b': 2}.values().sum()", "3"},

{nil, "[1,2,3,3,4].uniq().sum()", "10"},
{nil, "['a', 'a', 'b', 'c'].uniq().join(',')", "a,b,c"},

{nil, "['h', 'b', 'a'].sort()", "[a b h]"},
{nil, "[3,2,1].sort()", "[1 2 3]"},
})
}

Expand Down Expand Up @@ -276,7 +279,9 @@ func TestCelMaps(t *testing.T) {
},
},
}

runTests(t, []Test{
{nil, "{'a': 1, 'b': 2}.values().sum()", "3"},
{m, "x.a", "b"},
{m, "x.c", "1"},
{m, "x.d", "true"},
Expand All @@ -286,12 +291,40 @@ func TestCelMaps(t *testing.T) {
{nil, "{'a': 'c'}.merge({'b': 'd'}).keys().join(',')", "a,b"},
{nil, "{'a': '1', 'b': '2', 'c': '3'}.pick(['a', 'c']).keys()", "[a c]"},
{nil, "{'a': '1', 'b': '2', 'c': '3'}.omit(['b']).keys()", "[a c]"},
{nil, `{"first": "John", "last": "Doe"}.omit(["first"]).keys()`, "[last]"},
{map[string]interface{}{"x": map[string]string{
"a": "1",
"b": "2",
"c": "3",
}}, "x.pick(['a', 'c']).keys()", "[a c]"},
})

runTests(t, []Test{
{
map[string]any{
"a": map[string]any{"first": "John"},
"b": map[string]any{"last": "Doe"},
},
`a.merge(b).last`,
"Doe",
},
{
map[string]any{
"a": map[string]any{"first": 1},
"b": map[string]any{"last": 2},
},
`a.merge(b).last`,
"2",
},
{
map[string]any{
"a": map[string]any{"first": 1},
"b": map[string]any{"first": 2},
},
`a.merge(b).first`,
"2",
},
})
}

func TestCelRandom(t *testing.T) {
Expand Down

0 comments on commit ba34fc0

Please sign in to comment.