lang: Move the Edge struct into the interfaces package

This makes it consumable from more than one package and avoids future
cycles.
This commit is contained in:
James Shubin
2022-11-11 20:28:22 -05:00
parent 76e0345609
commit 7c394bf735
3 changed files with 27 additions and 25 deletions

View File

@@ -5508,7 +5508,7 @@ func (obj *ExprList) Graph() (*pgraph.Graph, error) {
}
fieldName := fmt.Sprintf("%d", index) // argNames as integers!
edge := &funcs.Edge{Args: []string{fieldName}}
edge := &interfaces.FuncEdge{Args: []string{fieldName}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -5984,7 +5984,7 @@ func (obj *ExprMap) Graph() (*pgraph.Graph, error) {
}
// do the key names ever change? -- yes
fieldName := fmt.Sprintf("key:%d", index) // stringify map key
edge := &funcs.Edge{Args: []string{fieldName}}
edge := &interfaces.FuncEdge{Args: []string{fieldName}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -6004,7 +6004,7 @@ func (obj *ExprMap) Graph() (*pgraph.Graph, error) {
return nil, err
}
fieldName := fmt.Sprintf("val:%d", index) // stringify map val
edge := &funcs.Edge{Args: []string{fieldName}}
edge := &interfaces.FuncEdge{Args: []string{fieldName}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -6396,7 +6396,7 @@ func (obj *ExprStruct) Graph() (*pgraph.Graph, error) {
}
fieldName := x.Name
edge := &funcs.Edge{Args: []string{fieldName}}
edge := &interfaces.FuncEdge{Args: []string{fieldName}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -7209,7 +7209,7 @@ func (obj *ExprFunc) Graph() (*pgraph.Graph, error) {
// when we add an edge from this, then we'll get two because the
// contents aren't linked.
name := "body" // TODO: what should we name this?
edge := &funcs.Edge{Args: []string{name}}
edge := &interfaces.FuncEdge{Args: []string{name}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -8221,9 +8221,9 @@ func (obj *ExprCall) Graph() (*pgraph.Graph, error) {
//argName := fmt.Sprintf("%d", pos) // indexed!
argName := argNames[pos]
edge := &funcs.Edge{Args: []string{argName}}
edge := &interfaces.FuncEdge{Args: []string{argName}}
// TODO: replace with:
//edge := &funcs.Edge{Args: []string{fmt.Sprintf("arg:%s", argName)}}
//edge := &interfaces.FuncEdge{Args: []string{fmt.Sprintf("arg:%s", argName)}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -8253,7 +8253,7 @@ func (obj *ExprCall) Graph() (*pgraph.Graph, error) {
return nil, err
}
edge := &funcs.Edge{Args: []string{fmt.Sprintf("call:%s", obj.Name)}}
edge := &interfaces.FuncEdge{Args: []string{fmt.Sprintf("call:%s", obj.Name)}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -8536,7 +8536,7 @@ func (obj *ExprVar) Graph() (*pgraph.Graph, error) {
return nil, err
}
edge := &funcs.Edge{Args: []string{fmt.Sprintf("var:%s", obj.Name)}}
edge := &interfaces.FuncEdge{Args: []string{fmt.Sprintf("var:%s", obj.Name)}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
@@ -8949,7 +8949,7 @@ func (obj *ExprIf) Graph() (*pgraph.Graph, error) {
return nil, err
}
edge := &funcs.Edge{Args: []string{argName}}
edge := &interfaces.FuncEdge{Args: []string{argName}}
var once bool
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {

View File

@@ -19,7 +19,6 @@ package funcs
import (
"fmt"
"strings"
"sync"
"github.com/purpleidea/mgmt/engine"
@@ -86,17 +85,6 @@ func (obj *State) String() string {
return obj.Expr.String()
}
// Edge links an output vertex (value) to an input vertex with a named argument.
type Edge struct {
Args []string // list of named args that this edge sends to
}
// String displays the list of arguments this edge satisfies. It is a required
// property to be a valid pgraph.Edge.
func (obj *Edge) String() string {
return strings.Join(obj.Args, ", ")
}
// Engine represents the running time varying directed acyclic function graph.
type Engine struct {
Graph *pgraph.Graph
@@ -205,7 +193,7 @@ func (obj *Engine) Validate() error {
ptrs = append(ptrs, node.handle)
}
for _, edge := range obj.Graph.Edges() {
if _, ok := edge.(*Edge); !ok {
if _, ok := edge.(*interfaces.FuncEdge); !ok {
e := fmt.Errorf("edge `%s` was not the correct type", edge)
err = errwrap.Append(err, e)
}
@@ -250,7 +238,7 @@ func (obj *Engine) Validate() error {
node1 := obj.state[vertex1]
for vertex2, edge := range obj.Graph.Adjacency()[vertex1] {
node2 := obj.state[vertex2]
edge := edge.(*Edge)
edge := edge.(*interfaces.FuncEdge)
// check vertex1 -> vertex2 (with e) is valid
for _, arg := range edge.Args { // loop over each arg
@@ -378,7 +366,7 @@ func (obj *Engine) Run() error {
}
st := types.NewStruct(si)
for _, v := range incoming {
args := obj.Graph.Adjacency()[v][vertex].(*Edge).Args
args := obj.Graph.Adjacency()[v][vertex].(*interfaces.FuncEdge).Args
from := obj.state[v]
obj.mutex.RLock()
value, exists := obj.table[v]

View File

@@ -18,6 +18,8 @@
package interfaces
import (
"strings"
"github.com/purpleidea/mgmt/engine"
"github.com/purpleidea/mgmt/lang/types"
)
@@ -187,3 +189,15 @@ type DataFunc interface {
// context.
SetData(*FuncData)
}
// FuncEdge links an output vertex (value) to an input vertex with a named
// argument.
type FuncEdge struct {
Args []string // list of named args that this edge sends to
}
// String displays the list of arguments this edge satisfies. It is a required
// property to be a valid pgraph.Edge.
func (obj *FuncEdge) String() string {
return strings.Join(obj.Args, ", ")
}