-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenv.go
225 lines (204 loc) · 5.22 KB
/
env.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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrp
import (
"encoding/base64"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
func toEnvMap(msg any) map[string]string {
v := reflect.ValueOf(msg)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
return nil
}
v = v.Elem()
t := v.Type()
envVars := make(map[string]string)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
value := v.Field(i)
tag := field.Tag.Get("env")
if tag == "" || tag == "-" {
continue
}
tagParts := strings.Split(tag, ",")
key := tagParts[0]
options := tagParts[1:]
if key == "" {
continue
}
if isEmptyValue(value) {
if contains(options, "omitempty") {
continue
}
envVars[key] = ""
}
switch value.Kind() {
case reflect.String:
envVars[key] = value.String()
case reflect.Ptr:
if value.Elem().Kind() == reflect.Int64 {
if !value.IsNil() {
envVars[key] = fmt.Sprintf("%d", value.Elem().Int())
}
}
case reflect.Slice:
if value.Type().Elem().Kind() == reflect.String {
if contains(options, "multiline") {
for i, s := range value.Interface().([]string) {
envVars[fmt.Sprintf("%s_%03d", key, i)] = s
}
} else {
envVars[key] = strings.Join(value.Interface().([]string), ",")
}
} else if value.Type().Elem().Kind() == reflect.Uint8 {
envVars[key] = base64.StdEncoding.EncodeToString(value.Interface().([]byte))
}
case reflect.Map:
if value.Type().Key().Kind() == reflect.String && value.Type().Elem().Kind() == reflect.String {
for k, v := range value.Interface().(map[string]string) {
safe := sanitizeEnvVarName(k)
envVars[fmt.Sprintf("%s_%s", key, safe)] = k + "=" + v
}
}
case reflect.Int, reflect.Int64:
envVars[key] = fmt.Sprintf("%d", value.Int())
}
}
return envVars
}
func fromEnvMap(envVars []string, msg any) error {
v := reflect.ValueOf(msg)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
return fmt.Errorf("msg must be a pointer to a struct")
}
v = v.Elem()
t := v.Type()
envMap := make(map[string]string)
for _, envVar := range envVars {
parts := strings.SplitN(envVar, "=", 2)
if len(parts) == 2 {
envMap[parts[0]] = parts[1]
}
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
value := v.Field(i)
tag := field.Tag.Get("env")
if tag == "" || tag == "-" {
continue
}
tagParts := strings.Split(tag, ",")
key := tagParts[0]
options := tagParts[1:]
if key == "" {
key = field.Name
}
switch value.Kind() {
case reflect.String:
value.SetString(envMap[key])
case reflect.Ptr:
if value.Type().Elem().Kind() == reflect.Int64 {
if v, exists := envMap[key]; !exists || v == "" {
continue
}
intVal, err := strconv.ParseInt(envMap[key], 10, 64)
if err != nil {
return err
}
value.Set(reflect.ValueOf(&intVal))
}
case reflect.Slice:
if value.Type().Elem().Kind() == reflect.String {
if contains(options, "multiline") {
var slice []string
for i := 0; ; i++ {
multiKey := fmt.Sprintf("%s_%03d", key, i)
if multiValue, ok := envMap[multiKey]; ok {
slice = append(slice, multiValue)
} else {
break
}
}
if len(slice) > 0 {
value.Set(reflect.ValueOf(slice))
}
} else {
if _, exists := envMap[key]; !exists {
continue
}
list := strings.Split(envMap[key], ",")
if len(list) > 0 && list[0] != "" {
value.Set(reflect.ValueOf(list))
}
}
} else if value.Type().Elem().Kind() == reflect.Uint8 {
if _, exists := envMap[key]; !exists {
continue
}
decoded, err := base64.StdEncoding.DecodeString(envMap[key])
if err != nil {
return err
}
if len(decoded) > 0 {
value.Set(reflect.ValueOf(decoded))
}
}
case reflect.Map:
if value.Type().Key().Kind() == reflect.String && value.Type().Elem().Kind() == reflect.String {
mapValue := make(map[string]string)
for k, v := range envMap {
if strings.HasPrefix(k, key) {
list := strings.SplitN(v, "=", 2)
for i := range list {
list[i] = strings.TrimSpace(list[i])
}
// If there is no value, append an empty string
list = append(list, "")
mapValue[list[0]] = list[1]
}
}
if len(mapValue) > 0 {
value.Set(reflect.ValueOf(mapValue))
}
}
case reflect.Int, reflect.Int64:
if v, exists := envMap[key]; !exists || v == "" {
continue
}
intVal, err := strconv.ParseInt(envMap[key], 10, 64)
if err != nil {
return err
}
value.SetInt(intVal)
}
}
return nil
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.String, reflect.Slice, reflect.Map:
return v.Len() == 0
case reflect.Ptr, reflect.Interface:
return v.IsNil()
}
return false
}
// Allowed environment variable characters.
var envVarNameRegexp = regexp.MustCompile(`[^a-zA-Z0-9_]`)
func sanitizeEnvVarName(name string) string {
name = envVarNameRegexp.ReplaceAllString(name, "_")
// Chomp off leading underscores.
return strings.TrimLeft(name, "_")
}