-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringValue.go
138 lines (99 loc) · 3.27 KB
/
stringValue.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
package goconfig
import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"reflect"
)
const (
invalidStringDefault = ""
)
// StringValue string Config Value type
type StringValue struct {
*valueInfo
defaultValue *string
actualValue *string
}
func makeStringValue(name string, shorthand *string, defaultValue *string, usageDescription string) *StringValue {
var exampleType string
valueInfo := makeValueInfo(name, shorthand, usageDescription, reflect.TypeOf(exampleType))
result := &StringValue{
valueInfo: valueInfo,
defaultValue: defaultValue,
}
result.Setup()
return result
}
func (this *StringValue) HasAValue() bool {
return this.Value() != nil
}
func (this *StringValue) HasADefaultValue() bool {
return this.defaultValue != nil && *this.defaultValue != invalidStringDefault
}
func (this *StringValue) DefaultValueAsString() *string {
if this.defaultValue != nil && *this.defaultValue != invalidStringDefault {
return this.defaultValue
}
return nil
}
func (this *StringValue) Setup() {
if this.shorthand != nil {
this.actualValue = pflag.StringP(this.name,
*this.shorthand,
this.useDefaultWithViper(),
this.usageDescription)
} else {
this.actualValue = pflag.String(this.name,
this.useDefaultWithViper(),
this.usageDescription)
}
}
func (this *StringValue) useDefaultWithViper() string {
if this.defaultValue != nil {
return *this.defaultValue
}
return invalidStringDefault
}
func (this *StringValue) Value() *string {
if this.actualValue != nil && *this.actualValue != invalidStringDefault {
return this.actualValue
}
return nil
}
func (this *StringValue) SetDefaultForConfigFile() {
viper.SetDefault(this.name, this.useDefaultWithViper())
}
func (this *StringValue) UpdateValueFromConfigFile() {
*this.actualValue = viper.GetString(this.name)
}
///////////////////////////////////////////////////////////////
func (this *ConfigValues) makeStringValueActual(name string, shorthand *string, defaultValue *string, usageDescription string) *StringValue {
result := makeStringValue(name, shorthand, defaultValue, usageDescription)
this.AddValue(result)
return result
}
func (this *ConfigValues) MakeStringValueWithShorthandAndDefault(name string, shorthand string, defaultValue string, usageDescription string) *StringValue {
return this.makeStringValueActual(name, &shorthand, &defaultValue, usageDescription)
}
func (this *ConfigValues) MakeStringValueWithShorthand(name string, shorthand string, usageDescription string) *StringValue {
return this.makeStringValueActual(name, &shorthand, nil, usageDescription)
}
func (this *ConfigValues) MakeStringValueWithDefault(name string, defaultValue string, usageDescription string) *StringValue {
return this.makeStringValueActual(name, nil, &defaultValue, usageDescription)
}
func (this *ConfigValues) MakeStringValue(name string, usageDescription string) *StringValue {
return this.makeStringValueActual(name, nil, nil, usageDescription)
}
func (this *ConfigValues) StringValue(name string) *string {
if this.allValues == nil {
return nil
}
configValue, valid := this.allValues[name]
if !valid || configValue == nil {
return nil
}
stringConfigValue, valid := configValue.(*StringValue)
if !valid || stringConfigValue == nil {
return nil
}
return stringConfigValue.Value()
}