Skip to content

Commit

Permalink
Merge pull request #16 from joelrebel/edge-labels
Browse files Browse the repository at this point in the history
Support adding labels on edges.
  • Loading branch information
nikolaydubina authored Nov 11, 2022
2 parents 6aa043e + 1bd2c98 commit 0ac0e8a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
16 changes: 12 additions & 4 deletions dot/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewBasicGraph(

edges := make([]Renderable, 0, len(graph.Edges))
for _, e := range graph.Edges {
edges = append(edges, BasicEdge{from: e.From(), to: e.To()})
edges = append(edges, BasicEdge{from: e.From(), to: e.To(), label: e.Label()})
}

return BasicGraph{
Expand Down Expand Up @@ -67,10 +67,18 @@ func (r BasicGraph) Render() string {
}

type BasicEdge struct {
from string
to string
from string
to string
label string
}

func (r BasicEdge) Render() string {
return fmt.Sprintf(`"%s" -> "%s"`, r.from, r.to)
e := fmt.Sprintf(`"%s" -> "%s"`, r.from, r.to)

// include edge label when declared
if r.label != "" {
e += fmt.Sprintf(" [label=%s]", r.label)
}

return e
}
7 changes: 6 additions & 1 deletion graph/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (n NodeData) ID() string {
return v
}

// EdgeData can have any fields, only `from` and `to` is reserved.
// EdgeData can have any fields, only `from`, `to`, `label` is reserved.
// This is JSON representation of edge.
type EdgeData map[string]interface{}

Expand All @@ -50,6 +50,11 @@ func (e EdgeData) To() string {
return v
}

func (e EdgeData) Label() string {
v, _ := e["label"].(string)
return v
}

// union type, can be either
type orNodeDataEdgeData map[string]interface{}

Expand Down
20 changes: 18 additions & 2 deletions graph/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestEdgeData(t *testing.T) {
},
{
name: "valid with string id",
EdgeData: EdgeData(map[string]interface{}{"from": "asdf", "to": "12a3"}),
EdgeData: EdgeData(map[string]interface{}{"from": "asdf", "to": "12a3", "label": "foo"}),
valid: true,
},
{
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestOr(t *testing.T) {
},
{
name: "EdgeData",
or: orNodeDataEdgeData(map[string]interface{}{"from": "123", "to": "456"}),
or: orNodeDataEdgeData(map[string]interface{}{"from": "123", "to": "456", "label": "foo"}),
e: &e,
},
{
Expand Down Expand Up @@ -180,6 +180,7 @@ func TestParser(t *testing.T) {
input := `
{"id": "123"}
{"from": "123", "to": "321"}
{"from": "456", "to": "789", "label": "foo"}
`
g, err := NewGraphFromJSONL(strings.NewReader(input))
if err != nil {
Expand All @@ -200,5 +201,20 @@ func TestParser(t *testing.T) {
if edge["from"] != "123" || edge["to"] != "321" {
t.Errorf("bad EdgeData %#v", edge)
}

from = g.IDStorage.Get("456")
if v, ok := g.Nodes[from]; !ok || v["id"] != "456" {
t.Errorf("bad NodeData %#v", g.Nodes)
}

to = g.IDStorage.Get("789")
if v, ok := g.Nodes[to]; !ok || v["id"] != "789" {
t.Errorf("bad NodeData %#v", g.Nodes)
}

edge = g.Edges[[2]uint64{from, to}]
if edge["from"] != "456" || edge["to"] != "789" || edge["label"] != "foo" {
t.Errorf("bad EdgeData %#v", edge)
}
})
}

0 comments on commit 0ac0e8a

Please sign in to comment.