Skip to content

Commit

Permalink
Merge pull request #5 from n-peugnet/skip-empty-default
Browse files Browse the repository at this point in the history
Allow to parse empty data with empty default string key
  • Loading branch information
yaricom authored Dec 11, 2023
2 parents 3b2501e + fa7fe19 commit ab538b4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
13 changes: 13 additions & 0 deletions data/test_graph_default_empty_string.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" for="node" attr.name="string-key" attr.type="string">
<default></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"></data>
</node>
</graph>
</graphml>

2 changes: 1 addition & 1 deletion graphml/graphml.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func attributesForData(data []*Data, target KeyForElement, gml *GraphML) (map[st
}
// use data value of default value
dataValue := d.Value
if dataValue == "" {
if dataValue == "" && key.KeyType != StringType {
if key.DefaultValue != "" {
dataValue = key.DefaultValue
} else {
Expand Down
38 changes: 38 additions & 0 deletions graphml/graphml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@ func TestGraphML_Decode_keyTypeDefault(t *testing.T) {
assert.Equal(t, key.KeyType, StringType)
}

func TestGraphML_Decode_emptyStringDefault(t *testing.T) {
graphFile, err := os.Open("../data/test_graph_default_empty_string.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 := make(map[string]interface{})
attributes["string-key"] = ""

// 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 types was set properly
//
key := gml.GetKey("string-key", KeyForNode)
require.NotNil(t, key, "key expected")
assert.Equal(t, key.KeyType, StringType)
}

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

0 comments on commit ab538b4

Please sign in to comment.