Update dependencies and build to go1.22 (#2113)
* Update dependencies and build to go1.22 * Fix api changes wrt to dependencies * Update golangci config
This commit is contained in:
73
vendor/github.com/gorilla/schema/decoder.go
generated
vendored
73
vendor/github.com/gorilla/schema/decoder.go
generated
vendored
@@ -84,6 +84,7 @@ func (d *Decoder) Decode(dst interface{}, src map[string][]string) error {
|
||||
errors[path] = UnknownKeyError{Key: path}
|
||||
}
|
||||
}
|
||||
errors.merge(d.setDefaults(t, v))
|
||||
errors.merge(d.checkRequired(t, src))
|
||||
if len(errors) > 0 {
|
||||
return errors
|
||||
@@ -91,6 +92,76 @@ func (d *Decoder) Decode(dst interface{}, src map[string][]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//setDefaults sets the default values when the `default` tag is specified,
|
||||
//default is supported on basic/primitive types and their pointers,
|
||||
//nested structs can also have default tags
|
||||
func (d *Decoder) setDefaults(t reflect.Type, v reflect.Value) MultiError {
|
||||
struc := d.cache.get(t)
|
||||
if struc == nil {
|
||||
// unexpect, cache.get never return nil
|
||||
return MultiError{"default-" + t.Name(): errors.New("cache fail")}
|
||||
}
|
||||
|
||||
errs := MultiError{}
|
||||
|
||||
for _, f := range struc.fields {
|
||||
vCurrent := v.FieldByName(f.name)
|
||||
|
||||
if vCurrent.Type().Kind() == reflect.Struct && f.defaultValue == "" {
|
||||
errs.merge(d.setDefaults(vCurrent.Type(), vCurrent))
|
||||
} else if isPointerToStruct(vCurrent) && f.defaultValue == "" {
|
||||
errs.merge(d.setDefaults(vCurrent.Elem().Type(), vCurrent.Elem()))
|
||||
}
|
||||
|
||||
if f.defaultValue != "" && f.isRequired {
|
||||
errs.merge(MultiError{"default-" + f.name: errors.New("required fields cannot have a default value")})
|
||||
} else if f.defaultValue != "" && vCurrent.IsZero() && !f.isRequired {
|
||||
if f.typ.Kind() == reflect.Struct {
|
||||
errs.merge(MultiError{"default-" + f.name: errors.New("default option is supported only on: bool, float variants, string, unit variants types or their corresponding pointers or slices")})
|
||||
} else if f.typ.Kind() == reflect.Slice {
|
||||
vals := strings.Split(f.defaultValue, "|")
|
||||
|
||||
//check if slice has one of the supported types for defaults
|
||||
if _, ok := builtinConverters[f.typ.Elem().Kind()]; !ok {
|
||||
errs.merge(MultiError{"default-" + f.name: errors.New("default option is supported only on: bool, float variants, string, unit variants types or their corresponding pointers or slices")})
|
||||
continue
|
||||
}
|
||||
|
||||
defaultSlice := reflect.MakeSlice(f.typ, 0, cap(vals))
|
||||
for _, val := range vals {
|
||||
//this check is to handle if the wrong value is provided
|
||||
if convertedVal := builtinConverters[f.typ.Elem().Kind()](val); convertedVal.IsValid() {
|
||||
defaultSlice = reflect.Append(defaultSlice, convertedVal)
|
||||
}
|
||||
}
|
||||
vCurrent.Set(defaultSlice)
|
||||
} else if f.typ.Kind() == reflect.Ptr {
|
||||
t1 := f.typ.Elem()
|
||||
|
||||
if t1.Kind() == reflect.Struct || t1.Kind() == reflect.Slice {
|
||||
errs.merge(MultiError{"default-" + f.name: errors.New("default option is supported only on: bool, float variants, string, unit variants types or their corresponding pointers or slices")})
|
||||
}
|
||||
|
||||
//this check is to handle if the wrong value is provided
|
||||
if convertedVal := convertPointer(t1.Kind(), f.defaultValue); convertedVal.IsValid() {
|
||||
vCurrent.Set(convertedVal)
|
||||
}
|
||||
} else {
|
||||
//this check is to handle if the wrong value is provided
|
||||
if convertedVal := builtinConverters[f.typ.Kind()](f.defaultValue); convertedVal.IsValid() {
|
||||
vCurrent.Set(builtinConverters[f.typ.Kind()](f.defaultValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func isPointerToStruct(v reflect.Value) bool {
|
||||
return !v.IsZero() && v.Type().Kind() == reflect.Ptr && v.Elem().Type().Kind() == reflect.Struct
|
||||
}
|
||||
|
||||
// checkRequired checks whether required fields are empty
|
||||
//
|
||||
// check type t recursively if t has struct fields.
|
||||
@@ -193,7 +264,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
|
||||
if v.Type().Kind() == reflect.Struct {
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
if field.Type().Kind() == reflect.Ptr && field.IsNil() && v.Type().Field(i).Anonymous == true {
|
||||
if field.Type().Kind() == reflect.Ptr && field.IsNil() && v.Type().Field(i).Anonymous {
|
||||
field.Set(reflect.New(field.Type().Elem()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user