fix http header binding failure bug #885 (#887)

This commit is contained in:
voidint
2021-08-10 17:38:03 +08:00
committed by GitHub
parent 872e75e10d
commit 28a7c9d38f
3 changed files with 30 additions and 8 deletions

View File

@@ -43,7 +43,8 @@ type (
UnmarshalOption func(*unmarshalOptions)
unmarshalOptions struct {
fromString bool
fromString bool
canonicalKey func(key string) string
}
keyCache map[string][]string
@@ -321,9 +322,12 @@ func (u *Unmarshaler) processNamedField(field reflect.StructField, value reflect
if err != nil {
return err
}
k := key
if u.opts.canonicalKey != nil {
k = u.opts.canonicalKey(key)
}
fullName = join(fullName, key)
mapValue, hasValue := getValue(m, key)
mapValue, hasValue := getValue(m, k)
if hasValue {
return u.processNamedFieldWithValue(field, value, mapValue, key, opts, fullName)
}
@@ -621,6 +625,12 @@ func WithStringValues() UnmarshalOption {
}
}
func WithCanonicalKeyFunc(f func(string) string) UnmarshalOption {
return func(opt *unmarshalOptions) {
opt.canonicalKey = f
}
}
func fillDurationValue(fieldKind reflect.Kind, value reflect.Value, dur string) error {
d, err := time.ParseDuration(dur)
if err != nil {