Skip to content

Commit 43253f7

Browse files
committed
feat: add form_type argument to parameters
1 parent 9a74558 commit 43253f7

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

Diff for: provider/formtype.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package provider
2+
3+
import (
4+
"slices"
5+
6+
"golang.org/x/xerrors"
7+
)
8+
9+
type ParameterFormType string
10+
11+
const (
12+
ParameterFormTypeDefault ParameterFormType = ""
13+
ParameterFormTypeRadio ParameterFormType = "radio"
14+
ParameterFormTypeInput ParameterFormType = "input"
15+
ParameterFormTypeDropdown ParameterFormType = "dropdown"
16+
ParameterFormTypeCheckbox ParameterFormType = "checkbox"
17+
ParameterFormTypeSwitch ParameterFormType = "switch"
18+
ParameterFormTypeMultiSelect ParameterFormType = "multi-select"
19+
ParameterFormTypeTagInput ParameterFormType = "tag-input"
20+
)
21+
22+
func ParameterFormTypes() []ParameterFormType {
23+
return []ParameterFormType{
24+
ParameterFormTypeDefault,
25+
ParameterFormTypeRadio,
26+
ParameterFormTypeInput,
27+
ParameterFormTypeDropdown,
28+
ParameterFormTypeCheckbox,
29+
ParameterFormTypeSwitch,
30+
ParameterFormTypeMultiSelect,
31+
ParameterFormTypeTagInput,
32+
}
33+
}
34+
35+
// formTypeTruthTable is a map of [`type`][`optionCount` > 0] to `form_type`.
36+
// The first value in the slice is the default value assuming `form_type` is
37+
// not specified.
38+
// | Type | Options | Specified Form Type | form_type | Notes |
39+
// |-------------------|---------|---------------------|----------------|--------------------------------|
40+
// | `string` `number` | Y | | `radio` | |
41+
// | `string` `number` | Y | `dropdown` | `dropdown` | |
42+
// | `string` `number` | N | | `input` | |
43+
// | `bool` | Y | | `radio` | |
44+
// | `bool` | N | | `checkbox` | |
45+
// | `bool` | N | `switch` | `switch` | |
46+
// | `list(string)` | Y | | `radio` | |
47+
// | `list(string)` | N | | `tag-select` | |
48+
// | `list(string)` | Y | `multi-select` | `multi-select` | Option values will be `string` |
49+
var formTypeTruthTable = map[string]map[bool][]ParameterFormType{
50+
"string": {
51+
true: {ParameterFormTypeRadio, ParameterFormTypeDropdown},
52+
false: {ParameterFormTypeInput},
53+
},
54+
"number": {
55+
true: {ParameterFormTypeRadio, ParameterFormTypeDropdown},
56+
false: {ParameterFormTypeInput},
57+
},
58+
"bool": {
59+
true: {ParameterFormTypeRadio},
60+
false: {ParameterFormTypeCheckbox, ParameterFormTypeSwitch},
61+
},
62+
"list(string)": {
63+
true: {ParameterFormTypeRadio, ParameterFormTypeMultiSelect},
64+
false: {ParameterFormTypeTagInput},
65+
},
66+
}
67+
68+
// ValidateFormType handles the truth table for the valid set of `type` and
69+
// `form_type` options.
70+
// | Type | Options | Specified Form Type | form_type | Notes |
71+
// |-------------------|---------|---------------------|----------------|--------------------------------|
72+
// | `string` `number` | Y | | `radio` | |
73+
// | `string` `number` | Y | `dropdown` | `dropdown` | |
74+
// | `string` `number` | N | | `input` | |
75+
// | `bool` | Y | | `radio` | |
76+
// | `bool` | N | | `checkbox` | |
77+
// | `bool` | N | `switch` | `switch` | |
78+
// | `list(string)` | Y | | `radio` | |
79+
// | `list(string)` | N | | `tag-select` | |
80+
// | `list(string)` | Y | `multi-select` | `multi-select` | Option values will be `string` |
81+
func ValidateFormType(paramType string, optionCount int, specifiedFormType ParameterFormType) (ParameterFormType, string, error) {
82+
allowed, ok := formTypeTruthTable[paramType][optionCount > 0]
83+
if !ok || len(allowed) == 0 {
84+
return specifiedFormType, paramType, xerrors.Errorf("value type %q is not supported for 'form_types'", paramType)
85+
}
86+
87+
if specifiedFormType == ParameterFormTypeDefault {
88+
// handle the default case
89+
specifiedFormType = allowed[0]
90+
}
91+
92+
if !slices.Contains(allowed, specifiedFormType) {
93+
return specifiedFormType, paramType, xerrors.Errorf("value type %q is not supported for 'form_types'", paramType)
94+
}
95+
96+
// Special case
97+
if paramType == "list(string)" && specifiedFormType == ParameterFormTypeMultiSelect {
98+
return ParameterFormTypeMultiSelect, "string", nil
99+
}
100+
101+
return specifiedFormType, paramType, nil
102+
}
103+
104+
func toStrings[A ~string](l []A) []string {
105+
var r []string
106+
for _, v := range l {
107+
r = append(r, string(v))
108+
}
109+
return r
110+
}

