pgraph: Replace CompareMatch with VertexMatchFn
This removes a reference to the resources package in pgraph.
This commit is contained in:
@@ -226,15 +226,18 @@ func (g *Graph) DeleteEdge(e *Edge) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompareMatch searches for an equivalent resource in the graph and returns the
|
// VertexMatchFn searches for a vertex in the graph and returns the vertex if
|
||||||
// vertex it is found in, or nil if not found.
|
// one matches. It uses a user defined function to match. That function must
|
||||||
func (g *Graph) CompareMatch(obj resources.Res) *Vertex {
|
// return true on match, and an error if anything goes wrong.
|
||||||
|
func (g *Graph) VertexMatchFn(fn func(*Vertex) (bool, error)) (*Vertex, error) {
|
||||||
for v := range g.adjacency {
|
for v := range g.adjacency {
|
||||||
if v.Res.Compare(obj) {
|
if b, err := fn(v); err != nil {
|
||||||
return v
|
return nil, errwrap.Wrapf(err, "fn in VertexMatchFn() errored")
|
||||||
|
} else if b {
|
||||||
|
return v, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil, nil // nothing found
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: consider adding a mutate API.
|
// TODO: consider adding a mutate API.
|
||||||
@@ -602,7 +605,14 @@ func (g *Graph) GraphSync(oldGraph *Graph) (*Graph, error) {
|
|||||||
|
|
||||||
// step one, direct compare with res.Compare
|
// step one, direct compare with res.Compare
|
||||||
if vertex == nil { // redundant guard for consistency
|
if vertex == nil { // redundant guard for consistency
|
||||||
vertex = oldGraph.CompareMatch(res)
|
fn := func(v *Vertex) (bool, error) {
|
||||||
|
return v.Res.Compare(res), nil
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
vertex, err = oldGraph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not VertexMatchFn() resource")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: consider adding a mutate API.
|
// TODO: consider adding a mutate API.
|
||||||
@@ -640,8 +650,8 @@ func (g *Graph) GraphSync(oldGraph *Graph) (*Graph, error) {
|
|||||||
// lookup vertices (these should exist now)
|
// lookup vertices (these should exist now)
|
||||||
//res1 := v1.Res // resource
|
//res1 := v1.Res // resource
|
||||||
//res2 := v2.Res
|
//res2 := v2.Res
|
||||||
//vertex1 := oldGraph.CompareMatch(res1)
|
//vertex1 := oldGraph.CompareMatch(res1) // now: VertexMatchFn
|
||||||
//vertex2 := oldGraph.CompareMatch(res2)
|
//vertex2 := oldGraph.CompareMatch(res2) // now: VertexMatchFn
|
||||||
vertex1, exists1 := lookup[v1]
|
vertex1, exists1 := lookup[v1]
|
||||||
vertex2, exists2 := lookup[v2]
|
vertex2, exists2 := lookup[v2]
|
||||||
if !exists1 || !exists2 { // no match found, bug?
|
if !exists1 || !exists2 { // no match found, bug?
|
||||||
|
|||||||
@@ -136,7 +136,13 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
|
|||||||
// XXX: should we export based on a @@ prefix, or a metaparam
|
// XXX: should we export based on a @@ prefix, or a metaparam
|
||||||
// like exported => true || exported => (host pattern)||(other pattern?)
|
// like exported => true || exported => (host pattern)||(other pattern?)
|
||||||
if !strings.HasPrefix(res.GetName(), "@@") { // not exported resource
|
if !strings.HasPrefix(res.GetName(), "@@") { // not exported resource
|
||||||
v := graph.CompareMatch(res)
|
fn := func(v *pgraph.Vertex) (bool, error) {
|
||||||
|
return v.Res.Compare(res), nil
|
||||||
|
}
|
||||||
|
v, err := graph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not VertexMatchFn() resource")
|
||||||
|
}
|
||||||
if v == nil { // no match found
|
if v == nil { // no match found
|
||||||
v = pgraph.NewVertex(res)
|
v = pgraph.NewVertex(res)
|
||||||
graph.AddVertex(v) // call standalone in case not part of an edge
|
graph.AddVertex(v) // call standalone in case not part of an edge
|
||||||
@@ -207,7 +213,14 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
|
|||||||
if _, exists := lookup[kind]; !exists {
|
if _, exists := lookup[kind]; !exists {
|
||||||
lookup[kind] = make(map[string]*pgraph.Vertex)
|
lookup[kind] = make(map[string]*pgraph.Vertex)
|
||||||
}
|
}
|
||||||
v := graph.CompareMatch(res)
|
|
||||||
|
fn := func(v *pgraph.Vertex) (bool, error) {
|
||||||
|
return v.Res.Compare(res), nil
|
||||||
|
}
|
||||||
|
v, err := graph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not VertexMatchFn() resource")
|
||||||
|
}
|
||||||
if v == nil { // no match found
|
if v == nil { // no match found
|
||||||
v = pgraph.NewVertex(res)
|
v = pgraph.NewVertex(res)
|
||||||
graph.AddVertex(v) // call standalone in case not part of an edge
|
graph.AddVertex(v) // call standalone in case not part of an edge
|
||||||
|
|||||||
@@ -182,7 +182,13 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
|
|||||||
// XXX: should we export based on a @@ prefix, or a metaparam
|
// XXX: should we export based on a @@ prefix, or a metaparam
|
||||||
// like exported => true || exported => (host pattern)||(other pattern?)
|
// like exported => true || exported => (host pattern)||(other pattern?)
|
||||||
if !strings.HasPrefix(res.GetName(), "@@") { // not exported resource
|
if !strings.HasPrefix(res.GetName(), "@@") { // not exported resource
|
||||||
v := graph.CompareMatch(res)
|
fn := func(v *pgraph.Vertex) (bool, error) {
|
||||||
|
return v.Res.Compare(res), nil
|
||||||
|
}
|
||||||
|
v, err := graph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not VertexMatchFn() resource")
|
||||||
|
}
|
||||||
if v == nil { // no match found
|
if v == nil { // no match found
|
||||||
v = pgraph.NewVertex(res)
|
v = pgraph.NewVertex(res)
|
||||||
graph.AddVertex(v) // call standalone in case not part of an edge
|
graph.AddVertex(v) // call standalone in case not part of an edge
|
||||||
@@ -253,7 +259,14 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
|
|||||||
if _, exists := lookup[kind]; !exists {
|
if _, exists := lookup[kind]; !exists {
|
||||||
lookup[kind] = make(map[string]*pgraph.Vertex)
|
lookup[kind] = make(map[string]*pgraph.Vertex)
|
||||||
}
|
}
|
||||||
v := graph.CompareMatch(res)
|
|
||||||
|
fn := func(v *pgraph.Vertex) (bool, error) {
|
||||||
|
return v.Res.Compare(res), nil
|
||||||
|
}
|
||||||
|
v, err := graph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not VertexMatchFn() resource")
|
||||||
|
}
|
||||||
if v == nil { // no match found
|
if v == nil { // no match found
|
||||||
v = pgraph.NewVertex(res)
|
v = pgraph.NewVertex(res)
|
||||||
graph.AddVertex(v) // call standalone in case not part of an edge
|
graph.AddVertex(v) // call standalone in case not part of an edge
|
||||||
|
|||||||
Reference in New Issue
Block a user