-
Notifications
You must be signed in to change notification settings - Fork 22
feat: form_type
and styling
metadata arguments added
#375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 33 commits
43253f7
2d79a97
4bafcc6
19d6749
9fc221c
b80ab8f
50c6a0d
c0d38d3
0b94955
05e682d
0dfab58
e971837
12aec75
0b28b64
bf2b673
c0be472
cd52cae
fec030d
f0bca91
18c3d05
a04ffa6
2b5ff08
18886f8
acf5977
fcdd6bd
5da70d5
8356ff7
771be40
bb00c25
d25fcf6
8951940
5d485d6
f21c732
f110129
d330f21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
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. | ||
// For example, "multi-select" has the type "list(string)" but the option | ||
// values are "string". | ||
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good to explicitly document each type + option value for the possible values of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather defer that to the truth table below. I do not want to document it in 2 places. |
||
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) { | ||
allowed, ok := formTypeTruthTable[paramType][optionCount > 0] | ||
if !ok || len(allowed) == 0 { | ||
return paramType, specifiedFormType, xerrors.Errorf("value type %q is not supported for 'form_types'", paramType) | ||
} | ||
|
||
if specifiedFormType == ParameterFormTypeDefault { | ||
// handle the default case | ||
specifiedFormType = allowed[0] | ||
} | ||
|
||
if !slices.Contains(allowed, specifiedFormType) { | ||
return paramType, specifiedFormType, xerrors.Errorf("value type %q is not supported for 'form_types'", specifiedFormType) | ||
} | ||
|
||
// 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 | ||
} | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future readers wishing to understand the source of our woes, it might be good to add some relevant links to existing issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually do not know what to link 🤔.