Skip to content

Commit 48c14d1

Browse files
authored
getAttributes() (#41)
Signed-off-by: Jason Madigan <[email protected]>
1 parent e681158 commit 48c14d1

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

edge.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ func (e Edge) From() Node {
7171
func (e Edge) To() Node {
7272
return e.to
7373
}
74+
75+
// Returns attributes for this edge
76+
func (e Edge) GetAttributes() map[string]interface{} {
77+
return e.AttributesMap.GetAttributes()
78+
}

edge_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,17 @@ func TestNonStringAttribute(t *testing.T) {
119119
t.Errorf("got [%v] want [%v]", got, want)
120120
}
121121
}
122+
123+
func TestEdgeGetAttributes(t *testing.T) {
124+
di := NewGraph(Directed)
125+
n1 := di.Node("A")
126+
n2 := di.Node("B")
127+
e := di.Edge(n1, n2).Label("edge-label").Attr("foo", "bar")
128+
attrs := e.GetAttributes()
129+
if v, ok := attrs["label"]; !ok || v != "edge-label" {
130+
t.Errorf("expected label=edge-label, got %v", attrs)
131+
}
132+
if v, ok := attrs["foo"]; !ok || v != "bar" {
133+
t.Errorf("expected foo=bar, got %v", attrs)
134+
}
135+
}

graph.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,12 @@ func (g *Graph) EdgesMap() map[string][]Edge {
418418
func (g *Graph) HasNode(n Node) bool {
419419
return g == n.graph
420420
}
421+
422+
// GetAttributes returns a copy of the attributes
423+
func (am *AttributesMap) GetAttributes() map[string]interface{} {
424+
copyMap := make(map[string]interface{}, len(am.attributes))
425+
for k, v := range am.attributes {
426+
copyMap[k] = v
427+
}
428+
return copyMap
429+
}

graph_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,15 @@ func TestFindNodeWithLabel(t *testing.T) {
414414
t.Fail()
415415
}
416416
}
417+
418+
func TestNodeGetAttributesCopy(t *testing.T) {
419+
di := NewGraph(Directed)
420+
n := di.Node("A")
421+
n.Attr("foo", "bar")
422+
attrs := n.GetAttributes()
423+
attrs["foo"] = "bar"
424+
attrs2 := n.GetAttributes()
425+
if v, ok := attrs2["foo"]; !ok || v != "bar" {
426+
t.Errorf("expected foo=bar, got %v", attrs2)
427+
}
428+
}

0 commit comments

Comments
 (0)