inOptionSet returns if the value given is in the set of options for a parameter.
(richParameter TemplateVersionParameter, value string)
| 123 | |
| 124 | // inOptionSet returns if the value given is in the set of options for a parameter. |
| 125 | func inOptionSet(richParameter TemplateVersionParameter, value string) bool { |
| 126 | optionValues := make([]string, 0, len(richParameter.Options)) |
| 127 | for _, option := range richParameter.Options { |
| 128 | optionValues = append(optionValues, option.Value) |
| 129 | } |
| 130 | |
| 131 | // If the type is `list(string)` and the form_type is `multi-select`, then we check each individual |
| 132 | // value in the list against the option set. |
| 133 | isMultiSelect := richParameter.Type == provider.OptionTypeListString && richParameter.FormType == string(provider.ParameterFormTypeMultiSelect) |
| 134 | |
| 135 | if !isMultiSelect { |
| 136 | // This is the simple case. Just checking if the value is in the option set. |
| 137 | return slice.Contains(optionValues, value) |
| 138 | } |
| 139 | |
| 140 | var checks []string |
| 141 | err := json.Unmarshal([]byte(value), &checks) |
| 142 | if err != nil { |
| 143 | return false |
| 144 | } |
| 145 | |
| 146 | for _, check := range checks { |
| 147 | if !slice.Contains(optionValues, check) { |
| 148 | return false |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return true |
| 153 | } |
| 154 | |
| 155 | func findBuildParameter(params []WorkspaceBuildParameter, parameterName string) (*WorkspaceBuildParameter, bool) { |
| 156 | if params == nil { |