Skip to content

Commit

Permalink
Add default tag (#183)
Browse files Browse the repository at this point in the history
**Summary of Changes**

this is the implementation for the functionality described in #182 

1. add support for an optional `default` tag to allow setting values
when decoding in case no value is provided
2. limiting the `default` tag scope to only primitive types and their
pointers as well
  • Loading branch information
zak905 authored Feb 10, 2024
1 parent 4890efc commit eda9c33
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (c *cache) createField(field reflect.StructField, parentAlias string) *fiel
isSliceOfStructs: isSlice && isStruct,
isAnonymous: field.Anonymous,
isRequired: options.Contains("required"),
defaultValue: options.getDefaultOptionValue(),
}
}

Expand Down Expand Up @@ -246,8 +247,9 @@ type fieldInfo struct {
// isSliceOfStructs indicates if the field type is a slice of structs.
isSliceOfStructs bool
// isAnonymous indicates whether the field is embedded in the struct.
isAnonymous bool
isRequired bool
isAnonymous bool
isRequired bool
defaultValue string
}

func (f *fieldInfo) paths(prefix string) []string {
Expand Down Expand Up @@ -303,3 +305,13 @@ func (o tagOptions) Contains(option string) bool {
}
return false
}

func (o tagOptions) getDefaultOptionValue() string {
for _, s := range o {
if strings.HasPrefix(s, "default:") {
return strings.Split(s, ":")[1]
}
}

return ""
}
77 changes: 77 additions & 0 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,80 @@ func convertUint64(value string) reflect.Value {
}
return invalidValue
}

func convertPointer(k reflect.Kind, value string) reflect.Value {
switch k {
case boolType:
if v := convertBool(value); v.IsValid() {
converted := v.Bool()
return reflect.ValueOf(&converted)
}
case float32Type:
if v := convertFloat32(value); v.IsValid() {
converted := float32(v.Float())
return reflect.ValueOf(&converted)
}
case float64Type:
if v := convertFloat64(value); v.IsValid() {
converted := float64(v.Float())
return reflect.ValueOf(&converted)
}
case intType:
if v := convertInt(value); v.IsValid() {
converted := int(v.Int())
return reflect.ValueOf(&converted)
}
case int8Type:
if v := convertInt8(value); v.IsValid() {
converted := int8(v.Int())
return reflect.ValueOf(&converted)
}
case int16Type:
if v := convertInt16(value); v.IsValid() {
converted := int16(v.Int())
return reflect.ValueOf(&converted)
}
case int32Type:
if v := convertInt32(value); v.IsValid() {
converted := int32(v.Int())
return reflect.ValueOf(&converted)
}
case int64Type:
if v := convertInt64(value); v.IsValid() {
converted := int64(v.Int())
return reflect.ValueOf(&converted)
}
case stringType:
if v := convertString(value); v.IsValid() {
converted := v.String()
return reflect.ValueOf(&converted)
}
case uintType:
if v := convertUint(value); v.IsValid() {
converted := uint(v.Uint())
return reflect.ValueOf(&converted)
}
case uint8Type:
if v := convertUint8(value); v.IsValid() {
converted := uint8(v.Uint())
return reflect.ValueOf(&converted)
}
case uint16Type:
if v := convertUint16(value); v.IsValid() {
converted := uint16(v.Uint())
return reflect.ValueOf(&converted)
}
case uint32Type:
if v := convertUint32(value); v.IsValid() {
converted := uint32(v.Uint())
return reflect.ValueOf(&converted)
}
case uint64Type:
if v := convertUint64(value); v.IsValid() {
converted := uint64(v.Uint())
return reflect.ValueOf(&converted)
}
}

return invalidValue
}
71 changes: 71 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,84 @@ 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
}
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.
Expand Down
Loading

0 comments on commit eda9c33

Please sign in to comment.