Skip to content

Commit

Permalink
Merge branch 'master' into BUX-246/Linters
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii-4chain authored Apr 9, 2024
2 parents b434f03 + 89f4210 commit 766f7cb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
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))
})
}

0 comments on commit 766f7cb

Please # to comment.