Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Don't work around omitempty issue with *[]
Browse files Browse the repository at this point in the history
See golang/go#7233. Prefer correct Go over slightly
lighter XML.
  • Loading branch information
mvdan committed Sep 17, 2015
1 parent b327010 commit c0897f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
30 changes: 15 additions & 15 deletions gexf.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ type Graph struct {
DefEdge EdgeType `xml:"defaultedgetype,attr,omitempty"`

Attrs []ClassAttrs `xml:"attributes,omitempty"`
Nodes *[]Node `xml:"nodes>node,omitempty"`
Edges *[]Edge `xml:"edges>edge,omitempty"`
Nodes []Node `xml:"nodes>node,omitempty"`
Edges []Edge `xml:"edges>edge,omitempty"`
}

type EdgeType int
Expand Down Expand Up @@ -169,12 +169,12 @@ type Attr struct {
}

type Node struct {
ID string `xml:"id,attr"`
Label string `xml:"label,attr,omitempty"`
Attrs *[]AttrVal `xml:"attvalues>attvalue,omiempty"`
Size *Size `xml:"http://www.gexf.net/1.2draft/viz size,omitempty"`
Pos *Pos `xml:"http://www.gexf.net/1.2draft/viz position,omitempty"`
Color *Color `xml:"http://www.gexf.net/1.2draft/viz color,omitempty"`
ID string `xml:"id,attr"`
Label string `xml:"label,attr,omitempty"`
Attrs []AttrVal `xml:"attvalues>attvalue,omiempty"`
Size *Size `xml:"http://www.gexf.net/1.2draft/viz size,omitempty"`
Pos *Pos `xml:"http://www.gexf.net/1.2draft/viz position,omitempty"`
Color *Color `xml:"http://www.gexf.net/1.2draft/viz color,omitempty"`
}

type Size struct {
Expand All @@ -199,11 +199,11 @@ type AttrVal struct {
}

type Edge struct {
ID string `xml:"id,attr"`
Label string `xml:"label,attr,omitempty"`
Type EdgeType `xml:"type,attr,omitempty"`
Source string `xml:"source,attr"`
Target string `xml:"target,attr"`
Weight float64 `xml:"weight,attr,omitempty"`
Attrs *[]AttrVal `xml:"attvalues>attvalue,omiempty"`
ID string `xml:"id,attr"`
Label string `xml:"label,attr,omitempty"`
Type EdgeType `xml:"type,attr,omitempty"`
Source string `xml:"source,attr"`
Target string `xml:"target,attr"`
Weight float64 `xml:"weight,attr,omitempty"`
Attrs []AttrVal `xml:"attvalues>attvalue,omiempty"`
}
27 changes: 18 additions & 9 deletions gexf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ func TestHelloWorld(t *testing.T) {
</meta>
<graph>
<nodes>
<node id="0" label="Hello"></node>
<node id="1" label="World"></node>
<node id="0" label="Hello">
<attvalues></attvalues>
</node>
<node id="1" label="World">
<attvalues></attvalues>
</node>
</nodes>
<edges>
<edge id="0" source="0" target="1"></edge>
<edge id="0" source="0" target="1">
<attvalues></attvalues>
</edge>
</edges>
</graph>
</gexf>`
Expand All @@ -53,7 +59,7 @@ func TestHelloWorld(t *testing.T) {
doc.Graph = Graph{
Mode: Static,
DefEdge: Directed,
Nodes: &[]Node{
Nodes: []Node{
{
ID: "0",
Label: "Hello",
Expand All @@ -63,7 +69,7 @@ func TestHelloWorld(t *testing.T) {
Label: "World",
},
},
Edges: &[]Edge{
Edges: []Edge{
{
ID: "0",
Source: "0",
Expand Down Expand Up @@ -102,6 +108,7 @@ func TestAttributes(t *testing.T) {
</attvalues>
</node>
</nodes>
<edges></edges>
</graph>
</gexf>`
doc := New()
Expand Down Expand Up @@ -135,11 +142,11 @@ func TestAttributes(t *testing.T) {
},
},
},
Nodes: &[]Node{
Nodes: []Node{
{
ID: "0",
Label: "Gephi",
Attrs: &[]AttrVal{
Attrs: []AttrVal{
{
For: "0",
Value: "http://gephi.org",
Expand All @@ -153,7 +160,7 @@ func TestAttributes(t *testing.T) {
{
ID: "1",
Label: "Webatlas",
Attrs: &[]AttrVal{
Attrs: []AttrVal{
{
For: "1",
Value: "2",
Expand All @@ -178,9 +185,11 @@ func TestViz(t *testing.T) {
<graph>
<nodes>
<node id="0" label="Hello">
<attvalues></attvalues>
<size xmlns="http://www.gexf.net/1.2draft/viz" value="20.5"></size>
</node>
</nodes>
<edges></edges>
</graph>
</gexf>`
doc := New()
Expand All @@ -192,7 +201,7 @@ func TestViz(t *testing.T) {
doc.Graph = Graph{
Mode: Static,
DefEdge: Directed,
Nodes: &[]Node{
Nodes: []Node{
{
ID: "0",
Label: "Hello",
Expand Down

2 comments on commit c0897f1

@sshikaree
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slice already returns a pointer, so **[] returns ***.

@mvdan
Copy link
Owner Author

@mvdan mvdan commented on c0897f1 Sep 18, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of using *[] was that omitempty would hide the empty <nodes> element. The pointer to a slice was a pain and wrong as far as Go goes, so I reverted to not using a pointer and leaving the empty XML elements present.

Please sign in to comment.