Skip to content

Commit 1a2e2a0

Browse files
authored
Merge pull request #951 from shabbskagalwala/gonative-byte-conversion
feat: add bytes conversion to go native types
2 parents dcfa07b + a9c1caf commit 1a2e2a0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/cel/conversions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func GoNativeType(v ref.Val) (interface{}, error) {
4343
return v.Value().(float64), nil
4444
case types.StringType:
4545
return v.Value().(string), nil
46+
case types.BytesType:
47+
return v.Value().([]byte), nil
4648
case types.ListType:
4749
return convertList(v)
4850
case types.MapType:

pkg/cel/conversions_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,30 @@ func TestGoNativeType_ComplexNested(t *testing.T) {
8383
_, err = json.Marshal(native)
8484
assert.NoError(t, err, "Should be JSON marshallable")
8585
}
86+
87+
func TestGoNativeType_Bytes(t *testing.T) {
88+
env, err := cel.NewEnv()
89+
require.NoError(t, err)
90+
91+
ast, issues := env.Compile(`b"hello world"`)
92+
require.NoError(t, issues.Err())
93+
94+
prog, err := env.Program(ast)
95+
require.NoError(t, err)
96+
97+
val, _, err := prog.Eval(map[string]interface{}{})
98+
require.NoError(t, err)
99+
100+
native, err := GoNativeType(val)
101+
require.NoError(t, err)
102+
103+
// Check type
104+
bytes, ok := native.([]byte)
105+
require.True(t, ok, "Expected []byte, got %T", native)
106+
assert.Equal(t, []byte("hello world"), bytes)
107+
108+
// Check JSON marshalling
109+
marshalled, err := json.Marshal(native)
110+
assert.NoError(t, err, "Should be JSON marshallable")
111+
assert.NotEmpty(t, marshalled)
112+
}

0 commit comments

Comments
 (0)