mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-18 16:30:44 +00:00
Implement IS_DEFINED function
This commit is contained in:
@@ -2,6 +2,7 @@ package memoryexecutor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -122,6 +123,11 @@ func evaluateFilters(expr ExpressionType, queryParameters map[string]interface{}
|
||||
return value
|
||||
}
|
||||
return false
|
||||
case parsers.SelectItem:
|
||||
resolvedValue := getFieldValue(typedValue, queryParameters, row)
|
||||
if value, ok := resolvedValue.(bool); ok {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -172,6 +178,8 @@ func getFieldValue(field parsers.SelectItem, queryParameters map[string]interfac
|
||||
switch typedValue.Type {
|
||||
case parsers.FunctionCallStringEquals:
|
||||
return strings_StringEquals(typedValue.Arguments, queryParameters, row)
|
||||
case parsers.FunctionCallIsDefined:
|
||||
return typeChecking_IsDefined(typedValue.Arguments, queryParameters, row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,6 +232,10 @@ func orderBy(orderBy []parsers.OrderExpression, queryParameters map[string]inter
|
||||
}
|
||||
|
||||
func compareValues(val1, val2 interface{}) int {
|
||||
if reflect.TypeOf(val1) != reflect.TypeOf(val2) {
|
||||
return 1
|
||||
}
|
||||
|
||||
switch val1 := val1.(type) {
|
||||
case int:
|
||||
val2 := val2.(int)
|
||||
@@ -255,6 +267,9 @@ func compareValues(val1, val2 interface{}) int {
|
||||
}
|
||||
// TODO: Add more types
|
||||
default:
|
||||
return 0
|
||||
if reflect.DeepEqual(val1, val2) {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package memoryexecutor
|
||||
|
||||
import (
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
)
|
||||
|
||||
func typeChecking_IsDefined(arguments []interface{}, queryParameters map[string]interface{}, row RowType) bool {
|
||||
exItem := arguments[0].(parsers.SelectItem)
|
||||
ex := getFieldValue(exItem, queryParameters, row)
|
||||
|
||||
return ex != nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package memoryexecutor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
func Test_Execute_TypeCheckingFunctions(t *testing.T) {
|
||||
mockData := []memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "pk": "aaa"},
|
||||
map[string]interface{}{"id": "456"},
|
||||
map[string]interface{}{"id": "789", "pk": ""},
|
||||
}
|
||||
|
||||
t.Run("Should execute function IS_DEFINED(path)", func(t *testing.T) {
|
||||
testQueryExecute(
|
||||
t,
|
||||
parsers.SelectStmt{
|
||||
SelectItems: []parsers.SelectItem{
|
||||
{Path: []string{"c", "id"}},
|
||||
{
|
||||
Alias: "defined",
|
||||
Type: parsers.SelectItemTypeFunctionCall,
|
||||
Value: parsers.FunctionCall{
|
||||
Type: parsers.FunctionCallIsDefined,
|
||||
Arguments: []interface{}{
|
||||
parsers.SelectItem{
|
||||
Path: []string{"c", "pk"},
|
||||
Type: parsers.SelectItemTypeField,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Table: parsers.Table{Value: "c"},
|
||||
},
|
||||
mockData,
|
||||
[]memoryexecutor.RowType{
|
||||
map[string]interface{}{"id": "123", "defined": true},
|
||||
map[string]interface{}{"id": "456", "defined": false},
|
||||
map[string]interface{}{"id": "789", "defined": true},
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user