Skip to content

Commit

Permalink
Expose optional types as an option (google#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones committed Apr 12, 2023
1 parent 51db1d1 commit 3d2e878
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion repl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ func newExtensionOption(extType string) (*extensionOption, error) {
var extOption cel.EnvOption
extType = strings.ToLower(extType)
switch op := extType; op {
case "optional":
extOption = cel.OptionalTypes()
case "strings":
extOption = ext.Strings()
case "protos":
Expand Down Expand Up @@ -774,7 +776,7 @@ func (e *Evaluator) loadExtensionOption(idx int, args []string) error {
argExtType := args[idx]
if argExtType == "all" {
// Load all extension types as a convenience
var extensionTypes = []string{"strings", "protos", "math", "encoders"}
var extensionTypes = []string{"optional", "strings", "protos", "math", "encoders"}
for _, val := range extensionTypes {
err := e.loadExtensionOptionType(val)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions repl/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,24 @@ func TestProcess(t *testing.T) {
wantExit: false,
wantError: false,
},
{
name: "OptionExtensionOptional",
commands: []Cmder{
&simpleCmd{
cmd: "option",
args: []string{
"--extension",
"optional",
},
},
&evalCmd{
expr: "optional.none().orValue('default')",
},
},
wantText: "default : string",
wantExit: false,
wantError: false,
},
{
name: "OptionExtensionStrings",
commands: []Cmder{
Expand Down
2 changes: 1 addition & 1 deletion repl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df
github.com/chzyer/readline v1.5.1
github.com/google/cel-go v0.13.0
github.com/google/cel-go v0.14.0
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
google.golang.org/protobuf v1.29.1
)
Expand Down

0 comments on commit 3d2e878

Please sign in to comment.