/
bool.go
91 lines (77 loc) · 2.36 KB
/
bool.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
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package settings
// BoolSetting is the interface of a setting variable that will be
// updated automatically when the corresponding cluster-wide setting
// of type "bool" is updated.
type BoolSetting struct {
common
defaultValue bool
}
var _ extendedSetting = &BoolSetting{}
// Get retrieves the bool value in the setting.
func (b *BoolSetting) Get(sv *Values) bool {
return sv.getInt64(b.slotIdx) != 0
}
func (b *BoolSetting) String(sv *Values) string {
return EncodeBool(b.Get(sv))
}
// Encoded returns the encoded value of the current value of the setting.
func (b *BoolSetting) Encoded(sv *Values) string {
return b.String(sv)
}
// EncodedDefault returns the encoded value of the default value of the setting.
func (b *BoolSetting) EncodedDefault() string {
return EncodeBool(b.defaultValue)
}
// Typ returns the short (1 char) string denoting the type of setting.
func (*BoolSetting) Typ() string {
return "b"
}
// Override changes the setting without validation and also overrides the
// default value.
//
// For testing usage only.
func (b *BoolSetting) Override(sv *Values, v bool) {
b.set(sv, v)
vInt := int64(0)
if v {
vInt = 1
}
sv.setDefaultOverrideInt64(b.slotIdx, vInt)
}
func (b *BoolSetting) set(sv *Values, v bool) {
vInt := int64(0)
if v {
vInt = 1
}
sv.setInt64(b.slotIdx, vInt)
}
func (b *BoolSetting) setToDefault(sv *Values) {
// See if the default value was overridden.
ok, val, _ := sv.getDefaultOverride(b.slotIdx)
if ok {
b.set(sv, val > 0)
return
}
b.set(sv, b.defaultValue)
}
// RegisterBoolSetting defines a new setting with type bool.
func RegisterBoolSetting(key, desc string, defaultValue bool) *BoolSetting {
setting := &BoolSetting{defaultValue: defaultValue}
register(key, desc, setting)
return setting
}
// RegisterPublicBoolSetting defines a new setting with type bool and makes it public.
func RegisterPublicBoolSetting(key, desc string, defaultValue bool) *BoolSetting {
s := RegisterBoolSetting(key, desc, defaultValue)
s.SetVisibility(Public)
return s
}