Skip to content

Commit

Permalink
Prevent empty slice init
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectrumQT committed Jan 28, 2019
1 parent 5c5d0f3 commit 1879333
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func setField(field reflect.Value, defaultVal string) error {
return nil
}

if !shouldInitializeField(field.Kind(), defaultVal) {
if !shouldInitializeField(field, defaultVal) {
return nil
}

Expand Down Expand Up @@ -172,12 +172,12 @@ func isInitialValue(field reflect.Value) bool {
return reflect.DeepEqual(reflect.Zero(field.Type()).Interface(), field.Interface())
}

func shouldInitializeField(kind reflect.Kind, tag string) bool {
switch kind {
func shouldInitializeField(field reflect.Value, tag string) bool {
switch field.Kind() {
case reflect.Struct:
return true
case reflect.Slice:
return true
return field.Len() > 0 || tag != ""
}

return (tag != "")
Expand Down

0 comments on commit 1879333

Please sign in to comment.