Nullable args (#292)

* feature: Argument values can be null be default. Use RejectNull to change.

* feature: Argument values can be null be default. Use RejectNull to change.
This commit is contained in:
James Read 2024-04-26 18:36:33 +01:00 committed by GitHub
parent 12cf0013e2
commit 500419307b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 0 deletions

View File

@ -33,6 +33,7 @@ type ActionArgument struct {
Default string Default string
Choices []ActionArgumentChoice Choices []ActionArgumentChoice
Entity string Entity string
RejectNull bool
Suggestions map[string]string Suggestions map[string]string
} }

View File

@ -71,6 +71,10 @@ func typecheckActionArgument(name string, value string, action *config.Action) e
return errors.New("Action arg not defined: " + name) return errors.New("Action arg not defined: " + name)
} }
if value == "" {
return typecheckNull(arg)
}
if len(arg.Choices) > 0 { if len(arg.Choices) > 0 {
return typecheckChoice(value, arg) return typecheckChoice(value, arg)
} }
@ -78,6 +82,14 @@ func typecheckActionArgument(name string, value string, action *config.Action) e
return TypeSafetyCheck(name, value, arg.Type) 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 { func typecheckChoice(value string, arg *config.ActionArgument) error {
if arg.Entity != "" { if arg.Entity != "" {
return typecheckChoiceEntity(value, arg) return typecheckChoiceEntity(value, arg)

View File

@ -17,6 +17,34 @@ func TestSanitizeUnimplemented(t *testing.T) {
assert.NotNil(t, err, "Test an argument type that does not exist") 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) { func TestArgumentNameNumbers(t *testing.T) {
a1 := config.Action{ a1 := config.Action{
Title: "Do some tickles", Title: "Do some tickles",