Skip to content

Commit 00e44ed

Browse files
committed
Only fill data with default value when there is a default in the key
1 parent 5fcb4aa commit 00e44ed

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

data/test_graph_default_key_type.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<key id="key2" for="node" attr.name="color" attr.type="string">
1111
<default>yellow</default>
1212
</key>
13+
<key id="key3" for="node" attr.name="int-no-default" attr.type="int"></key>
1314
<graph edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
1415
<node id="n0">
1516
<desc>test node #1</desc>

graphml/graphml.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,13 @@ func attributesForData(data []*Data, target KeyForElement, gml *GraphML) (map[st
458458
}
459459
// fill defaults for undefined keys
460460
for _, k := range keysForElement(gml.Keys, target) {
461+
if k.DefaultValue == "" && k.KeyType != StringType {
462+
continue
463+
}
461464
if _, ok := attr[k.Name]; !ok {
462465
val, err := valueByType(k.DefaultValue, k.KeyType, gml.keyTypeDefault)
463466
if err != nil {
464-
return nil, err
467+
return nil, errors.New("could not parse default value for key id: " + k.ID)
465468
}
466469
attr[k.Name] = val
467470
}

graphml/graphml_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func TestGraphML_Decode_keyTypeDefault(t *testing.T) {
8080
key = gml.GetKey("integer-key", KeyForNode)
8181
require.NotNil(t, key, "key expected")
8282
assert.Equal(t, key.KeyType, IntType)
83+
84+
key = gml.GetKey("color", KeyForNode)
85+
require.NotNil(t, key, "key expected")
86+
assert.Equal(t, key.KeyType, StringType)
8387
}
8488

8589
func TestGraphML_Decode(t *testing.T) {

0 commit comments

Comments
 (0)