forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 1
/
enum.go
116 lines (103 loc) · 2.94 KB
/
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
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
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package settings
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
// EnumSetting is a StringSetting that restricts the values to be one of the `enumValues`
type EnumSetting struct {
IntSetting
enumValues map[int64]string
}
var _ Setting = &EnumSetting{}
// Typ returns the short (1 char) string denoting the type of setting.
func (e *EnumSetting) Typ() string {
return "e"
}
// ParseEnum returns the enum value, and a boolean that indicates if it was parseable.
func (e *EnumSetting) ParseEnum(raw string) (int64, bool) {
rawLower := strings.ToLower(raw)
for k, v := range e.enumValues {
if v == rawLower {
return k, true
}
}
// Attempt to parse the string as an integer since it isn't a valid enum string.
v, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return 0, false
}
_, ok := e.enumValues[v]
return v, ok
}
func (e *EnumSetting) set(k int64) error {
if _, ok := e.enumValues[k]; !ok {
return errors.Errorf("unrecognized value %d", k)
}
return e.IntSetting.set(k)
}
func enumValuesToDesc(enumValues map[int64]string) string {
var buffer bytes.Buffer
buffer.WriteString("[")
var notFirstElem bool
for k, v := range enumValues {
if notFirstElem {
buffer.WriteString(", ")
}
fmt.Fprintf(&buffer, "%s = %d", strings.ToLower(v), k)
notFirstElem = true
}
buffer.WriteString("]")
return buffer.String()
}
// RegisterEnumSetting defines a new setting with type int.
func RegisterEnumSetting(
key, desc string, defaultValue string, enumValues map[int64]string,
) *EnumSetting {
enumValuesLower := make(map[int64]string)
var i int64
var found bool
for k, v := range enumValues {
enumValuesLower[k] = strings.ToLower(v)
if v == defaultValue {
i = k
found = true
}
}
if !found {
panic(fmt.Sprintf("enum registered with default value %s not in map %s", defaultValue, enumValuesToDesc(enumValuesLower)))
}
setting := &EnumSetting{
IntSetting: IntSetting{defaultValue: i},
enumValues: enumValuesLower,
}
register(key, fmt.Sprintf("%s %s", desc, enumValuesToDesc(enumValues)), setting)
return setting
}
// TestingSetEnum returns a mock, unregistered enum setting for testing. See
// TestingSetBool for more details.
func TestingSetEnum(s **EnumSetting, i int64) func() {
saved := *s
*s = &EnumSetting{
IntSetting: IntSetting{v: i},
enumValues: saved.enumValues,
}
return func() {
*s = saved
}
}