Skip to content

Commit

Permalink
fix: Fix the growth of the 'valid' slice
Browse files Browse the repository at this point in the history
Add ValidationValInSlice
  • Loading branch information
vaerh committed Jan 5, 2024
1 parent 1899983 commit 206cfc2
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 7 deletions.
54 changes: 47 additions & 7 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,30 +430,31 @@ var (
// but the provided value can be a single value or a comma-separated list of values.
// The negative indication of the parameter is also supported by adding "!" before value if mikrotikNegative is true.
ValidationMultiValInSlice = func(valid []string, ignoreCase, mikrotikNegative bool) schema.SchemaValidateDiagFunc {
return func(v interface{}, path cty.Path) (diags diag.Diagnostics) {
val, ok := v.(string)
return func(i interface{}, path cty.Path) (diags diag.Diagnostics) {
v, ok := i.(string)

if !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Bad value type",
Detail: fmt.Sprintf("Value should be a string: %v (type = %T)", val, val),
Detail: fmt.Sprintf("Value should be a string: %v (type = %T)", v, v),
})

return
}

var negative []string
if mikrotikNegative {
for _, v := range valid {
valid = append(valid, "!"+v)
for _, str := range valid {
negative = append(negative, "!"+str)
}
}

for _, sValue := range strings.Split(val, ",") {
for _, sValue := range strings.Split(v, ",") {
ok := false
sValue = strings.TrimSpace(sValue)

for _, sValid := range valid {
for _, sValid := range append(negative, valid...) {
if sValue == sValid || (ignoreCase && strings.EqualFold(sValue, sValid)) {
ok = true
break
Expand All @@ -472,6 +473,45 @@ var (
return
}
}

ValidationValInSlice = func(valid []string, ignoreCase, mikrotikNegative bool) schema.SchemaValidateDiagFunc {
return func(i interface{}, path cty.Path) (diags diag.Diagnostics) {
v, ok := i.(string)

if !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Bad value type",
Detail: fmt.Sprintf("Value should be a string: %v (type = %T)", v, v),
})

return
}

var negative []string
if mikrotikNegative {
for _, str := range valid {
negative = append(negative, "!"+str)
}
}

v = strings.TrimSpace(v)

for _, str := range append(negative, valid...) {
if v == str || (ignoreCase && strings.EqualFold(v, str)) {
return
}
}

diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Bad value",
Detail: fmt.Sprintf("Unexpected value: %v", v),
})

return
}
}
)

// Properties DiffSuppressFunc.
Expand Down
63 changes: 63 additions & 0 deletions routeros/provider_schema_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,66 @@ func TestValidationMultiValInSlice(t *testing.T) {
})
}
}

func TestValidationValInSlice(t *testing.T) {
type args struct {
valid []string
ignoreCase bool
mikrotikNegative bool
value string
}
tests := []struct {
name string
args args
want int
}{
{
"Positive #1",
args{[]string{"a", "b", "c", "d"}, false, false, " a "},
0,
},
{
"Positive #2",
args{[]string{"a", "b", "c", "d"}, false, false, "b"},
0,
},
{
"Positive #3",
args{[]string{"a", "b", "c", "d"}, false, false, "d"},
0,
},
{
"Positive #4",
args{[]string{"a", "b", "c", "d"}, false, true, "!d"},
0,
},
{
"Positive #5",
args{[]string{"a", "b", "c", "d"}, true, true, "!D"},
0,
},
{
"Negative #1",
args{[]string{"a", "b", "c", "d"}, false, false, "e"},
1,
},
{
"Negative #2",
args{[]string{"a", "b", "c", "d"}, false, false, "!a"},
1,
},
{
"Positive #3",
args{[]string{"a", "b", "c", "d"}, false, false, "A"},
1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := ValidationValInSlice(tt.args.valid, tt.args.ignoreCase, tt.args.mikrotikNegative)
if got := f(tt.args.value, *new(cty.Path)); len(got) != tt.want {
t.Errorf("ValidationValInSlice() diag length = %v, want %v, diags: %v", len(got), tt.want, got)
}
})
}
}

0 comments on commit 206cfc2

Please sign in to comment.