From c4cc7115b3c3fe69eda22799f79f5159977db357 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 3 Mar 2026 16:22:02 -0500 Subject: [PATCH] Move Subgraph into heartwood package as EventSubgraph. --- expanders.go | 6 +++--- graph/graph.go | 48 ------------------------------------------------ subgraph.go | 46 ++++++++++++++++++++++++++++++++++++++++++++-- subgraph_test.go | 22 +++++++++++----------- 4 files changed, 58 insertions(+), 64 deletions(-) diff --git a/expanders.go b/expanders.go index a837d3c..35c736d 100644 --- a/expanders.go +++ b/expanders.go @@ -5,7 +5,7 @@ import ( roots "git.wisehodl.dev/jay/go-roots/events" ) -type Expander func(e roots.Event, s *graph.Subgraph) +type Expander func(e roots.Event, s *EventSubgraph) type ExpanderRegistry []Expander func NewExpanderRegistry() ExpanderRegistry { @@ -27,7 +27,7 @@ func (r *ExpanderRegistry) Add(m Expander) { // Default Expander Functions -func ExpandTaggedEvents(e roots.Event, s *graph.Subgraph) { +func ExpandTaggedEvents(e roots.Event, s *EventSubgraph) { tagNodes := s.NodesByLabel("Tag") for _, tag := range e.Tags { if !isValidTag(tag) { @@ -52,7 +52,7 @@ func ExpandTaggedEvents(e roots.Event, s *graph.Subgraph) { } } -func ExpandTaggedUsers(e roots.Event, s *graph.Subgraph) { +func ExpandTaggedUsers(e roots.Event, s *EventSubgraph) { tagNodes := s.NodesByLabel("Tag") for _, tag := range e.Tags { if !isValidTag(tag) { diff --git a/graph/graph.go b/graph/graph.go index 435676b..a150e03 100644 --- a/graph/graph.go +++ b/graph/graph.go @@ -161,54 +161,6 @@ func (r *Relationship) Serialize() *SerializedRel { return &srel } -// ======================================== -// Simple Subgraph -// ======================================== - -// Subgraph represents a simple collection of nodes and relationships. -type Subgraph struct { - // The nodes in the subgraph. - nodes []*Node - // The relationships in the subgraph. - rels []*Relationship -} - -// NewSubgraph creates an empty subgraph. -func NewSubgraph() *Subgraph { - return &Subgraph{ - nodes: []*Node{}, - rels: []*Relationship{}, - } -} - -// AddNode adds a node to the subgraph -func (s *Subgraph) AddNode(node *Node) { - s.nodes = append(s.nodes, node) -} - -// AddRel adds a relationship to the subgraph. -func (s *Subgraph) AddRel(rel *Relationship) { - s.rels = append(s.rels, rel) -} - -func (s *Subgraph) Nodes() []*Node { - return s.nodes -} - -func (s *Subgraph) Rels() []*Relationship { - return s.rels -} - -func (s *Subgraph) NodesByLabel(label string) []*Node { - nodes := []*Node{} - for _, node := range s.nodes { - if node.Labels.Contains(label) { - nodes = append(nodes, node) - } - } - return nodes -} - // ======================================== // Structured Subgraph // ======================================== diff --git a/subgraph.go b/subgraph.go index 568e6a2..bd34327 100644 --- a/subgraph.go +++ b/subgraph.go @@ -5,8 +5,50 @@ import ( roots "git.wisehodl.dev/jay/go-roots/events" ) -func EventToSubgraph(e roots.Event, exp ExpanderRegistry) *graph.Subgraph { - subgraph := graph.NewSubgraph() +// Event subgraph struct + +type EventSubgraph struct { + nodes []*graph.Node + rels []*graph.Relationship +} + +func NewEventSubgraph() *EventSubgraph { + return &EventSubgraph{ + nodes: []*graph.Node{}, + rels: []*graph.Relationship{}, + } +} + +func (s *EventSubgraph) AddNode(node *graph.Node) { + s.nodes = append(s.nodes, node) +} + +func (s *EventSubgraph) AddRel(rel *graph.Relationship) { + s.rels = append(s.rels, rel) +} + +func (s *EventSubgraph) Nodes() []*graph.Node { + return s.nodes +} + +func (s *EventSubgraph) Rels() []*graph.Relationship { + return s.rels +} + +func (s *EventSubgraph) NodesByLabel(label string) []*graph.Node { + nodes := []*graph.Node{} + for _, node := range s.nodes { + if node.Labels.Contains(label) { + nodes = append(nodes, node) + } + } + return nodes +} + +// Event to subgraph conversion + +func EventToSubgraph(e roots.Event, exp ExpanderRegistry) *EventSubgraph { + subgraph := NewEventSubgraph() // Create Event node eventNode := graph.NewEventNode(e.ID) diff --git a/subgraph_test.go b/subgraph_test.go index 2421a8a..af13902 100644 --- a/subgraph_test.go +++ b/subgraph_test.go @@ -30,8 +30,8 @@ func newFullEventNode(id string, createdAt, kind int, content string) *graph.Nod return n } -func baseSubgraph(eventID, pubkey string) (*graph.Subgraph, *graph.Node, *graph.Node) { - s := graph.NewSubgraph() +func baseSubgraph(eventID, pubkey string) (*EventSubgraph, *graph.Node, *graph.Node) { + s := NewEventSubgraph() eventNode := newFullEventNode(eventID, static.CreatedAt, static.Kind, static.Content) userNode := graph.NewUserNode(pubkey) s.AddNode(eventNode) @@ -44,7 +44,7 @@ func TestEventToSubgraph(t *testing.T) { cases := []struct { name string event roots.Event - expected *graph.Subgraph + expected *EventSubgraph }{ { name: "bare event", @@ -52,7 +52,7 @@ func TestEventToSubgraph(t *testing.T) { ID: ids["a"], PubKey: ids["b"], CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, }, - expected: func() *graph.Subgraph { + expected: func() *EventSubgraph { s, _, _ := baseSubgraph(ids["a"], ids["b"]) return s }(), @@ -64,7 +64,7 @@ func TestEventToSubgraph(t *testing.T) { CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, Tags: []roots.Tag{{"t", "bitcoin"}}, }, - expected: func() *graph.Subgraph { + expected: func() *EventSubgraph { s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) tagNode := graph.NewTagNode("t", "bitcoin") s.AddNode(tagNode) @@ -79,7 +79,7 @@ func TestEventToSubgraph(t *testing.T) { CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, Tags: []roots.Tag{{"t"}}, }, - expected: func() *graph.Subgraph { + expected: func() *EventSubgraph { s, _, _ := baseSubgraph(ids["a"], ids["b"]) return s }(), @@ -91,7 +91,7 @@ func TestEventToSubgraph(t *testing.T) { CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, Tags: []roots.Tag{{"e", ids["c"]}}, }, - expected: func() *graph.Subgraph { + expected: func() *EventSubgraph { s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) tagNode := graph.NewTagNode("e", ids["c"]) referencedEvent := graph.NewEventNode(ids["c"]) @@ -109,7 +109,7 @@ func TestEventToSubgraph(t *testing.T) { CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, Tags: []roots.Tag{{"e", "notvalid"}}, }, - expected: func() *graph.Subgraph { + expected: func() *EventSubgraph { s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) tagNode := graph.NewTagNode("e", "notvalid") s.AddNode(tagNode) @@ -124,7 +124,7 @@ func TestEventToSubgraph(t *testing.T) { CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, Tags: []roots.Tag{{"p", ids["d"]}}, }, - expected: func() *graph.Subgraph { + expected: func() *EventSubgraph { s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) tagNode := graph.NewTagNode("p", ids["d"]) referencedUser := graph.NewUserNode(ids["d"]) @@ -142,7 +142,7 @@ func TestEventToSubgraph(t *testing.T) { CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, Tags: []roots.Tag{{"p", "notvalid"}}, }, - expected: func() *graph.Subgraph { + expected: func() *EventSubgraph { s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) tagNode := graph.NewTagNode("p", "notvalid") s.AddNode(tagNode) @@ -228,7 +228,7 @@ func propsEqual(expected, got graph.Properties) error { return nil } -func assertSubgraphsEqual(t *testing.T, expected, got *graph.Subgraph) { +func assertSubgraphsEqual(t *testing.T, expected, got *EventSubgraph) { t.Helper() gotNodes := make([]*graph.Node, len(got.Nodes()))