-
Notifications
You must be signed in to change notification settings - Fork 337
Add json.marshal and json.unmarshal cel functions #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Copyright 2025 The Kubernetes Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package library | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "math" | ||
| "reflect" | ||
|
|
||
| "github.com/google/cel-go/cel" | ||
| "github.com/google/cel-go/common/types" | ||
| "github.com/google/cel-go/common/types/ref" | ||
|
|
||
| "github.com/kubernetes-sigs/kro/pkg/cel/conversion" | ||
| ) | ||
|
|
||
| // JSON returns a CEL library that provides JSON parsing functions. | ||
| // | ||
| // Library functions: | ||
| // | ||
| // json.unmarshal() parses a JSON string and returns the parsed value. | ||
| // | ||
| // The function takes one argument: | ||
| // - jsonString: a string containing JSON | ||
| // | ||
| // Returns a dynamic type that can be: | ||
| // - map for JSON objects | ||
| // - list for JSON arrays | ||
| // - string, int, double, bool for JSON primitives | ||
| // - null for JSON null | ||
| // | ||
| // Example usage: | ||
| // | ||
| // json.unmarshal('{"name": "test"}').name | ||
| // json.unmarshal('[1,2,3]')[0] | ||
| // json.unmarshal(schema.spec.config).enabled | ||
| // | ||
| // json.marshal() converts a CEL value to a JSON string. | ||
| // | ||
| // The function takes one argument: | ||
| // - value: any CEL value (map, list, string, number, bool, null) | ||
| // | ||
| // Returns a JSON string representation of the value. | ||
| // | ||
| // Example usage: | ||
| // | ||
| // json.marshal({"name": "test", "count": 42}) | ||
| // json.marshal([1, 2, 3]) | ||
| // json.marshal(schema.spec.config) | ||
| func JSON(options ...JSONOption) cel.EnvOption { | ||
| lib := &jsonLibrary{version: math.MaxUint32} | ||
| for _, o := range options { | ||
| lib = o(lib) | ||
| } | ||
| return cel.Lib(lib) | ||
| } | ||
|
|
||
| type jsonLibrary struct { | ||
| version uint32 | ||
| } | ||
|
|
||
| // JSONOption is a functional option for configuring the json library. | ||
| type JSONOption func(*jsonLibrary) *jsonLibrary | ||
|
|
||
| // JSONVersion configures the version of the json library. | ||
| func JSONVersion(version uint32) JSONOption { | ||
| return func(lib *jsonLibrary) *jsonLibrary { | ||
| lib.version = version | ||
| return lib | ||
| } | ||
| } | ||
|
|
||
| func (l *jsonLibrary) LibraryName() string { | ||
| return "json" | ||
| } | ||
|
|
||
| func (l *jsonLibrary) CompileOptions() []cel.EnvOption { | ||
| return []cel.EnvOption{ | ||
| cel.Function("json.unmarshal", | ||
| cel.Overload("json.unmarshal_string", | ||
| []*cel.Type{cel.StringType}, | ||
| cel.DynType, | ||
| cel.UnaryBinding(unmarshalJSON), | ||
| ), | ||
| ), | ||
| cel.Function("json.marshal", | ||
| cel.Overload("json.marshal_dyn", | ||
| []*cel.Type{cel.DynType}, | ||
| cel.StringType, | ||
| cel.UnaryBinding(marshalJSON), | ||
| ), | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| func (l *jsonLibrary) ProgramOptions() []cel.ProgramOption { | ||
| return nil | ||
| } | ||
|
|
||
| func unmarshalJSON(jsonString ref.Val) ref.Val { | ||
| native, err := jsonString.ConvertToNative(reflect.TypeOf("")) | ||
| if err != nil { | ||
| return types.NewErr("json.unmarshal argument must be a string") | ||
| } | ||
|
|
||
| str, ok := native.(string) | ||
| if !ok { | ||
| return types.NewErr("json.unmarshal argument must be a string") | ||
| } | ||
|
|
||
| var result interface{} | ||
| if err := json.Unmarshal([]byte(str), &result); err != nil { | ||
| return types.NewErr("json.unmarshal failed to parse JSON: %s", err.Error()) | ||
| } | ||
|
|
||
| return types.DefaultTypeAdapter.NativeToValue(result) | ||
| } | ||
|
|
||
| func marshalJSON(value ref.Val) ref.Val { | ||
| native, err := conversion.GoNativeType(value) | ||
| if err != nil { | ||
| return types.NewErr("json.marshal failed to convert value: %s", err.Error()) | ||
| } | ||
|
|
||
| jsonBytes, err := json.Marshal(native) | ||
| if err != nil { | ||
| return types.NewErr("json.marshal failed to encode value: %s", err.Error()) | ||
| } | ||
|
|
||
| return types.String(string(jsonBytes)) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be versioned internally to v1alpha1
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you clarify what you mean for versioned internally here?
I see some other libraries have integer versioning like this but not seeing any use a string version like
v1alpha1. The random library also doesn't seem to have any versioning currently.https://github.com/google/cel-go/blob/v0.27.0/ext/lists.go#L213
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mabye take a look at https://github.com/google/cel-go/blob/v0.27.0/ext/lists.go#L175-L189 or https://github.com/google/cel-go/blob/v0.27.0/ext/sets.go#L88-L97. Its basically about introducing a version so we can control it later. i dont really mind if its v1alpha1 or an integer tbh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added support for setting a version
currently the version integer is unused but can be referenced for future updates to the library