-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
42 lines (33 loc) · 909 Bytes
/
Copy pathutils.go
File metadata and controls
42 lines (33 loc) · 909 Bytes
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
package confuso
import (
"fmt"
"reflect"
"regexp"
)
func setField(fieldName string, field reflect.Value, value any) error {
val := reflect.ValueOf(value)
if !val.Type().AssignableTo(field.Type()) {
return fmt.Errorf("cannot assign value '%v' to field %s", value, fieldName)
}
field.Set(val)
return nil
}
func setOptionalField(fieldName string, optField reflect.Value, value any) error {
valueField := optField.FieldByName("Value")
okField := optField.FieldByName("Ok")
val := reflect.ValueOf(value)
if !val.Type().AssignableTo(valueField.Type()) {
return fmt.Errorf("cannot assign value '%v' to optional field %s", value, fieldName)
}
valueField.Set(val)
okField.SetBool(true)
return nil
}
func matchEnvVar(s string) string {
r := regexp.MustCompile("\\${([a-zA-Z_0-9]+)}")
submatches := r.FindStringSubmatch(s)
if len(submatches) >= 2 {
return submatches[1]
}
return ""
}