Diff for: provider/parameter.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Parameter struct {
5151
DisplayName string `mapstructure:"display_name"`
5252
Description string
5353
Type string
54+
FormType ParameterFormType `mapstructure:"form_type"`
5455
Mutable bool
5556
Default string
5657
Icon string
@@ -86,6 +87,7 @@ func parameterDataSource() *schema.Resource {
8687
DisplayName interface{}
8788
Description interface{}
8889
Type interface{}
90+
FormType interface{}
8991
Mutable interface{}
9092
Default interface{}
9193
Icon interface{}
@@ -100,6 +102,7 @@ func parameterDataSource() *schema.Resource {
100102
DisplayName: rd.Get("display_name"),
101103
Description: rd.Get("description"),
102104
Type: rd.Get("type"),
105+
FormType: rd.Get("form_type"),
103106
Mutable: rd.Get("mutable"),
104107
Default: rd.Get("default"),
105108
Icon: rd.Get("icon"),
@@ -149,6 +152,10 @@ func parameterDataSource() *schema.Resource {
149152
}
150153
}
151154

155+
// Validate options
156+
var optionType string
157+
parameter.FormType, optionType, err = ValidateFormType(parameter.Type, len(parameter.Option), parameter.FormType)
158+
152159
if len(parameter.Option) > 0 {
153160
names := map[string]interface{}{}
154161
values := map[string]interface{}{}
@@ -161,7 +168,7 @@ func parameterDataSource() *schema.Resource {
161168
if exists {
162169
return diag.Errorf("multiple options cannot have the same value %q", option.Value)
163170
}
164-
err := valueIsType(parameter.Type, option.Value)
171+
err := valueIsType(optionType, option.Value)
165172
if err != nil {
166173
return err
167174
}
@@ -206,6 +213,13 @@ func parameterDataSource() *schema.Resource {
206213
ValidateFunc: validation.StringInSlice([]string{"number", "string", "bool", "list(string)"}, false),
207214
Description: "The type of this parameter. Must be one of: `\"number\"`, `\"string\"`, `\"bool\"`, or `\"list(string)\"`.",
208215
},
216+
"form_type": {
217+
Type: schema.TypeString,
218+
Default: ParameterFormTypeDefault,
219+
Optional: true,
220+
ValidateFunc: validation.StringInSlice(toStrings(ParameterFormTypes()), false),
221+
Description: fmt.Sprintf("The type of this parameter. Must be one of: [%s].", strings.Join(toStrings(ParameterFormTypes()), ", ")),
222+
},
209223
"mutable": {
210224
Type: schema.TypeBool,
211225
Optional: true,

0 commit comments

Comments
 (0)