-
Notifications
You must be signed in to change notification settings - Fork 329
/
parse.go
267 lines (236 loc) · 7.29 KB
/
parse.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configmap
import (
"fmt"
"strconv"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
)
// ParseFunc is a function taking ConfigMap data and applying a parse operation to it.
type ParseFunc func(map[string]string) error
// AsString passes the value at key through into the target, if it exists.
func AsString(key string, target *string) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
*target = raw
}
return nil
}
}
// AsBool parses the value at key as a boolean into the target, if it exists.
func AsBool(key string, target *bool) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseBool(raw)
*target = val // If err != nil — this is always false.
return err
}
return nil
}
}
// AsInt16 parses the value at key as an int16 into the target, if it exists.
func AsInt16(key string, target *int16) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseInt(raw, 10, 16)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = int16(val)
}
return nil
}
}
// AsInt32 parses the value at key as an int32 into the target, if it exists.
func AsInt32(key string, target *int32) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseInt(raw, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = int32(val)
}
return nil
}
}
// AsInt64 parses the value at key as an int64 into the target, if it exists.
func AsInt64(key string, target *int64) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = val
}
return nil
}
}
// AsInt parses the value at key as an int into the target, if it exists.
func AsInt(key string, target *int) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.Atoi(raw)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = val
}
return nil
}
}
// AsUint16 parses the value at key as an uint16 into the target, if it exists.
func AsUint16(key string, target *uint16) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseUint(raw, 10, 16)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = uint16(val)
}
return nil
}
}
// AsUint32 parses the value at key as an uint32 into the target, if it exists.
func AsUint32(key string, target *uint32) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseUint(raw, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = uint32(val)
}
return nil
}
}
// AsFloat64 parses the value at key as a float64 into the target, if it exists.
func AsFloat64(key string, target *float64) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseFloat(raw, 64)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = val
}
return nil
}
}
// AsDuration parses the value at key as a time.Duration into the target, if it exists.
func AsDuration(key string, target *time.Duration) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := time.ParseDuration(raw)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = val
}
return nil
}
}
// AsStringSet parses the value at key as a sets.Set[string] (split by ',') into the target, if it exists.
func AsStringSet(key string, target *sets.Set[string]) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
splitted := strings.Split(raw, ",")
for i, v := range splitted {
splitted[i] = strings.TrimSpace(v)
}
*target = sets.New[string](splitted...)
}
return nil
}
}
// AsQuantity parses the value at key as a *resource.Quantity into the target, if it exists
func AsQuantity(key string, target **resource.Quantity) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := resource.ParseQuantity(raw)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", key, err)
}
*target = &val
}
return nil
}
}
// AsOptionalNamespacedName parses the value at key as a types.NamespacedName into the target, if it exists
// The namespace and name are both required and expected to be valid DNS labels
func AsOptionalNamespacedName(key string, target **types.NamespacedName) ParseFunc {
return func(data map[string]string) error {
if _, ok := data[key]; !ok {
return nil
}
*target = &types.NamespacedName{}
return AsNamespacedName(key, *target)(data)
}
}
// AsNamespacedName parses the value at key as a types.NamespacedName into the target, if it exists
// The namespace and name are both required and expected to be valid DNS labels
func AsNamespacedName(key string, target *types.NamespacedName) ParseFunc {
return func(data map[string]string) error {
raw, ok := data[key]
if !ok {
return nil
}
v := strings.SplitN(raw, string(types.Separator), 3)
if len(v) != 2 {
return fmt.Errorf("failed to parse %q: expected 'namespace/name' format", key)
}
for _, val := range v {
if errs := validation.ValidateNamespaceName(val, false); len(errs) > 0 {
return fmt.Errorf("failed to parse %q: %s", key, strings.Join(errs, ", "))
}
}
target.Namespace = v[0]
target.Name = v[1]
return nil
}
}
// Parse parses the given map using the parser functions passed in.
func Parse(data map[string]string, parsers ...ParseFunc) error {
for _, parse := range parsers {
if err := parse(data); err != nil {
return err
}
}
return nil
}
// CollectMapEntriesWithPrefix parses the data into the target as a map[string]string, if it exists.
// The map is represented as a list of key-value pairs with a common prefix.
func CollectMapEntriesWithPrefix(prefix string, target *map[string]string) ParseFunc {
if target == nil {
panic("target cannot be nil")
}
return func(data map[string]string) error {
for k, v := range data {
if strings.HasPrefix(k, prefix) && len(k) > len(prefix)+1 {
if *target == nil {
m := make(map[string]string, 2)
*target = m
}
(*target)[k[len(prefix)+1: /* remove dot `.` */]] = v
}
}
return nil
}
}