Wrote merge nodes test and neo4j testing infrastructure.

Added entity formatting functionality.
Renamed the EventSubgraph to Subgraph because it is generic.
This commit is contained in:
jay
2026-11-26 11:59:31 -05:00
parent 54f3f81add
commit ffe11f9c36
8 changed files with 600 additions and 39 deletions
+73 -2
View File
@@ -2,6 +2,10 @@ package heartwood
import (
"fmt"
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
"maps"
"slices"
"strings"
)
// ----------------------------------------------------------------------------
@@ -11,6 +15,36 @@ import (
// Properties represents a map of node or relationship props.
type Properties map[string]any
func (p Properties) String() string {
var b strings.Builder
b.WriteString("{")
for i, k := range slices.Sorted(maps.Keys(p)) {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(fmt.Sprintf("%s: %s", k, formatPropValue(p[k])))
}
b.WriteString("}")
return b.String()
}
func formatPropValue(v any) string {
switch v := v.(type) {
case nil:
return "nil"
case string:
return fmt.Sprintf("%q", v)
case bool:
return fmt.Sprintf("%t", v)
case int, int64:
return fmt.Sprintf("%d", v)
case float64:
return fmt.Sprintf("%g", v)
default:
return fmt.Sprintf("%v", v)
}
}
// ----------------------------------------------------------------------------
// Match Key Provider
// ----------------------------------------------------------------------------
@@ -53,6 +87,8 @@ func (p *SimpleMatchKeys) GetKeys(label string) ([]string, bool) {
// Node represents a Neo4j node entity, encapsulating its labels and
// properties.
type Node struct {
// Element ID of the node.
ElementID string
// Set of labels on the node.
Labels *StringSet
// Mapping of properties on the node.
@@ -60,16 +96,24 @@ type Node struct {
}
// NewNode creates a new node with the given label and properties.
func NewNode(label string, props Properties) *Node {
func NewNode(labels []string, props Properties) *Node {
if props == nil {
props = make(Properties)
}
return &Node{
Labels: NewStringSet(label),
Labels: NewStringSet(labels...),
Props: props,
}
}
// NewNodeFromNeo4j creates a new node from a Neo4j Node.
func NewNodeFromNeo4j(n neo4j.Node) *Node {
return &Node{
ElementID: n.ElementId,
Labels: NewStringSet(n.Labels...),
Props: n.Props}
}
// MatchProps returns the node label and the property values to match it in the
// database.
func (n *Node) MatchProps(
@@ -105,6 +149,14 @@ func (n *Node) MatchProps(
return "", nil, fmt.Errorf("no recognized label found in %v", n.Labels)
}
func (n *Node) String() string {
if n.ElementID != "" && len(n.Labels.AsSortedArray()) == 0 {
return fmt.Sprintf("Node{element_id=%q}", n.ElementID)
}
return fmt.Sprintf("Node{element_id=%q labels=%v properties=%v}",
n.ElementID, n.Labels.AsSortedArray(), n.Props.String())
}
type SerializedNode = Properties
func (n *Node) Serialize() *SerializedNode {
@@ -118,6 +170,8 @@ func (n *Node) Serialize() *SerializedNode {
// Relationship represents a Neo4j relationship between two nodes, including
// its type and properties.
type Relationship struct {
// The element ID of the relationship.
ElementID string
// The relationship type.
Type string
// The start node for the relationship.
@@ -144,6 +198,23 @@ func NewRelationship(
}
}
// NewRelationshipFromNeo4j creates a new relationship from a Neo4j Relationship.
func NewRelationshipFromNeo4j(r neo4j.Relationship) *Relationship {
return &Relationship{
ElementID: r.ElementId,
Type: r.Type,
Start: &Node{ElementID: r.StartElementId},
End: &Node{ElementID: r.EndElementId},
Props: r.Props,
}
}
func (r *Relationship) String() string {
return fmt.Sprintf(
"Relationship{element_id=%q type=%q start=%v end=%v properties=%v}",
r.ElementID, r.Type, r.Start, r.End, r.Props.String())
}
type SerializedRel = map[string]Properties
func (r *Relationship) Serialize() *SerializedRel {