mirror of https://github.com/pikami/cosmium.git
Fix array access
This commit is contained in:
parent
e623a563f4
commit
b808e97c72
|
@ -65,8 +65,8 @@ func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.Container
|
||||||
Paths: []string{"/pk"},
|
Paths: []string{"/pk"},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "12345", "pk": "123", "isCool": false})
|
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})
|
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true, "arr": []int{6, 7, 8}})
|
||||||
|
|
||||||
ts := runTestServer()
|
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) {
|
func Test_Documents_Patch(t *testing.T) {
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -251,9 +251,8 @@ DotFieldAccess <- "." id:Identifier {
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayFieldAccess <- "[\"" id:Identifier "\"]" {
|
ArrayFieldAccess <- "[\"" id:Identifier "\"]" { return id, nil }
|
||||||
return id, nil
|
/ "[" id:Integer "]" { return strconv.Itoa(id.(int)), nil }
|
||||||
}
|
|
||||||
|
|
||||||
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
|
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* {
|
||||||
return string(c.text), nil
|
return string(c.text), nil
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pikami/cosmium/internal/logger"
|
"github.com/pikami/cosmium/internal/logger"
|
||||||
|
@ -303,9 +304,18 @@ func (c memoryExecutorContext) getFieldValue(field parsers.SelectItem, row RowTy
|
||||||
|
|
||||||
if len(field.Path) > 1 {
|
if len(field.Path) > 1 {
|
||||||
for _, pathSegment := range 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]
|
value = nestedValue[pathSegment]
|
||||||
} else {
|
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 nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue