Skip to content

Commit

Permalink
Merge pull request #8 from yaricom/default-key-target
Browse files Browse the repository at this point in the history
Fixed Decode() to properly handle default value for `Key` target if it is not found in XML.
  • Loading branch information
yaricom authored Dec 15, 2023
2 parents df3a6ed + c7f3703 commit 7bc6daf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions data/test_graph_default_key_target.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
<key id="key0" attr.name="test-key">
<desc>Messages related to this host</desc>
<default />
</key>
<graph edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
<node id="n0">
<desc>test node #1</desc>
<data key="key0">test data</data>
</node>
</graph>
</graphml>
3 changes: 3 additions & 0 deletions graphml/graphml.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ func (gml *GraphML) Decode(r io.Reader) error {
if key.KeyType == "" {
key.KeyType = gml.keyTypeDefault
}
if key.Target == "" {
key.Target = KeyForAll
}
gml.keysByIdentifier[keyIdentifier(key.Name, key.Target)] = key
gml.keysById[key.ID] = key
}
Expand Down
39 changes: 39 additions & 0 deletions graphml/graphml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,45 @@ func TestNewGraphMLWithAttributes(t *testing.T) {
assert.Equal(t, attributes, attr)
}

func TestGraphML_Decode_keyTargetDefault(t *testing.T) {
graphFile, err := os.Open("../data/test_graph_default_key_target.xml")
require.NoError(t, err, "failed to open file")
// decode
gml := NewGraphML("")
err = gml.Decode(graphFile)
require.NoError(t, err, "failed to decode")

// test results
attributes := map[string]interface{}{
"test-key": "test data",
}

// check Graph element
//
require.Len(t, gml.Graphs, 1, "wrong graphs number")
graph := gml.Graphs[0]

// check Node elements
//
require.Len(t, graph.Nodes, 1, "wrong nodes number")
for i, n := range graph.Nodes {
id := fmt.Sprintf("n%d", i)
assert.Equal(t, id, n.ID, "wrong node ID at: %d", i)
desc := fmt.Sprintf("test node #%d", i+1)
assert.Equal(t, desc, n.Description, "wrong node description at: %d", i)
// check GetAttributes
attrs, err := n.GetAttributes()
require.NoError(t, err, "failed to get attributes")
assert.Equal(t, attributes, attrs)
}

// check Key target was set properly
//
key := gml.GetKey("test-key", KeyForAll)
require.NotNil(t, key, "key expected")
assert.Equal(t, KeyForAll, key.Target)
}

func TestGraphML_Decode_keyTypeDefault(t *testing.T) {
graphFile, err := os.Open("../data/test_graph_default_key_type.xml")
require.NoError(t, err, "failed to open file")
Expand Down

0 comments on commit 7bc6daf

Please sign in to comment.