github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/settings/bool.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package settings 12 13 // BoolSetting is the interface of a setting variable that will be 14 // updated automatically when the corresponding cluster-wide setting 15 // of type "bool" is updated. 16 type BoolSetting struct { 17 common 18 defaultValue bool 19 } 20 21 var _ extendedSetting = &BoolSetting{} 22 23 // Get retrieves the bool value in the setting. 24 func (b *BoolSetting) Get(sv *Values) bool { 25 return sv.getInt64(b.slotIdx) != 0 26 } 27 28 func (b *BoolSetting) String(sv *Values) string { 29 return EncodeBool(b.Get(sv)) 30 } 31 32 // Encoded returns the encoded value of the current value of the setting. 33 func (b *BoolSetting) Encoded(sv *Values) string { 34 return b.String(sv) 35 } 36 37 // EncodedDefault returns the encoded value of the default value of the setting. 38 func (b *BoolSetting) EncodedDefault() string { 39 return EncodeBool(b.defaultValue) 40 } 41 42 // Typ returns the short (1 char) string denoting the type of setting. 43 func (*BoolSetting) Typ() string { 44 return "b" 45 } 46 47 // Override changes the setting without validation and also overrides the 48 // default value. 49 // 50 // For testing usage only. 51 func (b *BoolSetting) Override(sv *Values, v bool) { 52 b.set(sv, v) 53 54 vInt := int64(0) 55 if v { 56 vInt = 1 57 } 58 sv.setDefaultOverrideInt64(b.slotIdx, vInt) 59 } 60 61 func (b *BoolSetting) set(sv *Values, v bool) { 62 vInt := int64(0) 63 if v { 64 vInt = 1 65 } 66 sv.setInt64(b.slotIdx, vInt) 67 } 68 69 func (b *BoolSetting) setToDefault(sv *Values) { 70 // See if the default value was overridden. 71 ok, val, _ := sv.getDefaultOverride(b.slotIdx) 72 if ok { 73 b.set(sv, val > 0) 74 return 75 } 76 b.set(sv, b.defaultValue) 77 } 78 79 // RegisterBoolSetting defines a new setting with type bool. 80 func RegisterBoolSetting(key, desc string, defaultValue bool) *BoolSetting { 81 setting := &BoolSetting{defaultValue: defaultValue} 82 register(key, desc, setting) 83 return setting 84 } 85 86 // RegisterPublicBoolSetting defines a new setting with type bool and makes it public. 87 func RegisterPublicBoolSetting(key, desc string, defaultValue bool) *BoolSetting { 88 s := RegisterBoolSetting(key, desc, defaultValue) 89 s.SetVisibility(Public) 90 return s 91 }