-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
186 lines (171 loc) · 4.64 KB
/
parser.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
package tinyconf
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/insei/fmap/v3"
)
type Registered struct {
Storage fmap.Storage
Config any
}
type Manager struct {
drivers []Driver
log Logger
registered map[reflect.Type]*Registered
}
func checkConfig(conf any) error {
valOf := reflect.ValueOf(conf)
if !valOf.IsValid() {
return errors.New("config is not a correct value")
}
if !valOf.IsValid() || valOf.Kind() != reflect.Ptr {
return errors.New("config can be only valid pointer")
}
valOf = valOf.Elem()
if !valOf.IsValid() || valOf.Kind() != reflect.Struct {
return errors.New("config must be a struct pointer")
}
return nil
}
func (c *Manager) Register(conf any) error {
err := checkConfig(conf)
if err != nil {
return fmt.Errorf("config can't be registred: %w", err)
}
storage, err := fmap.GetFrom(conf)
if err != nil || storage == nil {
return fmt.Errorf("config can't be registred: %w", err)
}
c.registered[reflect.TypeOf(conf)] = &Registered{
Storage: storage,
Config: conf,
}
return nil
}
func getDereferencedValue(val any) any {
valOf := reflect.ValueOf(val)
for valOf.IsValid() && valOf.Kind() == reflect.Ptr {
valOf = valOf.Elem()
}
if valOf.IsValid() && valOf.CanInterface() {
return valOf.Interface()
}
return val
}
func getLoggerValue(field fmap.Field, val any) string {
derefDriverValue := getDereferencedValue(val)
valueLog := fmt.Sprintf("%v", derefDriverValue)
if hidden, ok := field.GetTag().Lookup("hidden"); ok && hidden == "true" {
valueLog = strings.Repeat("*", len(fmt.Sprintf("%s", valueLog)))
}
return valueLog
}
func copyToSubConfig(conf, subConf any, subpath string) error {
confFields, err := fmap.GetFrom(conf)
if err != nil {
return err
}
subConfFields, err := fmap.GetFrom(subConf)
if err != nil {
return err
}
for _, path := range confFields.GetAllPaths() {
if path != subpath && strings.HasPrefix(path, subpath) {
field := confFields.MustFind(path)
subFieldPath := strings.Replace(path, subpath+".", "", -1)
subField, ok := subConfFields.Find(subFieldPath)
if !ok {
return fmt.Errorf("subconf field %s not found", path)
}
subField.Set(subConf, field.Get(conf))
}
}
return nil
}
func (c *Manager) Parse(conf any) (err error) {
confParse := conf
confTypeOf := reflect.TypeOf(conf)
register, ok := c.registered[confTypeOf]
if !ok {
for registeredTypeOf, registeredConf := range c.registered {
for _, path := range registeredConf.Storage.GetAllPaths() {
field := registeredConf.Storage.MustFind(path)
fieldType := field.GetDereferencedType()
if fieldType.Kind() == reflect.Struct &&
reflect.PointerTo(fieldType) == confTypeOf {
register = registeredConf
confParse = reflect.New(registeredTypeOf.Elem()).Interface()
defer func() {
err = copyToSubConfig(confParse, conf, field.GetStructPath())
}()
break
}
}
}
}
if register == nil {
return ErrNotRegisteredConfig
}
for _, d := range c.drivers {
for _, path := range register.Storage.GetAllPaths() {
field := register.Storage.MustFind(path)
if field.GetType().Kind() == reflect.Struct {
continue
}
log := c.log.With(
LogField("config", confTypeOf.String()),
LogField("driver", d.GetName()),
LogField("field", path))
driverValue, err := d.GetValue(field)
switch {
case errors.Is(err, ErrIncorrectTagSettings):
log.Warn("ignore", LogField("details", err.Error()))
case errors.Is(err, ErrValueNotFound):
log.Debug("skip", LogField("details", err.Error()))
case err != nil &&
!errors.Is(err, ErrValueNotFound) &&
!errors.Is(err, ErrIncorrectTagSettings):
c.log.Error("failed", LogField("details", err.Error()))
case err == nil:
currentValue := field.Get(confParse)
if currentValue != driverValue.Value {
log.Debug("override", LogField("value", getLoggerValue(field, driverValue.Value)))
field.Set(confParse, driverValue.Value)
}
}
}
}
return nil
}
func (c *Manager) GenDoc(driverName string) string {
var registers []*Registered
for _, register := range c.registered {
registers = append(registers, register)
}
var doc string
for _, driver := range c.drivers {
if driverName == driver.GetName() {
doc = driver.GenDoc(registers...)
}
}
return doc
}
func New(opts ...Option) (*Manager, error) {
m := &Manager{log: &noopLogger{}, registered: map[reflect.Type]*Registered{}}
count := countDrivers(opts...)
if count > 0 {
m.drivers = make([]Driver, 0, count)
}
for i, _ := range opts {
//switch case for minimize heap allocations
switch opt := opts[i].(type) {
case driverOption:
opt.apply(m)
case loggerOption:
opt.apply(m)
}
}
return m, nil
}