-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding.go
More file actions
224 lines (200 loc) · 5.81 KB
/
Copy pathbinding.go
File metadata and controls
224 lines (200 loc) · 5.81 KB
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
// Copyright 2026 The Gopherly 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 synthra
import (
"fmt"
"reflect"
"sync"
"time"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/cast"
)
// defaultsCache caches whether a struct type has any `default` tags,
// avoiding repeated reflection scans on repeated Load calls.
var defaultsCache sync.Map // map[reflect.Type]bool
// Validator is implemented by bound structs that check themselves after
// decode. The validation package uses the same contract
// (validation.Validator); a type implementing either satisfies both.
type Validator interface {
Validate() error
}
// applyDefaults applies default values from struct tags to a struct.
// It walks through the struct fields and sets defaults for fields that
// have the 'default' tag and are currently zero-valued.
func applyDefaults(target any) error {
val := reflect.ValueOf(target)
if val.Kind() != reflect.Pointer {
return fmt.Errorf("target must be a pointer")
}
val = val.Elem()
if val.Kind() != reflect.Struct {
return fmt.Errorf("target must be a pointer to a struct")
}
return setDefaults(val)
}
// setDefaults recursively sets default values on a struct.
func setDefaults(val reflect.Value) error {
typ := val.Type()
if !structHasDefaults(typ) {
return nil
}
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldType := typ.Field(i)
if !field.CanSet() {
continue
}
switch field.Kind() {
case reflect.Struct:
if err := setDefaults(field); err != nil {
return err
}
continue
case reflect.Pointer:
if field.Type().Elem().Kind() == reflect.Struct {
if field.IsNil() {
if structHasDefaults(field.Type().Elem()) {
field.Set(reflect.New(field.Type().Elem()))
} else {
continue
}
}
if err := setDefaults(field.Elem()); err != nil {
return err
}
continue
}
}
defaultTag := fieldType.Tag.Get("default")
if defaultTag == "" {
continue
}
if !field.IsZero() {
continue
}
if err := setDefaultValue(field, defaultTag); err != nil {
return fmt.Errorf("failed to set default for field %s: %w", fieldType.Name, err)
}
}
return nil
}
// structHasDefaults reports whether typ (must be a struct type) or any of its
// nested structs carry at least one `default` tag. Results are cached.
func structHasDefaults(typ reflect.Type) bool {
if v, loaded := defaultsCache.Load(typ); loaded {
if has, ok := v.(bool); ok {
return has
}
}
has := scanDefaults(typ)
defaultsCache.Store(typ, has)
return has
}
func scanDefaults(typ reflect.Type) bool {
for f := range typ.Fields() {
if !f.IsExported() {
continue
}
if f.Tag.Get("default") != "" {
return true
}
ft := f.Type
if ft.Kind() == reflect.Pointer {
ft = ft.Elem()
}
if ft.Kind() == reflect.Struct && structHasDefaults(ft) {
return true
}
}
return false
}
// setDefaultValue sets a default value on a field based on its type.
func setDefaultValue(field reflect.Value, defaultVal string) error {
switch field.Kind() {
case reflect.String:
field.SetString(defaultVal)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// Special handling for time.Duration
if field.Type() == reflect.TypeFor[time.Duration]() {
d, err := time.ParseDuration(defaultVal)
if err != nil {
return err
}
field.SetInt(int64(d))
} else {
i, err := cast.ToInt64E(defaultVal)
if err != nil {
return err
}
field.SetInt(i)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u, err := cast.ToUint64E(defaultVal)
if err != nil {
return err
}
field.SetUint(u)
case reflect.Float32, reflect.Float64:
f, err := cast.ToFloat64E(defaultVal)
if err != nil {
return err
}
field.SetFloat(f)
case reflect.Bool:
b, err := cast.ToBoolE(defaultVal)
if err != nil {
return err
}
field.SetBool(b)
default:
return fmt.Errorf("unsupported type for default tag: %s", field.Kind())
}
return nil
}
// decodeBindingInto decodes values into target using mapstructure.
//
// A fresh DecoderConfig is built on every call because mapstructure documents
// that a config must not be reused after NewDecoder returns. The DecodeHook
// composition cost is negligible compared to the decode itself.
//
// mapstructure's default field-matching function is [strings.EqualFold], so
// it is already case-insensitive. Keys canonicalized by canonicalizeSchemaKeys
// (or preserved as-is when no schema is present) flow naturally into struct
// fields regardless of casing differences between the config source and the
// struct tag.
func (c *Synthra) decodeBindingInto(target, values any) error {
tagName := c.tagName
if tagName == "" {
tagName = "synthra"
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: tagName,
Result: target,
Squash: true,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
mapstructure.StringToTimeHookFunc(time.RFC3339),
mapstructure.StringToURLHookFunc(),
),
})
if err != nil {
return fmt.Errorf("failed to create decoder: %w", err)
}
if err = decoder.Decode(values); err != nil {
return fmt.Errorf("failed to decode configuration: %w", err)
}
return nil
}