Fix array access

This commit is contained in:
Pijus Kamandulis 2024-06-03 19:00:52 +03:00
parent e623a563f4
commit b808e97c72
4 changed files with 891 additions and 822 deletions

View File

@ -65,8 +65,8 @@ func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.Container
Paths: []string{"/pk"},
},
})
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false})
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true})
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false, "arr": []int{1, 2, 3}})
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
ts := runTestServer()
@ -146,6 +146,22 @@ func Test_Documents(t *testing.T) {
},
)
})
t.Run("Should query array accessor", func(t *testing.T) {
testCosmosQuery(t, collectionClient,
`SELECT c.id,
c["arr"][0] AS arr0,
c["arr"][1] AS arr1,
c["arr"][2] AS arr2,
c["arr"][3] AS arr3
FROM c ORDER BY c.id`,
nil,
[]interface{}{
map[string]interface{}{"id": "12345", "arr0": 1.0, "arr1": 2.0, "arr2": 3.0, "arr3": nil},
map[string]interface{}{"id": "67890", "arr0": 6.0, "arr1": 7.0, "arr2": 8.0, "arr3": nil},
},
)
})
}
func Test_Documents_Patch(t *testing.T) {

File diff suppressed because it is too large Load Diff

View File

@ -251,9 +251,8 @@ DotFieldAccess <- "." id:Identifier {
return id, nil
}
ArrayFieldAccess <- "[\"" id:Identifier "\"]" {
return id, nil
}
ArrayFieldAccess <- "[\"" id:Identifier "\"]" { return id, nil }
/ "[" id:Integer "]" { return strconv.Itoa(id.(int)), nil }
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
return string(c.text), nil

View File

@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"github.com/pikami/cosmium/internal/logger"
@ -303,11 +304,20 @@ func (c memoryExecutorContext) getFieldValue(field parsers.SelectItem, row RowTy
if len(field.Path) > 1 {
for _, pathSegment := range field.Path[1:] {
if nestedValue, ok := value.(map[string]interface{}); ok {
switch nestedValue := value.(type) {
case map[string]interface{}:
value = nestedValue[pathSegment]
case []int, []string, []interface{}:
slice := reflect.ValueOf(nestedValue)
if arrayIndex, err := strconv.Atoi(pathSegment); err == nil && slice.Len() > arrayIndex {
value = slice.Index(arrayIndex).Interface()
} else {
return nil
}
default:
return nil
}
}
}
return value