graph/encoding: rename decoding API in encoding package

This commit is contained in:
kortschak
2017-08-04 08:57:17 +09:30
committed by Dan Kortschak
parent 8e3d6da27f
commit 4d30eb012e
3 changed files with 36 additions and 40 deletions

View File

@@ -14,17 +14,16 @@ import (
"gonum.org/v1/gonum/graph/internal/set" "gonum.org/v1/gonum/graph/internal/set"
) )
// UnmashalerAttrs is implemented by graph values that can unmarshal global // AttributeSetters is implemented by graph values that can set global
// DOT attributes. // DOT attributes.
type UnmarshalerAttrs interface { type AttributeSetters interface {
// DOTUnmarshalerAttrs returns the global attribute unmarshalers. // DOTAttributeSetters returns the global attribute setters.
DOTUnmarshalerAttrs() (graph, node, edge encoding.UnmarshalerAttr) DOTAttributeSetters() (graph, node, edge encoding.AttributeSetter)
} }
// UnmarshalerID is implemented by types that can unmarshal a DOT ID. // DOTIDSetter is implemented by types that can set a DOT ID.
type UnmarshalerID interface { type DOTIDSetter interface {
// UnmarshalDOTID decodes a single DOT ID. SetDOTID(id string)
UnmarshalDOTID(id string)
} }
// Unmarshal parses the Graphviz DOT-encoded data and stores the result in dst. // Unmarshal parses the Graphviz DOT-encoded data and stores the result in dst.
@@ -55,8 +54,8 @@ func copyGraph(dst encoding.Builder, src *ast.Graph) (err error) {
directed: src.Directed, directed: src.Directed,
ids: make(map[string]graph.Node), ids: make(map[string]graph.Node),
} }
if a, ok := dst.(UnmarshalerAttrs); ok { if a, ok := dst.(AttributeSetters); ok {
gen.graphAttr, gen.nodeAttr, gen.edgeAttr = a.DOTUnmarshalerAttrs() gen.graphAttr, gen.nodeAttr, gen.edgeAttr = a.DOTAttributeSetters()
} }
for _, stmt := range src.Stmts { for _, stmt := range src.Stmts {
gen.addStmt(dst, stmt) gen.addStmt(dst, stmt)
@@ -78,7 +77,7 @@ type generator struct {
// corresponds to the start index of the active (or inner-most) subgraph. // corresponds to the start index of the active (or inner-most) subgraph.
subStart []int subStart []int
// graphAttr, nodeAttr and edgeAttr are global graph attributes. // graphAttr, nodeAttr and edgeAttr are global graph attributes.
graphAttr, nodeAttr, edgeAttr encoding.UnmarshalerAttr graphAttr, nodeAttr, edgeAttr encoding.AttributeSetter
} }
// node returns the gonum node corresponding to the given dot AST node ID, // node returns the gonum node corresponding to the given dot AST node ID,
@@ -89,8 +88,8 @@ func (gen *generator) node(dst encoding.Builder, id string) graph.Node {
} }
n := dst.NewNode() n := dst.NewNode()
dst.AddNode(n) dst.AddNode(n)
if n, ok := n.(UnmarshalerID); ok { if n, ok := n.(DOTIDSetter); ok {
n.UnmarshalDOTID(id) n.SetDOTID(id)
} }
gen.ids[id] = n gen.ids[id] = n
// Check if within the context of a subgraph, that is to be used as a vertex // Check if within the context of a subgraph, that is to be used as a vertex
@@ -107,7 +106,7 @@ func (gen *generator) node(dst encoding.Builder, id string) graph.Node {
func (gen *generator) addStmt(dst encoding.Builder, stmt ast.Stmt) { func (gen *generator) addStmt(dst encoding.Builder, stmt ast.Stmt) {
switch stmt := stmt.(type) { switch stmt := stmt.(type) {
case *ast.NodeStmt: case *ast.NodeStmt:
n, ok := gen.node(dst, stmt.Node.ID).(encoding.UnmarshalerAttr) n, ok := gen.node(dst, stmt.Node.ID).(encoding.AttributeSetter)
if !ok { if !ok {
return return
} }
@@ -116,14 +115,14 @@ func (gen *generator) addStmt(dst encoding.Builder, stmt ast.Stmt) {
Key: attr.Key, Key: attr.Key,
Value: attr.Val, Value: attr.Val,
} }
if err := n.UnmarshalAttr(a); err != nil { if err := n.SetAttribute(a); err != nil {
panic(fmt.Errorf("unable to unmarshal node DOT attribute (%s=%s)", a.Key, a.Value)) panic(fmt.Errorf("unable to unmarshal node DOT attribute (%s=%s)", a.Key, a.Value))
} }
} }
case *ast.EdgeStmt: case *ast.EdgeStmt:
gen.addEdgeStmt(dst, stmt) gen.addEdgeStmt(dst, stmt)
case *ast.AttrStmt: case *ast.AttrStmt:
var n encoding.UnmarshalerAttr var n encoding.AttributeSetter
var dst string var dst string
switch stmt.Kind { switch stmt.Kind {
case ast.GraphKind: case ast.GraphKind:
@@ -152,7 +151,7 @@ func (gen *generator) addStmt(dst encoding.Builder, stmt ast.Stmt) {
Key: attr.Key, Key: attr.Key,
Value: attr.Val, Value: attr.Val,
} }
if err := n.UnmarshalAttr(a); err != nil { if err := n.SetAttribute(a); err != nil {
panic(fmt.Errorf("unable to unmarshal global %s DOT attribute (%s=%s)", dst, a.Key, a.Value)) panic(fmt.Errorf("unable to unmarshal global %s DOT attribute (%s=%s)", dst, a.Key, a.Value))
} }
} }
@@ -173,7 +172,7 @@ func (gen *generator) addEdgeStmt(dst encoding.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, ok := dst.NewEdge(f, t).(encoding.UnmarshalerAttr) edge, ok := dst.NewEdge(f, t).(encoding.AttributeSetter)
if !ok { if !ok {
continue continue
} }
@@ -182,7 +181,7 @@ func (gen *generator) addEdgeStmt(dst encoding.Builder, e *ast.EdgeStmt) {
Key: attr.Key, Key: attr.Key,
Value: attr.Val, Value: attr.Val,
} }
if err := edge.UnmarshalAttr(a); err != nil { if err := edge.SetAttribute(a); err != nil {
panic(fmt.Errorf("unable to unmarshal edge DOT attribute (%s=%s)", a.Key, a.Value)) panic(fmt.Errorf("unable to unmarshal edge DOT attribute (%s=%s)", a.Key, a.Value))
} }
} }

