graph/encoding/dot: don't panic when an Attributers returns a nil
MODIFY
graph/encoding/dot/encode.go
graph/encoding/dot/encode.go
@@ -378,6 +378,9 @@ func (p *printer) writeAttributeComplex(ca Attributers) {
g, n, e := ca.DOTAttributers()
haveWrittenBlock := false
for i, a := range []encoding.Attributer{g, n, e} {
+ if a == nil {
+ continue
+ }
attributes := a.Attributes()
if len(attributes) == 0 {
continue

MODIFY
graph/encoding/dot/encode_test.go
graph/encoding/dot/encode_test.go
@@ -316,9 +316,9 @@ func undirectedPortedAttrGraphFrom(g []intset, attr [][]encoding.Attribute, port
type graphAttributer struct {
graph.Graph
- graph attributer
- node attributer
- edge attributer
+ graph encoding.Attributer
+ node encoding.Attributer
+ edge encoding.Attributer
}
type attributer []encoding.Attribute
@@ -1155,11 +1155,14 @@ var encodeTests = []struct {
// Handling graph attributes.
{
- g: graphAttributer{Graph: undirectedEdgeAttrGraphFrom(powerMethodGraph, map[edge][]encoding.Attribute{
+ g: graphAttributer{
+ Graph: undirectedEdgeAttrGraphFrom(powerMethodGraph, map[edge][]encoding.Attribute{
{from: 0, to: 2}: {{Key: "label", Value: `"???"`}, {Key: "style", Value: "dashed"}},
{from: 2, to: 4}: {},
{from: 3, to: 4}: {{Key: "color", Value: "red"}},
- })},
+ }),
+ graph: nil, node: nil, edge: nil,
+ },
want: `strict graph {
// Node definitions.
@@ -1183,13 +1186,15 @@ var encodeTests = []struct {
}`,
},
{
- g: graphAttributer{Graph: undirectedEdgeAttrGraphFrom(powerMethodGraph, map[edge][]encoding.Attribute{
+ g: graphAttributer{
+ Graph: undirectedEdgeAttrGraphFrom(powerMethodGraph, map[edge][]encoding.Attribute{
{from: 0, to: 2}: {{Key: "label", Value: `"???"`}, {Key: "style", Value: "dashed"}},
{from: 2, to: 4}: {},
{from: 3, to: 4}: {{Key: "color", Value: "red"}},
}),
- graph: []encoding.Attribute{{Key: "rankdir", Value: `"LR"`}},
- node: []encoding.Attribute{{Key: "fontsize", Value: "16"}, {Key: "shape", Value: "ellipse"}},
+ graph: attributer{{Key: "rankdir", Value: `"LR"`}},
+ node: attributer{{Key: "fontsize", Value: "16"}, {Key: "shape", Value: "ellipse"}},
+ edge: nil,
},
want: `strict graph {

graph/encoding/dot: don't panic when an Attributers returns a nil