mirror of
https://github.com/gonum/gonum.git
synced 2025-10-07 08:01:20 +08:00
graph/encoding: rename decoding API in encoding package
This commit is contained in:
@@ -14,17 +14,16 @@ import (
|
||||
"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.
|
||||
type UnmarshalerAttrs interface {
|
||||
// DOTUnmarshalerAttrs returns the global attribute unmarshalers.
|
||||
DOTUnmarshalerAttrs() (graph, node, edge encoding.UnmarshalerAttr)
|
||||
type AttributeSetters interface {
|
||||
// DOTAttributeSetters returns the global attribute setters.
|
||||
DOTAttributeSetters() (graph, node, edge encoding.AttributeSetter)
|
||||
}
|
||||
|
||||
// UnmarshalerID is implemented by types that can unmarshal a DOT ID.
|
||||
type UnmarshalerID interface {
|
||||
// UnmarshalDOTID decodes a single DOT ID.
|
||||
UnmarshalDOTID(id string)
|
||||
// DOTIDSetter is implemented by types that can set a DOT ID.
|
||||
type DOTIDSetter interface {
|
||||
SetDOTID(id string)
|
||||
}
|
||||
|
||||
// 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,
|
||||
ids: make(map[string]graph.Node),
|
||||
}
|
||||
if a, ok := dst.(UnmarshalerAttrs); ok {
|
||||
gen.graphAttr, gen.nodeAttr, gen.edgeAttr = a.DOTUnmarshalerAttrs()
|
||||
if a, ok := dst.(AttributeSetters); ok {
|
||||
gen.graphAttr, gen.nodeAttr, gen.edgeAttr = a.DOTAttributeSetters()
|
||||
}
|
||||
for _, stmt := range src.Stmts {
|
||||
gen.addStmt(dst, stmt)
|
||||
@@ -78,7 +77,7 @@ type generator struct {
|
||||
// corresponds to the start index of the active (or inner-most) subgraph.
|
||||
subStart []int
|
||||
// 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,
|
||||
@@ -89,8 +88,8 @@ func (gen *generator) node(dst encoding.Builder, id string) graph.Node {
|
||||
}
|
||||
n := dst.NewNode()
|
||||
dst.AddNode(n)
|
||||
if n, ok := n.(UnmarshalerID); ok {
|
||||
n.UnmarshalDOTID(id)
|
||||
if n, ok := n.(DOTIDSetter); ok {
|
||||
n.SetDOTID(id)
|
||||
}
|
||||
gen.ids[id] = n
|
||||
// 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) {
|
||||
switch stmt := stmt.(type) {
|
||||
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 {
|
||||
return
|
||||
}
|
||||
@@ -116,14 +115,14 @@ func (gen *generator) addStmt(dst encoding.Builder, stmt ast.Stmt) {
|
||||
Key: attr.Key,
|
||||
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))
|
||||
}
|
||||
}
|
||||
case *ast.EdgeStmt:
|
||||
gen.addEdgeStmt(dst, stmt)
|
||||
case *ast.AttrStmt:
|
||||
var n encoding.UnmarshalerAttr
|
||||
var n encoding.AttributeSetter
|
||||
var dst string
|
||||
switch stmt.Kind {
|
||||
case ast.GraphKind:
|
||||
@@ -152,7 +151,7 @@ func (gen *generator) addStmt(dst encoding.Builder, stmt ast.Stmt) {
|
||||
Key: attr.Key,
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -173,7 +172,7 @@ func (gen *generator) addEdgeStmt(dst encoding.Builder, e *ast.EdgeStmt) {
|
||||
ts := gen.addEdge(dst, e.To)
|
||||
for _, f := range fs {
|
||||
for _, t := range ts {
|
||||
edge, ok := dst.NewEdge(f, t).(encoding.UnmarshalerAttr)
|
||||
edge, ok := dst.NewEdge(f, t).(encoding.AttributeSetter)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
@@ -182,7 +181,7 @@ func (gen *generator) addEdgeStmt(dst encoding.Builder, e *ast.EdgeStmt) {
|
||||
Key: attr.Key,
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
@@ -134,8 +134,8 @@ func (g *dotDirectedGraph) DOTAttributers() (graph, node, edge encoding.Attribut
|
||||
return g.graph, g.node, g.edge
|
||||
}
|
||||
|
||||
// DOTUnmarshalerAttrs implements the dot.UnmarshalerAttrs interface.
|
||||
func (g *dotDirectedGraph) DOTUnmarshalerAttrs() (graph, node, edge encoding.UnmarshalerAttr) {
|
||||
// DOTAttributeSetters implements the dot.AttributeSetters interface.
|
||||
func (g *dotDirectedGraph) DOTAttributeSetters() (graph, node, edge encoding.AttributeSetter) {
|
||||
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.
|
||||
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
|
||||
}
|
||||
|
||||
@@ -194,13 +194,13 @@ func (n *dotNode) DOTID() string {
|
||||
return n.dotID
|
||||
}
|
||||
|
||||
// UnmarshalDOTID decodes a DOT ID.
|
||||
func (n *dotNode) UnmarshalDOTID(id string) {
|
||||
// SetDOTID sets a DOT ID.
|
||||
func (n *dotNode) SetDOTID(id string) {
|
||||
n.dotID = id
|
||||
}
|
||||
|
||||
// UnmarshalAttr decodes a single DOT attribute.
|
||||
func (n *dotNode) UnmarshalAttr(attr encoding.Attribute) error {
|
||||
// SetAttribute sets a DOT attribute.
|
||||
func (n *dotNode) SetAttribute(attr encoding.Attribute) error {
|
||||
if attr.Key != "label" {
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
attr := encoding.Attribute{
|
||||
return []encoding.Attribute{{
|
||||
Key: "label",
|
||||
Value: n.Label,
|
||||
}
|
||||
return []encoding.Attribute{attr}
|
||||
}}
|
||||
}
|
||||
|
||||
// dotEdge extends simple.Edge with a label field to test round-trip encoding and
|
||||
@@ -228,8 +227,8 @@ type dotEdge struct {
|
||||
Label string
|
||||
}
|
||||
|
||||
// UnmarshalAttr decodes a single DOT attribute.
|
||||
func (e *dotEdge) UnmarshalAttr(attr encoding.Attribute) error {
|
||||
// SetAttribute sets a DOT attribute.
|
||||
func (e *dotEdge) SetAttribute(attr encoding.Attribute) error {
|
||||
if attr.Key != "label" {
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
attr := encoding.Attribute{
|
||||
return []encoding.Attribute{{
|
||||
Key: "label",
|
||||
Value: e.Label,
|
||||
}
|
||||
return []encoding.Attribute{attr}
|
||||
}}
|
||||
}
|
||||
|
||||
// attributes is a helper for global attributes.
|
||||
@@ -255,7 +253,7 @@ type attributes []encoding.Attribute
|
||||
func (a attributes) Attributes() []encoding.Attribute {
|
||||
return []encoding.Attribute(a)
|
||||
}
|
||||
func (a *attributes) UnmarshalAttr(attr encoding.Attribute) error {
|
||||
func (a *attributes) SetAttribute(attr encoding.Attribute) error {
|
||||
*a = append(*a, attr)
|
||||
return nil
|
||||
}
|
||||
|
@@ -16,11 +16,10 @@ type Builder interface {
|
||||
NewEdge(from, to graph.Node) graph.Edge
|
||||
}
|
||||
|
||||
// UnmarshalerAttr is implemented by types that can unmarshal a graph
|
||||
// attribute description of themselves.
|
||||
type UnmarshalerAttr interface {
|
||||
// UnmarshalAttr decodes a single attribute.
|
||||
UnmarshalAttr(attr Attribute) error
|
||||
// AttributeSetter is implemented by types that can set an encoded graph
|
||||
// attribute.
|
||||
type AttributeSetter interface {
|
||||
SetAttribute(Attribute) error
|
||||
}
|
||||
|
||||
// Attributer defines graph.Node or graph.Edge values that can
|
||||
|
Reference in New Issue
Block a user