| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |