Skip to content

Commit

Permalink
getAttributes() (#41)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Madigan <jason@jasonmadigan.com>
  • Loading branch information
jasonmadigan authored Feb 28, 2025
1 parent e681158 commit 48c14d1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ func (e Edge) From() Node {
func (e Edge) To() Node {
return e.to
}

// Returns attributes for this edge
func (e Edge) GetAttributes() map[string]interface{} {
return e.AttributesMap.GetAttributes()
}
14 changes: 14 additions & 0 deletions edge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,17 @@ func TestNonStringAttribute(t *testing.T) {
t.Errorf("got [%v] want [%v]", got, want)
}
}

func TestEdgeGetAttributes(t *testing.T) {
di := NewGraph(Directed)
n1 := di.Node("A")
n2 := di.Node("B")
e := di.Edge(n1, n2).Label("edge-label").Attr("foo", "bar")
attrs := e.GetAttributes()
if v, ok := attrs["label"]; !ok || v != "edge-label" {
t.Errorf("expected label=edge-label, got %v", attrs)
}
if v, ok := attrs["foo"]; !ok || v != "bar" {
t.Errorf("expected foo=bar, got %v", attrs)
}
}
9 changes: 9 additions & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,12 @@ func (g *Graph) EdgesMap() map[string][]Edge {
func (g *Graph) HasNode(n Node) bool {
return g == n.graph
}

// GetAttributes returns a copy of the attributes
func (am *AttributesMap) GetAttributes() map[string]interface{} {
copyMap := make(map[string]interface{}, len(am.attributes))
for k, v := range am.attributes {
copyMap[k] = v
}
return copyMap
}
12 changes: 12 additions & 0 deletions graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,15 @@ func TestFindNodeWithLabel(t *testing.T) {
t.Fail()
}
}

func TestNodeGetAttributesCopy(t *testing.T) {
di := NewGraph(Directed)
n := di.Node("A")
n.Attr("foo", "bar")
attrs := n.GetAttributes()
attrs["foo"] = "bar"
attrs2 := n.GetAttributes()
if v, ok := attrs2["foo"]; !ok || v != "bar" {
t.Errorf("expected foo=bar, got %v", attrs2)
}
}

0 comments on commit 48c14d1

Please sign in to comment.