graph/endcoding/dot: tidy up decode code/docs
MODIFY
graph/encoding/dot/decode.go
graph/encoding/dot/decode.go
@@ -25,8 +25,8 @@ type Builder interface {
NewEdge(from, to graph.Node) graph.Edge
}
-// UnmarshalerAttr is the interface implemented by objects that can unmarshal a
-// DOT attribute description of themselves.
+// UnmarshalerAttr is implemented by types that can unmarshal a DOT
+// attribute description of themselves.
type UnmarshalerAttr interface {
// UnmarshalDOTAttr decodes a single DOT attribute.
UnmarshalDOTAttr(attr Attribute) error
@@ -103,8 +103,10 @@ func (gen *generator) node(dst Builder, id string) graph.Node {
func (gen *generator) addStmt(dst Builder, stmt ast.Stmt) {
switch stmt := stmt.(type) {
case *ast.NodeStmt:
- n := gen.node(dst, stmt.Node.ID)
- if n, ok := n.(UnmarshalerAttr); ok {
+ n, ok := gen.node(dst, stmt.Node.ID).(UnmarshalerAttr)
+ if !ok {
+ return
+ }
for _, attr := range stmt.Attrs {
a := Attribute{
Key: attr.Key,
@@ -114,7 +116,6 @@ func (gen *generator) addStmt(dst Builder, stmt ast.Stmt) {
panic(fmt.Errorf("unable to unmarshal node DOT attribute (%s=%s)", a.Key, a.Value))
}
}
- }
case *ast.EdgeStmt:
gen.addEdgeStmt(dst, stmt)
case *ast.AttrStmt:
@@ -136,8 +137,10 @@ func (gen *generator) addEdgeStmt(dst Builder, e *ast.EdgeStmt) {
ts := gen.addEdge(dst, e.To)
for _, f := range fs {
for _, t := range ts {
- edge := dst.NewEdge(f, t)
- if edge, ok := edge.(UnmarshalerAttr); ok {
+ edge, ok := dst.NewEdge(f, t).(UnmarshalerAttr)
+ if !ok {
+ continue
+ }
for _, attr := range e.Attrs {
a := Attribute{
Key: attr.Key,
@@ -150,7 +153,6 @@ func (gen *generator) addEdgeStmt(dst Builder, e *ast.EdgeStmt) {
}
}
}
-}
// addVertex adds the given vertex to the graph, and returns its set of nodes.
func (gen *generator) addVertex(dst Builder, v ast.Vertex) []graph.Node {

graph/endcoding/dot: tidy up decode code/docs