Move Subgraph into heartwood package as EventSubgraph.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
// ========================================
|
||||
|
||||
46
subgraph.go
46
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)
|
||||
|
||||
@@ -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()))
|
||||
|
||||
Reference in New Issue
Block a user