Move Subgraph into heartwood package as EventSubgraph.

This commit is contained in:
Jay
2026-03-03 16:22:02 -05:00
parent d87ec421db
commit c4cc7115b3
4 changed files with 58 additions and 64 deletions

View File

@@ -5,7 +5,7 @@ import (
roots "git.wisehodl.dev/jay/go-roots/events" 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 type ExpanderRegistry []Expander
func NewExpanderRegistry() ExpanderRegistry { func NewExpanderRegistry() ExpanderRegistry {
@@ -27,7 +27,7 @@ func (r *ExpanderRegistry) Add(m Expander) {
// Default Expander Functions // Default Expander Functions
func ExpandTaggedEvents(e roots.Event, s *graph.Subgraph) { func ExpandTaggedEvents(e roots.Event, s *EventSubgraph) {
tagNodes := s.NodesByLabel("Tag") tagNodes := s.NodesByLabel("Tag")
for _, tag := range e.Tags { for _, tag := range e.Tags {
if !isValidTag(tag) { 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") tagNodes := s.NodesByLabel("Tag")
for _, tag := range e.Tags { for _, tag := range e.Tags {
if !isValidTag(tag) { if !isValidTag(tag) {

View File

@@ -161,54 +161,6 @@ func (r *Relationship) Serialize() *SerializedRel {
return &srel 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 // Structured Subgraph
// ======================================== // ========================================

View File

@@ -5,8 +5,50 @@ import (
roots "git.wisehodl.dev/jay/go-roots/events" roots "git.wisehodl.dev/jay/go-roots/events"
) )
func EventToSubgraph(e roots.Event, exp ExpanderRegistry) *graph.Subgraph { // Event subgraph struct
subgraph := graph.NewSubgraph()
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 // Create Event node
eventNode := graph.NewEventNode(e.ID) eventNode := graph.NewEventNode(e.ID)

View File

@@ -30,8 +30,8 @@ func newFullEventNode(id string, createdAt, kind int, content string) *graph.Nod
return n return n
} }
func baseSubgraph(eventID, pubkey string) (*graph.Subgraph, *graph.Node, *graph.Node) { func baseSubgraph(eventID, pubkey string) (*EventSubgraph, *graph.Node, *graph.Node) {
s := graph.NewSubgraph() s := NewEventSubgraph()
eventNode := newFullEventNode(eventID, static.CreatedAt, static.Kind, static.Content) eventNode := newFullEventNode(eventID, static.CreatedAt, static.Kind, static.Content)
userNode := graph.NewUserNode(pubkey) userNode := graph.NewUserNode(pubkey)
s.AddNode(eventNode) s.AddNode(eventNode)
@@ -44,7 +44,7 @@ func TestEventToSubgraph(t *testing.T) {
cases := []struct { cases := []struct {
name string name string
event roots.Event event roots.Event
expected *graph.Subgraph expected *EventSubgraph
}{ }{
{ {
name: "bare event", name: "bare event",
@@ -52,7 +52,7 @@ func TestEventToSubgraph(t *testing.T) {
ID: ids["a"], PubKey: ids["b"], ID: ids["a"], PubKey: ids["b"],
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
}, },
expected: func() *graph.Subgraph { expected: func() *EventSubgraph {
s, _, _ := baseSubgraph(ids["a"], ids["b"]) s, _, _ := baseSubgraph(ids["a"], ids["b"])
return s return s
}(), }(),
@@ -64,7 +64,7 @@ func TestEventToSubgraph(t *testing.T) {
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"t", "bitcoin"}}, Tags: []roots.Tag{{"t", "bitcoin"}},
}, },
expected: func() *graph.Subgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) s, eventNode, _ := baseSubgraph(ids["a"], ids["b"])
tagNode := graph.NewTagNode("t", "bitcoin") tagNode := graph.NewTagNode("t", "bitcoin")
s.AddNode(tagNode) s.AddNode(tagNode)
@@ -79,7 +79,7 @@ func TestEventToSubgraph(t *testing.T) {
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"t"}}, Tags: []roots.Tag{{"t"}},
}, },
expected: func() *graph.Subgraph { expected: func() *EventSubgraph {
s, _, _ := baseSubgraph(ids["a"], ids["b"]) s, _, _ := baseSubgraph(ids["a"], ids["b"])
return s return s
}(), }(),
@@ -91,7 +91,7 @@ func TestEventToSubgraph(t *testing.T) {
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"e", ids["c"]}}, Tags: []roots.Tag{{"e", ids["c"]}},
}, },
expected: func() *graph.Subgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) s, eventNode, _ := baseSubgraph(ids["a"], ids["b"])
tagNode := graph.NewTagNode("e", ids["c"]) tagNode := graph.NewTagNode("e", ids["c"])
referencedEvent := graph.NewEventNode(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, CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"e", "notvalid"}}, Tags: []roots.Tag{{"e", "notvalid"}},
}, },
expected: func() *graph.Subgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) s, eventNode, _ := baseSubgraph(ids["a"], ids["b"])
tagNode := graph.NewTagNode("e", "notvalid") tagNode := graph.NewTagNode("e", "notvalid")
s.AddNode(tagNode) s.AddNode(tagNode)
@@ -124,7 +124,7 @@ func TestEventToSubgraph(t *testing.T) {
CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content, CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"p", ids["d"]}}, Tags: []roots.Tag{{"p", ids["d"]}},
}, },
expected: func() *graph.Subgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) s, eventNode, _ := baseSubgraph(ids["a"], ids["b"])
tagNode := graph.NewTagNode("p", ids["d"]) tagNode := graph.NewTagNode("p", ids["d"])
referencedUser := graph.NewUserNode(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, CreatedAt: static.CreatedAt, Kind: static.Kind, Content: static.Content,
Tags: []roots.Tag{{"p", "notvalid"}}, Tags: []roots.Tag{{"p", "notvalid"}},
}, },
expected: func() *graph.Subgraph { expected: func() *EventSubgraph {
s, eventNode, _ := baseSubgraph(ids["a"], ids["b"]) s, eventNode, _ := baseSubgraph(ids["a"], ids["b"])
tagNode := graph.NewTagNode("p", "notvalid") tagNode := graph.NewTagNode("p", "notvalid")
s.AddNode(tagNode) s.AddNode(tagNode)
@@ -228,7 +228,7 @@ func propsEqual(expected, got graph.Properties) error {
return nil return nil
} }
func assertSubgraphsEqual(t *testing.T, expected, got *graph.Subgraph) { func assertSubgraphsEqual(t *testing.T, expected, got *EventSubgraph) {
t.Helper() t.Helper()
gotNodes := make([]*graph.Node, len(got.Nodes())) gotNodes := make([]*graph.Node, len(got.Nodes()))