-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
183 lines (156 loc) · 7.64 KB
/
tree.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package onlineconf
import (
"context"
"fmt"
)
// GetStringIfExists reads a string value of a named parameter from the module "TREE".
// It returns the boolean true if the parameter exists and is a string.
// In the other case it returns the boolean false and an empty string.
func GetStringIfExists(ctx context.Context, path string) (string, bool, error) {
return FromContext(ctx).GetStringIfExists(path)
}
// GetIntIfExists reads an integer value of a named parameter from the module "TREE".
// It returns this value and the boolean true if the parameter exists and is an integer.
// In the other case it returns the boolean false and 0.
func GetIntIfExists(ctx context.Context, path string) (int, bool, error) {
return FromContext(ctx).GetIntIfExists(path)
}
// GetBoolIfExists reads an bool value of a named parameter from the module "TREE".
// It returns this value and the boolean true if the parameter exists and is a bool.
// In the other case it returns the boolean false and 0.
func GetBoolIfExists(ctx context.Context, path string) (bool, bool, error) {
return FromContext(ctx).GetBoolIfExists(path)
}
// GetString reads a string value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is a string.
// In the other case it panics unless default value is provided in
// the second argument.
func GetString(ctx context.Context, path string, d ...string) (string, error) {
return FromContext(ctx).GetString(path, d...)
}
// GetInt reads an integer value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is an integer.
// In the other case it panics unless default value is provided in
// the second argument.
func GetInt(ctx context.Context, path string, d ...int) (int, error) {
return FromContext(ctx).GetInt(path, d...)
}
// GetBool reads an bool value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is a bool.
// In the other case it panics unless default value is provided in
// the second argument.
func GetBool(ctx context.Context, path string, d ...bool) (bool, error) {
return FromContext(ctx).GetBool(path, d...)
}
// GetStrings reads a []string value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is a comma-separated string
// or JSON array.
// In the other case it returns a default value provided in the second argument.
func GetStrings(ctx context.Context, path string, defaultValue []string) ([]string, error) {
return FromContext(ctx).GetStrings(path, defaultValue)
}
// GetStruct reads a structured value of a named parameter from the module "TREE".
// It stores this value in the value pointed by the value argument
// and returns true if the parameter exists and was unmarshaled successfully.
// In the case of error or if the parameter is not exists, the function doesn't
// touch the value argument, so you can safely pass a default value as the value
// argument and completely ignore return values of this function.
// A value is unmarshaled from JSON using json.Unmarshal and is cached internally
// until the configuration is updated, so be careful to not modify values returned by
// a reference.
// Experimental: this function can be modified or removed without any notice.
func GetStruct(ctx context.Context, path string, value interface{}) (bool, error) {
return FromContext(ctx).GetStruct(path, value)
}
// GetStringIfExists reads a string value of a named parameter from the module "TREE".
// It returns the boolean true if the parameter exists and is a string.
// In the other case it returns the boolean false and an empty string.
func (oi *OnlineconfInstance) GetStringIfExists(path string) (string, bool, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return "", false, fmt.Errorf("can't get TREE module: %w", err)
}
return m.GetStringIfExists(path)
}
// GetIntIfExists reads an integer value of a named parameter from the module "TREE".
// It returns this value and the boolean true if the parameter exists and is an integer.
// In the other case it returns the boolean false and 0.
func (oi *OnlineconfInstance) GetIntIfExists(path string) (int, bool, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return 0, false, fmt.Errorf("can't get TREE module: %w", err)
}
return m.GetIntIfExists(path)
}
// GetBoolIfExists reads an bool value of a named parameter from the module "TREE".
// It returns this value and the boolean true if the parameter exists and is a bool.
// In the other case it returns the boolean false and 0.
func (oi *OnlineconfInstance) GetBoolIfExists(path string) (bool, bool, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return false, false, fmt.Errorf("can't get TREE module: %w", err)
}
return m.GetBoolIfExists(path)
}
// GetString reads a string value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is a string.
// In the other case it panics unless default value is provided in
// the second argument.
func (oi *OnlineconfInstance) GetString(path string, d ...string) (string, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return "", fmt.Errorf("can't get TREE module: %w", err)
}
ret, err := m.GetString(path, d...)
return ret, err
}
// GetInt reads an integer value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is an integer.
// In the other case it panics unless default value is provided in
// the second argument.
func (oi *OnlineconfInstance) GetInt(path string, d ...int) (int, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return 0, fmt.Errorf("can't get TREE module: %w", err)
}
return m.GetInt(path, d...)
}
// GetBool reads an bool value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is a bool.
// In the other case it panics unless default value is provided in
// the second argument.
func (oi *OnlineconfInstance) GetBool(path string, d ...bool) (bool, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return false, fmt.Errorf("can't get TREE module: %w", err)
}
return m.GetBool(path, d...)
}
// GetStrings reads a []string value of a named parameter from the module "TREE".
// It returns this value if the parameter exists and is a comma-separated string
// or JSON array.
// In the other case it returns a default value provided in the second argument.
func (oi *OnlineconfInstance) GetStrings(path string, defaultValue []string) ([]string, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return nil, fmt.Errorf("can't get TREE module: %w", err)
}
return m.GetStrings(path, defaultValue)
}
// GetStruct reads a structured value of a named parameter from the module "TREE".
// It stores this value in the value pointed by the value argument
// and returns true if the parameter exists and was unmarshaled successfully.
// In the case of error or if the parameter is not exists, the function doesn't
// touch the value argument, so you can safely pass a default value as the value
// argument and completely ignore return values of this function.
// A value is unmarshaled from JSON using json.Unmarshal and is cached internally
// until the configuration is updated, so be careful to not modify values returned by
// a reference.
// Experimental: this function can be modified or removed without any notice.
func (oi *OnlineconfInstance) GetStruct(path string, value interface{}) (bool, error) {
m, err := oi.GetOrAddModule(DefaultModule)
if err != nil {
return false, fmt.Errorf("can't get TREE module: %w", err)
}
return m.GetStruct(path, value)
}