-
Notifications
You must be signed in to change notification settings - Fork 0
/
intValue.go
144 lines (102 loc) · 3.21 KB
/
intValue.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package goconfig
import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"reflect"
"strconv"
)
const (
maxUint = ^uint(0)
invalidIntDefault = int(maxUint >> 1)
)
// IntValue int Config Value type
type IntValue struct {
*valueInfo
defaultValue *int
actualValue *int
}
func makeIntValue(name string, shorthand *string, defaultValue *int, usageDescription string) *IntValue {
var exampleType int
valueInfo := makeValueInfo(name, shorthand, usageDescription, reflect.TypeOf(exampleType))
result := &IntValue{
valueInfo: valueInfo,
defaultValue: defaultValue,
}
result.Setup()
return result
}
func (this *IntValue) HasAValue() bool {
return this.Value() != nil
}
func (this *IntValue) HasADefaultValue() bool {
return this.defaultValue != nil && *this.defaultValue != invalidIntDefault
}
func (this *IntValue) DefaultValueAsString() *string {
if this.defaultValue != nil && *this.defaultValue != invalidIntDefault {
asString := strconv.Itoa(*this.defaultValue)
return &asString
}
return nil
}
func (this *IntValue) Setup() {
if this.shorthand != nil {
this.actualValue = pflag.IntP(this.name,
*this.shorthand,
this.useDefaultWithViper(),
this.usageDescription)
} else {
this.actualValue = pflag.Int(this.name,
this.useDefaultWithViper(),
this.usageDescription)
}
}
func (this *IntValue) useDefaultWithViper() int {
if this.defaultValue != nil {
return *this.defaultValue
}
return invalidIntDefault
}
func (this *IntValue) Value() *int {
if this.actualValue != nil && *this.actualValue != invalidIntDefault {
return this.actualValue
}
return nil
}
func (this *IntValue) SetDefaultForConfigFile() {
viper.SetDefault(this.name, this.useDefaultWithViper())
}
func (this *IntValue) UpdateValueFromConfigFile() {
*this.actualValue = viper.GetInt(this.name)
}
//////////////////////////////////////////////////////////////////
func (this *ConfigValues) makeAndAddIntValue(name string, shorthand *string, defaultValue *int, usageDescription string) *IntValue {
result := makeIntValue(name, shorthand, defaultValue, usageDescription)
this.AddValue(result)
return result
}
func (this *ConfigValues) MakeIntValueWithShorthandAndDefault(name string, shorthand string, defaultValue int, usageDescription string) *IntValue {
return this.makeAndAddIntValue(name, &shorthand, &defaultValue, usageDescription)
}
func (this *ConfigValues) MakeIntValueWithShorthand(name string, shorthand string, usageDescription string) *IntValue {
return this.makeAndAddIntValue(name, &shorthand, nil, usageDescription)
}
func (this *ConfigValues) MakeIntValueWithDefault(name string, defaultValue int, usageDescription string) *IntValue {
return this.makeAndAddIntValue(name, nil, &defaultValue, usageDescription)
}
func (this *ConfigValues) MakeIntValue(name string, usageDescription string) *IntValue {
return this.makeAndAddIntValue(name, nil, nil, usageDescription)
}
func (this *ConfigValues) IntValue(name string) *int {
if this.allValues == nil {
return nil
}
configValue, valid := this.allValues[name]
if !valid || configValue == nil {
return nil
}
intConfigValue, valid := configValue.(*IntValue)
if !valid || intConfigValue == nil {
return nil
}
return intConfigValue.Value()
}