Skip to content

Commit

Permalink
Fill undefined keys with default value
Browse files Browse the repository at this point in the history
  • Loading branch information
n-peugnet committed Dec 9, 2023
1 parent 8e28f1b commit 5fcb4aa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
3 changes: 3 additions & 0 deletions data/test_graph_default_key_type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<key id="key1" for="node" attr.name="integer-key" attr.type="int">
<default>10</default>
</key>
<key id="key2" for="node" attr.name="color" attr.type="string">
<default>yellow</default>
</key>
<graph edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
<node id="n0">
<desc>test node #1</desc>
Expand Down
30 changes: 25 additions & 5 deletions graphml/graphml.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,26 +414,26 @@ func (gr *Graph) GetEdge(sourceId, targetId string) *Edge {

// GetAttributes return data attributes map associated with GraphML
func (gml *GraphML) GetAttributes() (map[string]interface{}, error) {
return attributesForData(gml.Data, gml)
return attributesForData(gml.Data, KeyForGraphML, gml)
}

// GetAttributes return data attributes map associated with Graph
func (gr *Graph) GetAttributes() (map[string]interface{}, error) {
return attributesForData(gr.Data, gr.parent)
return attributesForData(gr.Data, KeyForGraph, gr.parent)
}

// GetAttributes returns data attributes map associated with Node
func (n *Node) GetAttributes() (map[string]interface{}, error) {
return attributesForData(n.Data, n.graph.parent)
return attributesForData(n.Data, KeyForNode, n.graph.parent)
}

// GetAttributes returns data attributes map associated with Edge
func (e *Edge) GetAttributes() (map[string]interface{}, error) {
return attributesForData(e.Data, e.graph.parent)
return attributesForData(e.Data, KeyForEdge, e.graph.parent)
}

// builds attributes map for specified data array
func attributesForData(data []*Data, gml *GraphML) (map[string]interface{}, error) {
func attributesForData(data []*Data, target KeyForElement, gml *GraphML) (map[string]interface{}, error) {
attr := make(map[string]interface{})
for _, d := range data {
key, ok := gml.keysById[d.Key]
Expand All @@ -456,6 +456,16 @@ func attributesForData(data []*Data, gml *GraphML) (map[string]interface{}, erro
attr[key.Name] = value
}
}
// fill defaults for undefined keys
for _, k := range keysForElement(gml.Keys, target) {
if _, ok := attr[k.Name]; !ok {
val, err := valueByType(k.DefaultValue, k.KeyType, gml.keyTypeDefault)
if err != nil {
return nil, err
}

Check warning on line 465 in graphml/graphml.go

View check run for this annotation

Codecov / codecov/patch

graphml/graphml.go#L464-L465

Added lines #L464 - L465 were not covered by tests
attr[k.Name] = val
}
}
return attr, nil
}

Expand Down Expand Up @@ -616,3 +626,13 @@ func valueByType(val string, keyType DataType, keyTypeDefault DataType) (interfa
return valueByType(val, keyTypeDefault, keyTypeDefault)
}
}

// keysForElement returns all the keys from allKeys that apply to a certain element
func keysForElement(allKeys []*Key, target KeyForElement) (keys []*Key) {
for _, k := range allKeys {
if k.Target == target || k.Target == KeyForAll {
keys = append(keys, k)
}
}
return keys
}
1 change: 1 addition & 0 deletions graphml/graphml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestGraphML_Decode_keyTypeDefault(t *testing.T) {
attributes := make(map[string]interface{})
attributes["integer-key"] = 10
attributes["test-key"] = "test data"
attributes["color"] = "yellow"

// check Graph element
//
Expand Down

0 comments on commit 5fcb4aa

Please sign in to comment.