Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support defaultifempty on struct and array types #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/util/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func IsPointer(k reflect.Kind) bool {

func IsEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
case reflect.Array:
return v.IsZero()
case reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
Expand All @@ -76,6 +78,8 @@ func IsEmptyValue(v reflect.Value) bool {
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
case reflect.Struct:
return v.IsZero()
case reflect.Invalid:
return true
default:
Expand Down
41 changes: 22 additions & 19 deletions internal/util/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,26 @@ type (
str string
}
TestStruct struct {
arr [0]string
slc []string
mp map[string]interface{}
str string
bl bool
i int
i8 int8
i16 int16
i32 int32
i64 int64
ui uint
ui8 uint8
ui16 uint16
ui32 uint32
ui64 uint64
f32 float32
f64 float64
intr TestInterface
ptr *sql.NullString
arr [1]string
slc []string
mp map[string]interface{}
str string
bl bool
i int
i8 int8
i16 int16
i32 int32
i64 int64
ui uint
ui8 uint8
ui16 uint16
ui32 uint32
ui64 uint64
f32 float32
f64 float64
intr TestInterface
ptr *sql.NullString
nullable sql.NullString
}
)

Expand Down Expand Up @@ -342,6 +343,7 @@ func (rt *reflectTest) TestIsEmptyValue_emptyValues() {
rt.True(util.IsEmptyValue(reflect.ValueOf(ts.f64)))
rt.True(util.IsEmptyValue(reflect.ValueOf(ts.intr)))
rt.True(util.IsEmptyValue(reflect.ValueOf(ts.ptr)))
rt.True(util.IsEmptyValue(reflect.ValueOf(ts.nullable)))
}

func (rt *reflectTest) TestIsEmptyValue_validValues() {
Expand All @@ -365,6 +367,7 @@ func (rt *reflectTest) TestIsEmptyValue_validValues() {
rt.False(util.IsEmptyValue(reflect.ValueOf(float64(0.2))))
rt.False(util.IsEmptyValue(reflect.ValueOf(ts.intr)))
rt.False(util.IsEmptyValue(reflect.ValueOf(&TestStruct{str: "a"})))
rt.False(util.IsEmptyValue(reflect.ValueOf(sql.NullString{Valid: true})))
}

func (rt *reflectTest) TestColumnRename() {
Expand Down