Skip to content

Commit

Permalink
Nullable args (#292)
Browse files Browse the repository at this point in the history
* feature: Argument values can be null be default. Use RejectNull to change.

* feature: Argument values can be null be default. Use RejectNull to change.
  • Loading branch information
jamesread committed Apr 26, 2024
1 parent 12cf001 commit 5004193
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ActionArgument struct {
Default string
Choices []ActionArgumentChoice
Entity string
RejectNull bool
Suggestions map[string]string
}

Expand Down
12 changes: 12 additions & 0 deletions internal/executor/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,25 @@ func typecheckActionArgument(name string, value string, action *config.Action) e
return errors.New("Action arg not defined: " + name)
}

if value == "" {
return typecheckNull(arg)
}

if len(arg.Choices) > 0 {
return typecheckChoice(value, arg)
}

return TypeSafetyCheck(name, value, arg.Type)
}

func typecheckNull(arg *config.ActionArgument) error {
if arg.RejectNull {
return errors.New("Null values are not allowed")
}

return nil
}

func typecheckChoice(value string, arg *config.ActionArgument) error {
if arg.Entity != "" {
return typecheckChoiceEntity(value, arg)
Expand Down
28 changes: 28 additions & 0 deletions internal/executor/arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ func TestSanitizeUnimplemented(t *testing.T) {
assert.NotNil(t, err, "Test an argument type that does not exist")
}

func TestArgumentValueNullable(t *testing.T) {
a1 := config.Action{
Title: "Release the hounds",
Shell: "echo 'Releasing {{ count }} hounds'",
Arguments: []config.ActionArgument{
{
Name: "count",
Type: "int",
},
},
}

values := map[string]string{
"count": "",
}

out, err := parseActionArguments(a1.Shell, values, &a1, a1.Title, "")

assert.Equal(t, "echo 'Releasing hounds'", out)
assert.Nil(t, err)

a1.Arguments[0].RejectNull = true

_, err = parseActionArguments(a1.Shell, values, &a1, a1.Title, "")

assert.NotNil(t, err)
}

func TestArgumentNameNumbers(t *testing.T) {
a1 := config.Action{
Title: "Do some tickles",
Expand Down

0 comments on commit 5004193

Please sign in to comment.