From eca8e7ec9e8dd8ab7e71224f340d4c8fde08d5a1 Mon Sep 17 00:00:00 2001 From: Nicolas Peugnet Date: Tue, 12 Dec 2023 12:18:40 +0100 Subject: [PATCH 1/2] Omit empty for attribute of key element See for (graph|node|edge|hyperedge|port|endpoint|all) "all" for="" is not a valid value, it should be omited if empty. --- graphml/graphml.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphml/graphml.go b/graphml/graphml.go index fbf4392..e46a03d 100644 --- a/graphml/graphml.go +++ b/graphml/graphml.go @@ -100,7 +100,7 @@ type Key struct { // The ID of this key element (in form dX, where X denotes the number of occurrences of the key element before the current one) ID string `xml:"id,attr"` // The name of element this key is for (graphml|graph|node|edge|hyperedge|port|endpoint|all) - Target KeyForElement `xml:"for,attr"` + Target KeyForElement `xml:"for,attr,omitempty"` // The name of data-function associated with this key Name string `xml:"attr.name,attr"` // The type of input to the data-function associated with this key. (Allowed values: boolean, int, long, float, double, string) From b0b1f5e5f6cbb864fd65b54fe48b740549a4aea0 Mon Sep 17 00:00:00 2001 From: Nicolas Peugnet Date: Tue, 12 Dec 2023 12:22:09 +0100 Subject: [PATCH 2/2] Add test case for empty for element (eca8e7e) --- graphml/graphml_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/graphml/graphml_test.go b/graphml/graphml_test.go index 826279d..bfd2fc1 100644 --- a/graphml/graphml_test.go +++ b/graphml/graphml_test.go @@ -234,6 +234,23 @@ func TestGraphML_Encode(t *testing.T) { assert.Equal(t, resString, outBuf.String()) } +func TestGraphML_Encode_EmptyFor(t *testing.T) { + // build GraphML + gml := NewGraphML("TestGraphML_Encode_EmptyFor") + + // register common data-function for all elements + keyForAllName := "keyForAll" + _, err := gml.RegisterKey("", keyForAllName, "", reflect.String, nil) + require.NoError(t, err, "failed to register key") + + // encode + outBuf := &bytes.Buffer{} + err = gml.Encode(outBuf, false) + + // check results + assert.NotContains(t, outBuf.String(), "for=\"\"", "a key with an empty target should omit the for attribute") +} + func TestGraphML_AddGraph(t *testing.T) { description := "test graph" gml := NewGraphML("")