-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
83 lines (73 loc) · 1.54 KB
/
tag.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
package envconf
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
)
type tag struct {
name string
value string
defvalue string
required bool
}
func newTag(c *Config, s string, v reflect.Type) (*tag, error) {
var t = &tag{}
a := strings.Split(s, ",")
switch len(a) {
case 3:
t.required = a[2] == "true"
fallthrough
case 2:
t.setDefvalue(c.separator, a[1], v)
fallthrough
case 1:
t.setName(c.prefix, a[0])
case 0:
return nil, errors.New("empty tag")
default:
return nil, errors.New("invalid tag value")
}
if err := t.isValid(); err != nil {
return nil, err
}
if err := t.getValue(); err != nil {
return nil, err
}
if t.value == "" {
t.value = t.defvalue
}
return t, nil
}
func (t *tag) setName(prefix, name string) {
if prefix == "" {
t.name = strings.ToUpper(name)
} else {
t.name = strings.ToUpper(prefix + "_" + name)
}
}
func (t *tag) setDefvalue(separator, value string, v reflect.Type) {
t.defvalue = value
if v.Kind() == reflect.Array || v.Kind() == reflect.Slice {
// Replace all ; to config separator so decoder could parse it
t.defvalue = strings.Replace(t.defvalue, ";", separator, -1)
}
}
func (t *tag) isValid() error {
if t.required && t.name == "" {
return fmt.Errorf("missing name")
}
if t.required && t.defvalue != "" {
return fmt.Errorf("required not allowd with default value")
}
return nil
}
func (t *tag) getValue() error {
s, b := os.LookupEnv(t.name)
if t.required && !b {
return fmt.Errorf("env %s not found, but required", t.name)
}
t.value = s
return nil
}