package heartwood import ( "fmt" "github.com/neo4j/neo4j-go-driver/v6/neo4j" "maps" "slices" "strings" ) // ---------------------------------------------------------------------------- // Types // ---------------------------------------------------------------------------- // 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 // ---------------------------------------------------------------------------- // MatchKeysProvider defines methods for querying a mapping of node labels and // the property keys used to match nodes with them. type MatchKeysProvider interface { // GetLabels returns the array of node labels in the mapping. GetLabels() []string // GetKeys returns the node property keys used to match nodes with the // given label and a boolean indicating the success of the lookup. GetKeys(label string) ([]string, bool) } // SimpleMatchKeys is a simple implementation of the MatchKeysProvider interface. type SimpleMatchKeys struct { Keys map[string][]string } func (p *SimpleMatchKeys) GetLabels() []string { labels := make([]string, 0, len(p.Keys)) for l := range p.Keys { labels = append(labels, l) } return labels } func (p *SimpleMatchKeys) GetKeys(label string) ([]string, bool) { if keys, exists := p.Keys[label]; exists { return keys, exists } return nil, false } // ---------------------------------------------------------------------------- // Nodes // ---------------------------------------------------------------------------- // 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. Props Properties } // NewNode creates a new node with the given label and properties. func NewNode(labels []string, props Properties) *Node { if props == nil { props = make(Properties) } return &Node{ 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( matchProvider MatchKeysProvider) (string, Properties, error) { // Iterate over each label on the node, checking whether each has match // keys associated with it. labels := n.Labels.AsSortedArray() for _, label := range labels { if keys, exists := matchProvider.GetKeys(label); exists { props := make(Properties) // Get the property values associated with each match key. for _, key := range keys { if value, exists := n.Props[key]; exists { props[key] = value } else { // If any match property values are missing, return an // error. return label, nil, fmt.Errorf( "missing property %s for label %s", key, label) } } // Return the label and match properties return label, props, nil } } // If none of the node labels have defined match keys, return an error. 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 { return &n.Props } // ---------------------------------------------------------------------------- // Relationships // ---------------------------------------------------------------------------- // 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. Start *Node // The end node for the relationship. End *Node // Mapping of properties on the relationship Props Properties } // NewRelationship creates a new relationship with the given type, start node, // end node, and properties func NewRelationship( rtype string, start *Node, end *Node, props Properties) *Relationship { if props == nil { props = make(Properties) } return &Relationship{ Type: rtype, Start: start, End: end, Props: props, } } // NewRelationshipFromNeo4j creates a new relationship from a Neo4j Relationship. func NewRelationshipFromNeo4j(r neo4j.Relationship) *Relationship { start, end := NewNode(nil, nil), NewNode(nil, nil) start.ElementID = r.StartElementId end.ElementID = r.EndElementId return &Relationship{ ElementID: r.ElementId, Type: r.Type, Start: start, End: end, 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 { srel := make(map[string]Properties) srel["props"] = r.Props srel["start"] = r.Start.Props srel["end"] = r.End.Props return &srel }