/
parameter_enum.go
52 lines (40 loc) · 922 Bytes
/
parameter_enum.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
package config
import (
"context"
"errors"
)
var (
errInvalidChoice = errors.New("invalid choice")
errNoChoices = errors.New("no choices are prodvided")
)
const ParameterEnum = "enum"
type paramEnum struct {
baseParameter
choices map[string]struct{}
}
func (p paramEnum) Type() string {
return ParameterEnum
}
func (p paramEnum) Validate(_ context.Context, value string) error {
if _, ok := p.choices[value]; !ok {
return errInvalidChoice
}
return nil
}
func NewEnum(description string, required bool, spec map[string]string) (Parameter, error) {
param := paramEnum{
baseParameter: baseParameter{
required: required,
description: description,
specification: spec,
},
choices: make(map[string]struct{}),
}
for _, v := range parseCSV(spec["choices"]) {
param.choices[v] = struct{}{}
}
if len(param.choices) == 0 {
return param, errNoChoices
}
return param, nil
}