Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(BUX-000): isNilCondition to isEmptyCondition with unit tests #546

Merged
merged 1 commit into from
Apr 9, 2024
Merged
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
17 changes: 10 additions & 7 deletions engine/datastore/where_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (builder *whereBuilder) applyJSONArrayContains(tx customWhereInterface, key

// applyJSONCondition will apply condition on JSON Object field - client.GetObjectFields()
func (builder *whereBuilder) applyJSONCondition(tx customWhereInterface, key string, condition interface{}) {
if isNilCondition(condition) {
if isEmptyCondition(condition) {
return
}

Expand Down Expand Up @@ -228,14 +228,17 @@ func convertToDict(object interface{}) map[string]interface{} {
return converted
}

func isNilCondition(condition interface{}) bool {
func isEmptyCondition(condition interface{}) bool {
val := reflect.ValueOf(condition)
if val.IsNil() {
return true
for ; val.Kind() == reflect.Ptr; val = val.Elem() {
if val.IsNil() {
return true
}
}
if val.Kind() == reflect.Ptr {
val = val.Elem()
return val.IsNil()
kind := val.Kind()
if kind == reflect.Map || kind == reflect.Slice {
return val.Len() == 0
}

return false
}
61 changes: 61 additions & 0 deletions engine/datastore/where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,64 @@ func Test_sqlInjectionSafety(t *testing.T) {
assert.Contains(t, raw, `'{"1=1; DELETE FROM users":"field_value"}'`)
})
}

func Test_isEmptyCondition(t *testing.T) {
t.Parallel()

t.Run("nil ptr to map", func(t *testing.T) {
var condition *map[string]interface{}
assert.True(t, isEmptyCondition(condition))
})

t.Run("not-nil ptr to nil-map", func(t *testing.T) {
var theMap map[string]interface{}
condition := &theMap
assert.True(t, isEmptyCondition(condition))
})

t.Run("not-nil ptr to empty map", func(t *testing.T) {
theMap := map[string]interface{}{}
condition := &theMap
assert.True(t, isEmptyCondition(condition))
})

t.Run("not-nil ptr to not-empty map", func(t *testing.T) {
theMap := map[string]interface{}{
"key": 123,
}
condition := &theMap
assert.False(t, isEmptyCondition(condition))
})

t.Run("nil ptr to int", func(t *testing.T) {
var condition *int
assert.True(t, isEmptyCondition(condition))
})

t.Run("not nil ptr to int", func(t *testing.T) {
theInt := 123
condition := &theInt
assert.False(t, isEmptyCondition(condition))
})

t.Run("just int", func(t *testing.T) {
assert.False(t, isEmptyCondition(123))
})

t.Run("nil ptr to slice", func(t *testing.T) {
var condition *[]interface{} = nil
assert.True(t, isEmptyCondition(condition))
})

t.Run("not-nil ptr to nil-slice", func(t *testing.T) {
var theSlice []interface{} = nil
var condition *[]interface{} = &theSlice
assert.True(t, isEmptyCondition(condition))
})

t.Run("not-nil ptr to empty slice", func(t *testing.T) {
theSlice := []interface{}{}
condition := &theSlice
assert.True(t, isEmptyCondition(condition))
})
}
Loading