-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathformtype.go
170 lines (154 loc) · 6.91 KB
/
formtype.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package provider
import (
"slices"
"golang.org/x/xerrors"
)
// OptionType is a type of option that can be used in the 'type' argument of
// a parameter. These should match types as defined in terraform:
//
// https://developer.hashicorp.com/terraform/language/expressions/types
//
// The value have to be string literals, as type constraint keywords are not
// supported in providers.
type OptionType = string
const (
OptionTypeString OptionType = "string"
OptionTypeNumber OptionType = "number"
OptionTypeBoolean OptionType = "bool"
OptionTypeListString OptionType = "list(string)"
)
func OptionTypes() []OptionType {
return []OptionType{
OptionTypeString,
OptionTypeNumber,
OptionTypeBoolean,
OptionTypeListString,
}
}
// ParameterFormType is the list of supported form types for display in
// the Coder "create workspace" form. These form types are functional as well
// as cosmetic. Refer to `formTypeTruthTable` for the allowed pairings.
// For example, "multi-select" has the type "list(string)" but the option
// values are "string".
type ParameterFormType string
const (
ParameterFormTypeDefault ParameterFormType = ""
ParameterFormTypeRadio ParameterFormType = "radio"
ParameterFormTypeSlider ParameterFormType = "slider"
ParameterFormTypeInput ParameterFormType = "input"
ParameterFormTypeDropdown ParameterFormType = "dropdown"
ParameterFormTypeCheckbox ParameterFormType = "checkbox"
ParameterFormTypeSwitch ParameterFormType = "switch"
ParameterFormTypeMultiSelect ParameterFormType = "multi-select"
ParameterFormTypeTagSelect ParameterFormType = "tag-select"
ParameterFormTypeTextArea ParameterFormType = "textarea"
ParameterFormTypeError ParameterFormType = "error"
)
// ParameterFormTypes should be kept in sync with the enum list above.
func ParameterFormTypes() []ParameterFormType {
return []ParameterFormType{
// Intentionally omit "ParameterFormTypeDefault" from this set.
// It is a valid enum, but will always be mapped to a real value when
// being used.
ParameterFormTypeRadio,
ParameterFormTypeSlider,
ParameterFormTypeInput,
ParameterFormTypeDropdown,
ParameterFormTypeCheckbox,
ParameterFormTypeSwitch,
ParameterFormTypeMultiSelect,
ParameterFormTypeTagSelect,
ParameterFormTypeTextArea,
ParameterFormTypeError,
}
}
// formTypeTruthTable is a map of [`type`][`optionCount` > 0] to `form_type`.
// The first value in the slice is the default value assuming `form_type` is
// not specified.
//
// The boolean key indicates whether the `options` field is specified.
// | Type | Options | Specified Form Type | form_type | Notes |
// |-------------------|---------|---------------------|----------------|--------------------------------|
// | `string` `number` | Y | | `radio` | |
// | `string` `number` | Y | `dropdown` | `dropdown` | |
// | `string` `number` | N | | `input` | |
// | `string` | N | 'textarea' | `textarea` | |
// | `number` | N | 'slider' | `slider` | min/max validation |
// | `bool` | Y | | `radio` | |
// | `bool` | N | | `checkbox` | |
// | `bool` | N | `switch` | `switch` | |
// | `list(string)` | Y | | `radio` | |
// | `list(string)` | N | | `tag-select` | |
// | `list(string)` | Y | `multi-select` | `multi-select` | Option values will be `string` |
var formTypeTruthTable = map[OptionType]map[bool][]ParameterFormType{
OptionTypeString: {
true: {ParameterFormTypeRadio, ParameterFormTypeDropdown},
false: {ParameterFormTypeInput, ParameterFormTypeTextArea},
},
OptionTypeNumber: {
true: {ParameterFormTypeRadio, ParameterFormTypeDropdown},
false: {ParameterFormTypeInput, ParameterFormTypeSlider},
},
OptionTypeBoolean: {
true: {ParameterFormTypeRadio},
false: {ParameterFormTypeCheckbox, ParameterFormTypeSwitch},
},
OptionTypeListString: {
true: {ParameterFormTypeRadio, ParameterFormTypeMultiSelect},
false: {ParameterFormTypeTagSelect},
},
}
// ValidateFormType handles the truth table for the valid set of `type` and
// `form_type` options.
// The OptionType is also returned because it is possible the 'type' of the
// 'value' & 'default' fields is different from the 'type' of the options.
// The use case is when using multi-select. The options are 'string' and the
// value is 'list(string)'.
func ValidateFormType(paramType OptionType, optionCount int, specifiedFormType ParameterFormType) (OptionType, ParameterFormType, error) {
optionsExist := optionCount > 0
allowed, ok := formTypeTruthTable[paramType][optionsExist]
if !ok || len(allowed) == 0 {
// This error should really never be hit, as the provider sdk does an enum validation.
return paramType, specifiedFormType, xerrors.Errorf("\"type\" attribute=%q is not supported, choose one of %v", paramType, OptionTypes())
}
if specifiedFormType == ParameterFormTypeDefault {
// handle the default case
specifiedFormType = allowed[0]
}
if !slices.Contains(allowed, specifiedFormType) {
optionMsg := ""
opposite := formTypeTruthTable[paramType][!optionsExist]
// This extra message tells a user if they are using a valid form_type
// for a 'type', but it is invalid because options do/do-not exist.
// It serves as a more helpful error message.
//
// Eg: form_type=slider is valid for type=number, but invalid if options exist.
// And this error message is more accurate than just saying "form_type=slider is
// not valid for type=number".
if slices.Contains(opposite, specifiedFormType) {
if optionsExist {
optionMsg = " when options exist"
} else {
optionMsg = " when options do no exist"
}
}
return paramType, specifiedFormType,
xerrors.Errorf("\"form_type\" attribute=%q is not supported for \"type\"=%q%s, choose one of %v",
specifiedFormType, paramType,
optionMsg, toStrings(allowed))
}
// This is the only current special case. If 'multi-select' is selected, the type
// of 'value' and an options 'value' are different. The type of the parameter is
// `list(string)` but the type of the individual options is `string`.
if paramType == OptionTypeListString && specifiedFormType == ParameterFormTypeMultiSelect {
return OptionTypeString, ParameterFormTypeMultiSelect, nil
}
return paramType, specifiedFormType, nil
}
func toStrings[A ~string](l []A) []string {
var r []string
for _, v := range l {
r = append(r, string(v))
}
return r
}