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

This commit is contained in:
kortschak
2017-06-08 13:38:43 +09:30
committed by Dan Kortschak
parent d3dceefefd
commit d49f26e4f8

View File

@@ -25,8 +25,8 @@ type Builder interface {
NewEdge(from, to graph.Node) graph.Edge NewEdge(from, to graph.Node) graph.Edge
} }
// UnmarshalerAttr is the interface implemented by objects that can unmarshal a // UnmarshalerAttr is implemented by types that can unmarshal a DOT
// DOT attribute description of themselves. // attribute description of themselves.
type UnmarshalerAttr interface { type UnmarshalerAttr interface {
// UnmarshalDOTAttr decodes a single DOT attribute. // UnmarshalDOTAttr decodes a single DOT attribute.
UnmarshalDOTAttr(attr Attribute) error UnmarshalDOTAttr(attr Attribute) error
@@ -103,16 +103,17 @@ func (gen *generator) node(dst Builder, id string) graph.Node {
func (gen *generator) addStmt(dst Builder, stmt ast.Stmt) { func (gen *generator) addStmt(dst Builder, stmt ast.Stmt) {
switch stmt := stmt.(type) { switch stmt := stmt.(type) {
case *ast.NodeStmt: case *ast.NodeStmt:
n := gen.node(dst, stmt.Node.ID) n, ok := gen.node(dst, stmt.Node.ID).(UnmarshalerAttr)
if n, ok := n.(UnmarshalerAttr); ok { if !ok {
for _, attr := range stmt.Attrs { return
a := Attribute{ }
Key: attr.Key, for _, attr := range stmt.Attrs {
Value: attr.Val, a := Attribute{
} Key: attr.Key,
if err := n.UnmarshalDOTAttr(a); err != nil { Value: attr.Val,
panic(fmt.Errorf("unable to unmarshal node DOT attribute (%s=%s)", a.Key, a.Value)) }
} if err := n.UnmarshalDOTAttr(a); err != nil {
panic(fmt.Errorf("unable to unmarshal node DOT attribute (%s=%s)", a.Key, a.Value))
} }
} }
case *ast.EdgeStmt: case *ast.EdgeStmt:
@@ -136,16 +137,17 @@ func (gen *generator) addEdgeStmt(dst Builder, e *ast.EdgeStmt) {
ts := gen.addEdge(dst, e.To) ts := gen.addEdge(dst, e.To)
for _, f := range fs { for _, f := range fs {
for _, t := range ts { for _, t := range ts {
edge := dst.NewEdge(f, t) edge, ok := dst.NewEdge(f, t).(UnmarshalerAttr)
if edge, ok := edge.(UnmarshalerAttr); ok { if !ok {
for _, attr := range e.Attrs { continue
a := Attribute{ }
Key: attr.Key, for _, attr := range e.Attrs {
Value: attr.Val, a := Attribute{
} Key: attr.Key,
if err := edge.UnmarshalDOTAttr(a); err != nil { Value: attr.Val,
panic(fmt.Errorf("unable to unmarshal edge DOT attribute (%s=%s)", a.Key, a.Value)) }
} if err := edge.UnmarshalDOTAttr(a); err != nil {
panic(fmt.Errorf("unable to unmarshal edge DOT attribute (%s=%s)", a.Key, a.Value))
} }
} }
} }