|
| 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 | +} |
0 commit comments