From 96439972e22ad52f5aa1e59624be714ed79fb398 Mon Sep 17 00:00:00 2001 From: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Date: Fri, 24 Apr 2020 14:13:02 +0200 Subject: [PATCH] docs: show possible values in step documentation (#1453) * add possibleValues to model * include possible values into docs --- pkg/config/stepmeta.go | 1 + pkg/generator/helper/docuHelper.go | 16 ++++++++++- pkg/generator/helper/docuHelper_test.go | 38 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/pkg/config/stepmeta.go b/pkg/config/stepmeta.go index 11baedd21..175b3e0e1 100644 --- a/pkg/config/stepmeta.go +++ b/pkg/config/stepmeta.go @@ -52,6 +52,7 @@ type StepParameters struct { Type string `json:"type"` Mandatory bool `json:"mandatory,omitempty"` Default interface{} `json:"default,omitempty"` + PossibleValues []interface{} `json:"possibleValues,omitempty"` Aliases []Alias `json:"aliases,omitempty"` Conditions []Condition `json:"conditions,omitempty"` Secret bool `json:"secret,omitempty"` diff --git a/pkg/generator/helper/docuHelper.go b/pkg/generator/helper/docuHelper.go index bdc4f4993..72e6e0c0c 100644 --- a/pkg/generator/helper/docuHelper.go +++ b/pkg/generator/helper/docuHelper.go @@ -147,7 +147,7 @@ func createParametersTable(parameters []config.StepParameters) string { for _, param := range parameters { if v, ok := m[param.Name]; ok { - table += fmt.Sprintf(" | %v | %v | %v | |\n ", param.Name, ifThenElse(param.Mandatory && param.Default == nil, "Yes", "No"), ifThenElse(v == "", "", v)) + table += fmt.Sprintf(" | %v | %v | %v | %v |\n ", param.Name, ifThenElse(param.Mandatory && param.Default == nil, "Yes", "No"), ifThenElse(v == "", "", v), possibleValuesToString(param.PossibleValues)) delete(m, param.Name) } } @@ -468,3 +468,17 @@ func sortStepParameters(stepData *config.StepData) { }) } } + +func possibleValuesToString(in []interface{}) (out string) { + if len(in) == 0 { + return + } + out = fmt.Sprintf("`%v`", in[0]) + if len(in) == 1 { + return + } + for _, value := range in[1:] { + out += fmt.Sprintf(", `%v`", value) + } + return +} diff --git a/pkg/generator/helper/docuHelper_test.go b/pkg/generator/helper/docuHelper_test.go index 827de952d..c959cac18 100644 --- a/pkg/generator/helper/docuHelper_test.go +++ b/pkg/generator/helper/docuHelper_test.go @@ -400,3 +400,41 @@ func TestAddExistingParameterWithCondition(t *testing.T) { } }) } + +func TestRenderPossibleValues(t *testing.T) { + t.Run("none", func(t *testing.T) { + // init + var in []interface{} + // test + out := possibleValuesToString(in) + // assert + assert.Empty(t, out) + }) + t.Run("one", func(t *testing.T) { + // init + var in []interface{} + in = append(in, "fu") + // test + out := possibleValuesToString(in) + // assert + assert.Equal(t, "`fu`", out) + }) + t.Run("many", func(t *testing.T) { + // init + var in []interface{} + in = append(in, "fu", "fara") + // test + out := possibleValuesToString(in) + // assert + assert.Equal(t, "`fu`, `fara`", out) + }) + t.Run("boolean", func(t *testing.T) { + // init + var in []interface{} + in = append(in, false, true) + // test + out := possibleValuesToString(in) + // assert + assert.Equal(t, "`false`, `true`", out) + }) +}