View File

@@ -134,8 +134,8 @@ func (g *dotDirectedGraph) DOTAttributers() (graph, node, edge encoding.Attribut
return g.graph, g.node, g.edge return g.graph, g.node, g.edge
} }
// DOTUnmarshalerAttrs implements the dot.UnmarshalerAttrs interface. // DOTAttributeSetters implements the dot.AttributeSetters interface.
func (g *dotDirectedGraph) DOTUnmarshalerAttrs() (graph, node, edge encoding.UnmarshalerAttr) { func (g *dotDirectedGraph) DOTAttributeSetters() (graph, node, edge encoding.AttributeSetter) {
return &g.graph, &g.node, &g.edge return &g.graph, &g.node, &g.edge
} }
@@ -176,7 +176,7 @@ func (g *dotUndirectedGraph) DOTAttributers() (graph, node, edge encoding.Attrib
} }
// DOTUnmarshalerAttrs implements the dot.UnmarshalerAttrs interface. // DOTUnmarshalerAttrs implements the dot.UnmarshalerAttrs interface.
func (g *dotUndirectedGraph) DOTUnmarshalerAttrs() (graph, node, edge encoding.UnmarshalerAttr) { func (g *dotUndirectedGraph) DOTAttributeSetters() (graph, node, edge encoding.AttributeSetter) {
return &g.graph, &g.node, &g.edge return &g.graph, &g.node, &g.edge
} }
@@ -194,13 +194,13 @@ func (n *dotNode) DOTID() string {
return n.dotID return n.dotID
} }
// UnmarshalDOTID decodes a DOT ID. // SetDOTID sets a DOT ID.
func (n *dotNode) UnmarshalDOTID(id string) { func (n *dotNode) SetDOTID(id string) {
n.dotID = id n.dotID = id
} }
// UnmarshalAttr decodes a single DOT attribute. // SetAttribute sets a DOT attribute.
func (n *dotNode) UnmarshalAttr(attr encoding.Attribute) error { func (n *dotNode) SetAttribute(attr encoding.Attribute) error {
if attr.Key != "label" { if attr.Key != "label" {
return fmt.Errorf("unable to unmarshal node DOT attribute with key %q", attr.Key) return fmt.Errorf("unable to unmarshal node DOT attribute with key %q", attr.Key)
} }
@@ -213,11 +213,10 @@ func (n *dotNode) Attributes() []encoding.Attribute {
if len(n.Label) == 0 { if len(n.Label) == 0 {
return nil return nil
} }
attr := encoding.Attribute{ return []encoding.Attribute{{
Key: "label", Key: "label",
Value: n.Label, Value: n.Label,
} }}
return []encoding.Attribute{attr}
} }
// dotEdge extends simple.Edge with a label field to test round-trip encoding and // dotEdge extends simple.Edge with a label field to test round-trip encoding and
@@ -228,8 +227,8 @@ type dotEdge struct {
Label string Label string
} }
// UnmarshalAttr decodes a single DOT attribute. // SetAttribute sets a DOT attribute.
func (e *dotEdge) UnmarshalAttr(attr encoding.Attribute) error { func (e *dotEdge) SetAttribute(attr encoding.Attribute) error {
if attr.Key != "label" { if attr.Key != "label" {
return fmt.Errorf("unable to unmarshal node DOT attribute with key %q", attr.Key) return fmt.Errorf("unable to unmarshal node DOT attribute with key %q", attr.Key)
} }
@@ -242,11 +241,10 @@ func (e *dotEdge) Attributes() []encoding.Attribute {
if len(e.Label) == 0 { if len(e.Label) == 0 {
return nil return nil
} }
attr := encoding.Attribute{ return []encoding.Attribute{{
Key: "label", Key: "label",
Value: e.Label, Value: e.Label,
} }}
return []encoding.Attribute{attr}
} }
// attributes is a helper for global attributes. // attributes is a helper for global attributes.
@@ -255,7 +253,7 @@ type attributes []encoding.Attribute
func (a attributes) Attributes() []encoding.Attribute { func (a attributes) Attributes() []encoding.Attribute {
return []encoding.Attribute(a) return []encoding.Attribute(a)
} }
func (a *attributes) UnmarshalAttr(attr encoding.Attribute) error { func (a *attributes) SetAttribute(attr encoding.Attribute) error {
*a = append(*a, attr) *a = append(*a, attr)
return nil return nil
} }

View File

@@ -16,11 +16,10 @@ type Builder interface {
NewEdge(from, to graph.Node) graph.Edge NewEdge(from, to graph.Node) graph.Edge
} }
// UnmarshalerAttr is implemented by types that can unmarshal a graph // AttributeSetter is implemented by types that can set an encoded graph
// attribute description of themselves. // attribute.
type UnmarshalerAttr interface { type AttributeSetter interface {
// UnmarshalAttr decodes a single attribute. SetAttribute(Attribute) error
UnmarshalAttr(attr Attribute) error
} }
// Attributer defines graph.Node or graph.Edge values that can // Attributer defines graph.Node or graph.Edge values that can