feat(neo4j): expand node query properties into result columns
This commit is contained in:
1 parent
38a00c7cb7
commit
660071870b
12 files changed
+433
-29
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
neo4j "github.com/neo4j/neo4j-go-driver/v6/neo4j"
|
||||
)
|
||||
|
||||
func TestNodeResultKeepsSourceWidthAndExactPropertyValues(t *testing.T) {
|
||||
node := neo4j.Node{Labels: []string{"Person"}, Props: map[string]any{
|
||||
"name": "Ada", "age": int64(9007199254740993), "active": true, "scores": []any{int64(1), int64(2)},
|
||||
}}
|
||||
row := normalizeRecord(&neo4j.Record{Values: []any{node, int64(7), nil}}, 3)
|
||||
cell, ok := row[0].(nodeCell)
|
||||
if !ok || len(row) != 3 || row[1] != "7" || row[2] != nil {
|
||||
t.Fatalf("unexpected record: %#v", row)
|
||||
}
|
||||
want := []nodeProperty{
|
||||
{Name: "active", Type: "Boolean", Value: "true"},
|
||||
{Name: "age", Type: "Integer", Value: "9007199254740993"},
|
||||
{Name: "name", Type: "String", Value: "Ada"},
|
||||
{Name: "scores", Type: "List", Value: "[1,2]"},
|
||||
}
|
||||
if cell.Marker != "v1" || cell.Display != formatNode(node) || !reflect.DeepEqual(cell.Properties, want) {
|
||||
t.Fatalf("unexpected node cell: %#v", cell)
|
||||
}
|
||||
encoded, err := json.Marshal(row)
|
||||
if err != nil || !strings.Contains(string(encoded), `"value":"9007199254740993"`) {
|
||||
t.Fatalf("inexact wire value: %s, err=%v", encoded, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyNodeAndNonNodeValues(t *testing.T) {
|
||||
node := neo4j.Node{Labels: []string{"Empty"}}
|
||||
cell := normalizeNodeCell(node)
|
||||
if cell.Properties == nil || len(cell.Properties) != 0 {
|
||||
t.Fatalf("empty properties must serialize as []: %#v", cell)
|
||||
}
|
||||
for _, value := range []any{nil, "(:Person {\"name\":\"literal\"})", true, int64(42),
|
||||
neo4j.Relationship{Type: "KNOWS"}, neo4j.Path{Nodes: []neo4j.Node{node}},
|
||||
[]any{node}, map[string]any{"node": node}} {
|
||||
row := normalizeRecord(&neo4j.Record{Values: []any{value}}, 1)
|
||||
if !reflect.DeepEqual(row[0], normalizeQueryValue(value)) {
|
||||
t.Fatalf("non-node result changed for %T: %#v", value, row)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -242,11 +243,37 @@ func normalizeRecord(record *neo4j.Record, width int) []any {
|
||||
return row
|
||||
}
|
||||
for index := 0; index < width && index < len(record.Values); index++ {
|
||||
row[index] = normalizeQueryValue(record.Values[index])
|
||||
if node, ok := record.Values[index].(neo4j.Node); ok {
|
||||
row[index] = normalizeNodeCell(node)
|
||||
} else {
|
||||
row[index] = normalizeQueryValue(record.Values[index])
|
||||
}
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
type nodeProperty struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Value any `json:"value"`
|
||||
}
|
||||
|
||||
type nodeCell struct {
|
||||
Marker string `json:"__dbx_neo4j_node"`
|
||||
Display string `json:"display"`
|
||||
Properties []nodeProperty `json:"properties"`
|
||||
}
|
||||
|
||||
// Keep the original record keys for cursor paging; the grid projects properties.
|
||||
func normalizeNodeCell(node neo4j.Node) nodeCell {
|
||||
properties := make([]nodeProperty, 0, len(node.Props))
|
||||
for name, value := range node.Props {
|
||||
properties = append(properties, nodeProperty{Name: name, Type: neo4jTypeName(value), Value: normalizeQueryValue(value)})
|
||||
}
|
||||
sort.Slice(properties, func(i, j int) bool { return properties[i].Name < properties[j].Name })
|
||||
return nodeCell{Marker: "v1", Display: formatNode(node), Properties: properties}
|
||||
}
|
||||
|
||||
func normalizeQueryValue(value any) any {
|
||||
if value == nil {
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user