Debian Codegraph @

ext/extension_option_factory.go

raw symbols SCIP
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package ext
16
17 import (
18 "fmt"
19
20 "github.com/google/cel-go/cel"
21 "github.com/google/cel-go/common/env"
22 )
23
24 // ExtensionOptionFactory converts an ExtensionConfig value to a CEL environment option.
25 func ExtensionOptionFactory(configElement any) (cel.EnvOption, bool) {
26 ext, isExtension := configElement.(*env.Extension)
27 if !isExtension {
28 return nil, false
29 }
30 name := ext.Name
31 alias, found := extAliases[name]
32 if found {
33 name = alias
34 }
35 fac, found := extFactories[name]
36 if !found {
37 return nil, false
38 }
39 // If the version is 'latest', set the version value to the max uint.
40 ver, err := ext.VersionNumber()
41 if err != nil {
42 return func(*cel.Env) (*cel.Env, error) {
43 return nil, fmt.Errorf("invalid extension version: %s - %s", ext.Name, ext.Version)
44 }, true
45 }
46 return fac(ver), true
47 }
48
49 // extensionFactory accepts a version and produces a CEL environment associated with the versioned extension.
50 type extensionFactory func(uint32) cel.EnvOption
51
52 var extFactories = map[string]extensionFactory{
53 "cel.lib.ext.cel.bindings": func(version uint32) cel.EnvOption {
54 return Bindings(BindingsVersion(version))
55 },
56 "cel.lib.ext.encoders": func(version uint32) cel.EnvOption {
57 return Encoders(EncodersVersion(version))
58 },
59 "cel.lib.ext.lists": func(version uint32) cel.EnvOption {
60 return Lists(ListsVersion(version))
61 },
62 "cel.lib.ext.math": func(version uint32) cel.EnvOption {
63 return Math(MathVersion(version))
64 },
65 "cel.lib.ext.protos": func(version uint32) cel.EnvOption {
66 return Protos(ProtosVersion(version))
67 },
68 "cel.lib.ext.sets": func(version uint32) cel.EnvOption {
69 return Sets(SetsVersion(version))
70 },
71 "cel.lib.ext.strings": func(version uint32) cel.EnvOption {
72 return Strings(StringsVersion(version))
73 },
74 "cel.lib.ext.comprev2": func(version uint32) cel.EnvOption {
75 return TwoVarComprehensions(TwoVarComprehensionsVersion(version))
76 },
77 "cel.lib.ext.regex": func(version uint32) cel.EnvOption {
78 return Regex(RegexVersion(version))
79 },
80 }
81
82 var extAliases = map[string]string{
83 "bindings": "cel.lib.ext.cel.bindings",
84 "encoders": "cel.lib.ext.encoders",
85 "lists": "cel.lib.ext.lists",
86 "math": "cel.lib.ext.math",
87 "protos": "cel.lib.ext.protos",
88 "sets": "cel.lib.ext.sets",
89 "strings": "cel.lib.ext.strings",
90 "two-var-comprehensions": "cel.lib.ext.comprev2",
91 "regex": "cel.lib.ext.regex",
92 }