From 500419307bee8b79f4aa45b930f0854a17a624ac Mon Sep 17 00:00:00 2001 From: James Read Date: Fri, 26 Apr 2024 18:36:33 +0100 Subject: [PATCH] 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. --- internal/config/config.go | 1 + internal/executor/arguments.go | 12 ++++++++++++ internal/executor/arguments_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index 37b00d7..24eb8a9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -33,6 +33,7 @@ type ActionArgument struct { Default string Choices []ActionArgumentChoice Entity string + RejectNull bool Suggestions map[string]string } diff --git a/internal/executor/arguments.go b/internal/executor/arguments.go index 491fd70..aa23128 100644 --- a/internal/executor/arguments.go +++ b/internal/executor/arguments.go @@ -71,6 +71,10 @@ 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) } @@ -78,6 +82,14 @@ func typecheckActionArgument(name string, value string, action *config.Action) e 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) diff --git a/internal/executor/arguments_test.go b/internal/executor/arguments_test.go index ae14ab2..1718125 100644 --- a/internal/executor/arguments_test.go +++ b/internal/executor/arguments_test.go @@ -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",