Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e971837

Browse files
committedApr 3, 2025··
chore: use constants over string literals for option type
1 parent 0dfab58 commit e971837

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed
 

‎provider/formtype.go

+36-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ import (
66
"golang.org/x/xerrors"
77
)
88

9+
// OptionType is a type of option that can be used in the 'type' argument of
10+
// a parameter.
11+
type OptionType string
12+
13+
const (
14+
OptionTypeString OptionType = "string"
15+
OptionTypeNumber OptionType = "number"
16+
OptionTypeBoolean OptionType = "bool"
17+
OptionTypeListString OptionType = "list(string)"
18+
)
19+
20+
func OptionTypes() []OptionType {
21+
return []OptionType{
22+
OptionTypeString,
23+
OptionTypeNumber,
24+
OptionTypeBoolean,
25+
OptionTypeListString,
26+
}
27+
}
28+
929
type ParameterFormType string
1030

1131
const (
@@ -22,12 +42,13 @@ const (
2242
ParameterFormTypeError ParameterFormType = "error"
2343
)
2444

45+
// ParameterFormTypes should be kept in sync with the enum list above.
2546
func ParameterFormTypes() []ParameterFormType {
2647
return []ParameterFormType{
2748
ParameterFormTypeDefault,
2849
ParameterFormTypeRadio,
29-
ParameterFormTypeInput,
3050
ParameterFormTypeSlider,
51+
ParameterFormTypeInput,
3152
ParameterFormTypeDropdown,
3253
ParameterFormTypeCheckbox,
3354
ParameterFormTypeSwitch,
@@ -41,6 +62,8 @@ func ParameterFormTypes() []ParameterFormType {
4162
// formTypeTruthTable is a map of [`type`][`optionCount` > 0] to `form_type`.
4263
// The first value in the slice is the default value assuming `form_type` is
4364
// not specified.
65+
//
66+
// The boolean key indicates whether the `options` field is specified.
4467
// | Type | Options | Specified Form Type | form_type | Notes |
4568
// |-------------------|---------|---------------------|----------------|--------------------------------|
4669
// | `string` `number` | Y | | `radio` | |
@@ -54,28 +77,32 @@ func ParameterFormTypes() []ParameterFormType {
5477
// | `list(string)` | Y | | `radio` | |
5578
// | `list(string)` | N | | `tag-select` | |
5679
// | `list(string)` | Y | `multi-select` | `multi-select` | Option values will be `string` |
57-
var formTypeTruthTable = map[string]map[bool][]ParameterFormType{
58-
"string": {
80+
var formTypeTruthTable = map[OptionType]map[bool][]ParameterFormType{
81+
OptionTypeString: {
5982
true: {ParameterFormTypeRadio, ParameterFormTypeDropdown},
6083
false: {ParameterFormTypeInput, ParameterFormTypeTextArea},
6184
},
62-
"number": {
85+
OptionTypeNumber: {
6386
true: {ParameterFormTypeRadio, ParameterFormTypeDropdown},
6487
false: {ParameterFormTypeInput, ParameterFormTypeSlider},
6588
},
66-
"bool": {
89+
OptionTypeBoolean: {
6790
true: {ParameterFormTypeRadio},
6891
false: {ParameterFormTypeCheckbox, ParameterFormTypeSwitch},
6992
},
70-
"list(string)": {
93+
OptionTypeListString: {
7194
true: {ParameterFormTypeRadio, ParameterFormTypeMultiSelect},
7295
false: {ParameterFormTypeTagSelect},
7396
},
7497
}
7598

7699
// ValidateFormType handles the truth table for the valid set of `type` and
77100
// `form_type` options.
78-
func ValidateFormType(paramType string, optionCount int, specifiedFormType ParameterFormType) (string, ParameterFormType, error) {
101+
// The OptionType is also returned because it is possible the 'type' of the
102+
// 'value' & 'default' fields is different from the 'type' of the options.
103+
// The use case is when using multi-select. The options are 'string' and the
104+
// value is 'list(string)'.
105+
func ValidateFormType(paramType OptionType, optionCount int, specifiedFormType ParameterFormType) (OptionType, ParameterFormType, error) {
79106
allowed, ok := formTypeTruthTable[paramType][optionCount > 0]
80107
if !ok || len(allowed) == 0 {
81108
return paramType, specifiedFormType, xerrors.Errorf("value type %q is not supported for 'form_types'", paramType)
@@ -91,8 +118,8 @@ func ValidateFormType(paramType string, optionCount int, specifiedFormType Param
91118
}
92119

93120
// Special case
94-
if paramType == "list(string)" && specifiedFormType == ParameterFormTypeMultiSelect {
95-
return "string", ParameterFormTypeMultiSelect, nil
121+
if paramType == OptionTypeListString && specifiedFormType == ParameterFormTypeMultiSelect {
122+
return OptionTypeListString, ParameterFormTypeMultiSelect, nil
96123
}
97124

98125
return paramType, specifiedFormType, nil

‎provider/parameter.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Parameter struct {
5151
Name string
5252
DisplayName string `mapstructure:"display_name"`
5353
Description string
54-
Type string
54+
Type OptionType
5555
FormType ParameterFormType
5656
Mutable bool
5757
Default string
@@ -154,7 +154,7 @@ func parameterDataSource() *schema.Resource {
154154
}
155155

156156
// Validate options
157-
var optionType string
157+
var optionType OptionType
158158
optionType, parameter.FormType, err = ValidateFormType(parameter.Type, len(parameter.Option), parameter.FormType)
159159
if err != nil {
160160
return diag.FromErr(err)
@@ -182,7 +182,7 @@ func parameterDataSource() *schema.Resource {
182182
}
183183

184184
if parameter.Default != "" {
185-
if parameter.Type == "list(string)" && optionType == "string" {
185+
if parameter.Type == OptionTypeListString && optionType == OptionTypeString {
186186
// If the type is list(string) and optionType is string, we have
187187
// to ensure all elements of the default exist as options.
188188
var defaultValues []string
@@ -241,7 +241,7 @@ func parameterDataSource() *schema.Resource {
241241
Type: schema.TypeString,
242242
Default: "string",
243243
Optional: true,
244-
ValidateFunc: validation.StringInSlice([]string{"number", "string", "bool", "list(string)"}, false),
244+
ValidateFunc: validation.StringInSlice(toStrings(OptionTypes()), false),
245245
Description: "The type of this parameter. Must be one of: `\"number\"`, `\"string\"`, `\"bool\"`, or `\"list(string)\"`.",
246246
},
247247
"form_type": {
@@ -431,38 +431,38 @@ func fixValidationResourceData(rawConfig cty.Value, validation interface{}) (int
431431
return vArr, nil
432432
}
433433

434-
func valueIsType(typ, value string) diag.Diagnostics {
434+
func valueIsType(typ OptionType, value string) diag.Diagnostics {
435435
switch typ {
436-
case "number":
436+
case OptionTypeNumber:
437437
_, err := strconv.ParseFloat(value, 64)
438438
if err != nil {
439439
return diag.Errorf("%q is not a number", value)
440440
}
441-
case "bool":
441+
case OptionTypeBoolean:
442442
_, err := strconv.ParseBool(value)
443443
if err != nil {
444444
return diag.Errorf("%q is not a bool", value)
445445
}
446-
case "list(string)":
446+
case OptionTypeListString:
447447
var items []string
448448
err := json.Unmarshal([]byte(value), &items)
449449
if err != nil {
450450
return diag.Errorf("%q is not an array of strings", value)
451451
}
452-
case "string":
452+
case OptionTypeString:
453453
// Anything is a string!
454454
default:
455455
return diag.Errorf("invalid type %q", typ)
456456
}
457457
return nil
458458
}
459459

460-
func (v *Validation) Valid(typ, value string) error {
460+
func (v *Validation) Valid(typ OptionType, value string) error {
461461
if v.Invalid {
462462
return v.errorRendered(value)
463463
}
464464

465-
if typ != "number" {
465+
if typ != OptionTypeNumber {
466466
if !v.MinDisabled {
467467
return fmt.Errorf("a min cannot be specified for a %s type", typ)
468468
}
@@ -473,16 +473,16 @@ func (v *Validation) Valid(typ, value string) error {
473473
return fmt.Errorf("monotonic validation can only be specified for number types, not %s types", typ)
474474
}
475475
}
476-
if typ != "string" && v.Regex != "" {
476+
if typ != OptionTypeString && v.Regex != "" {
477477
return fmt.Errorf("a regex cannot be specified for a %s type", typ)
478478
}
479479
switch typ {
480-
case "bool":
480+
case OptionTypeBoolean:
481481
if value != "true" && value != "false" {
482482
return fmt.Errorf(`boolean value can be either "true" or "false"`)
483483
}
484484
return nil
485-
case "string":
485+
case OptionTypeString:
486486
if v.Regex == "" {
487487
return nil
488488
}
@@ -497,7 +497,7 @@ func (v *Validation) Valid(typ, value string) error {
497497
if !matched {
498498
return fmt.Errorf("%s (value %q does not match %q)", v.Error, value, regex)
499499
}
500-
case "number":
500+
case OptionTypeNumber:
501501
num, err := strconv.Atoi(value)
502502
if err != nil {
503503
return takeFirstError(v.errorRendered(value), fmt.Errorf("value %q is not a number", value))
@@ -511,7 +511,7 @@ func (v *Validation) Valid(typ, value string) error {
511511
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
512512
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
513513
}
514-
case "list(string)":
514+
case OptionTypeListString:
515515
var listOfStrings []string
516516
err := json.Unmarshal([]byte(value), &listOfStrings)
517517
if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.