Skip to content
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

Nullable args #292

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading