-
Notifications
You must be signed in to change notification settings - Fork 249
Expose extension option factory as a public method #1141
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
15 commits
Select commit
Hold shift + click to select a range
42eb0b4
Expose public methods to convert function and variable decl to v1 Decl
aakash070 df54904
Merge branch 'google:master' into master
aakash070 ecacf4a
Break up restricted destinations test config file into base_config an…
aakash070 ffb7d88
Merge branch 'google:master' into master
aakash070 9fc3fe1
Add k8s custom policy tag handler for test
aakash070 7152622
revert go.mod changes
aakash070 c13d213
added copyright and removed redundant attribute from go_library target
aakash070 996f7a5
Merge branch 'google:master' into master
aakash070 7f12940
Merge branch 'google:master' into master
aakash070 1078a19
Merge branch 'google:master' into master
aakash070 3687a4f
Merge branch 'google:master' into master
aakash070 17987e9
Merge branch 'google:master' into master
aakash070 c188874
refactor code to extract common methods
aakash070 2faf7cb
reverted redundant code
aakash070 7dc4425
added tests for extension option factory
aakash070 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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 ext | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/google/cel-go/cel" | ||
| "github.com/google/cel-go/common/env" | ||
| ) | ||
|
|
||
| // ExtensionOptionFactory converts an ExtensionConfig value to a CEL environment option. | ||
| func ExtensionOptionFactory(configElement any) (cel.EnvOption, bool) { | ||
| ext, isExtension := configElement.(*env.Extension) | ||
| if !isExtension { | ||
| return nil, false | ||
| } | ||
| fac, found := extFactories[ext.Name] | ||
| if !found { | ||
| return nil, false | ||
| } | ||
| // If the version is 'latest', set the version value to the max uint. | ||
| ver, err := ext.VersionNumber() | ||
| if err != nil { | ||
| return func(*cel.Env) (*cel.Env, error) { | ||
| return nil, fmt.Errorf("invalid extension version: %s - %s", ext.Name, ext.Version) | ||
| }, true | ||
| } | ||
| return fac(ver), true | ||
| } | ||
|
|
||
| // extensionFactory accepts a version and produces a CEL environment associated with the versioned extension. | ||
| type extensionFactory func(uint32) cel.EnvOption | ||
|
|
||
| var extFactories = map[string]extensionFactory{ | ||
| "bindings": func(version uint32) cel.EnvOption { | ||
| return Bindings(BindingsVersion(version)) | ||
| }, | ||
| "encoders": func(version uint32) cel.EnvOption { | ||
| return Encoders(EncodersVersion(version)) | ||
| }, | ||
| "lists": func(version uint32) cel.EnvOption { | ||
| return Lists(ListsVersion(version)) | ||
| }, | ||
| "math": func(version uint32) cel.EnvOption { | ||
| return Math(MathVersion(version)) | ||
| }, | ||
| "protos": func(version uint32) cel.EnvOption { | ||
| return Protos(ProtosVersion(version)) | ||
| }, | ||
| "sets": func(version uint32) cel.EnvOption { | ||
| return Sets(SetsVersion(version)) | ||
| }, | ||
| "strings": func(version uint32) cel.EnvOption { | ||
| return Strings(StringsVersion(version)) | ||
| }, | ||
| "two-var-comprehensions": func(version uint32) cel.EnvOption { | ||
| return TwoVarComprehensions(TwoVarComprehensionsVersion(version)) | ||
| }, | ||
| } | ||
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,67 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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 ext | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/google/cel-go/cel" | ||
| "github.com/google/cel-go/common/env" | ||
| ) | ||
|
|
||
| func TestExtensionOptionFactoryInvalidExtension(t *testing.T) { | ||
| invalidExtension := "invalid extension" | ||
| _, validExtension := ExtensionOptionFactory(invalidExtension) | ||
| if validExtension { | ||
| t.Fatalf("ExtensionOptionFactory(%s) returned valid extension for invalid input", invalidExtension) | ||
| } | ||
| } | ||
|
|
||
| func TestExtensionOptionFactoryInvalidExtensionName(t *testing.T) { | ||
| e := &env.Extension{Name: "invalid extension name"} | ||
| _, validExtension := ExtensionOptionFactory(e) | ||
| if validExtension { | ||
| t.Fatalf("ExtensionOptionFactory(%s) returned valid extension for invalid extension name", e.Name) | ||
| } | ||
| } | ||
|
|
||
| func TestExtensionOptionFactoryInvalidExtensionVersion(t *testing.T) { | ||
| e := &env.Extension{Name: "bindings", Version: "invalid version"} | ||
| opt, validExtension := ExtensionOptionFactory(e) | ||
| if !validExtension { | ||
| t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) | ||
| } | ||
| _, err := cel.NewCustomEnv(opt) | ||
| if err == nil || err.Error() != fmt.Sprintf("invalid extension version: %s - %s", e.Name, e.Version) { | ||
| t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension version", e.Name) | ||
| } | ||
| } | ||
|
|
||
| func TestExtensionOptionFactoryValidBindingsExtension(t *testing.T) { | ||
| e := &env.Extension{Name: "bindings", Version: "latest"} | ||
| opt, validExtension := ExtensionOptionFactory(e) | ||
| if !validExtension { | ||
| t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) | ||
| } | ||
| en, err := cel.NewCustomEnv(opt) | ||
| if err != nil { | ||
| t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) | ||
| } | ||
| cfg, err := en.ToConfig("test config") | ||
| if len(cfg.Extensions) != 1 || cfg.Extensions[0].Name != "cel.lib.ext.cel.bindings" || cfg.Extensions[0].Version != "latest" { | ||
| t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.