From e67d97d9dae3ba73cb1112ebd08204765dba42dc Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 13 May 2017 13:55:42 -0400 Subject: [PATCH] pgraph: Replace CompareMatch with VertexMatchFn This removes a reference to the resources package in pgraph. --- pgraph/pgraph.go | 28 +++++++++++++++++++--------- yamlgraph/gconfig.go | 17 +++++++++++++++-- yamlgraph2/gconfig.go | 17 +++++++++++++++-- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/pgraph/pgraph.go b/pgraph/pgraph.go index 1c3ff759..50b200aa 100644 --- a/pgraph/pgraph.go +++ b/pgraph/pgraph.go @@ -226,15 +226,18 @@ func (g *Graph) DeleteEdge(e *Edge) { } } -// CompareMatch searches for an equivalent resource in the graph and returns the -// vertex it is found in, or nil if not found. -func (g *Graph) CompareMatch(obj resources.Res) *Vertex { +// VertexMatchFn searches for a vertex in the graph and returns the vertex if +// one matches. It uses a user defined function to match. That function must +// 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 { - if v.Res.Compare(obj) { - return v + if b, err := fn(v); err != nil { + 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. @@ -602,7 +605,14 @@ func (g *Graph) GraphSync(oldGraph *Graph) (*Graph, error) { // step one, direct compare with res.Compare 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. @@ -640,8 +650,8 @@ func (g *Graph) GraphSync(oldGraph *Graph) (*Graph, error) { // lookup vertices (these should exist now) //res1 := v1.Res // resource //res2 := v2.Res - //vertex1 := oldGraph.CompareMatch(res1) - //vertex2 := oldGraph.CompareMatch(res2) + //vertex1 := oldGraph.CompareMatch(res1) // now: VertexMatchFn + //vertex2 := oldGraph.CompareMatch(res2) // now: VertexMatchFn vertex1, exists1 := lookup[v1] vertex2, exists2 := lookup[v2] if !exists1 || !exists2 { // no match found, bug? diff --git a/yamlgraph/gconfig.go b/yamlgraph/gconfig.go index 5d557e87..43d63a62 100644 --- a/yamlgraph/gconfig.go +++ b/yamlgraph/gconfig.go @@ -136,7 +136,13 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World, // XXX: should we export based on a @@ prefix, or a metaparam // like exported => true || exported => (host pattern)||(other pattern?) 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 v = pgraph.NewVertex(res) 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 { 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 v = pgraph.NewVertex(res) graph.AddVertex(v) // call standalone in case not part of an edge diff --git a/yamlgraph2/gconfig.go b/yamlgraph2/gconfig.go index f52843ee..0a691791 100644 --- a/yamlgraph2/gconfig.go +++ b/yamlgraph2/gconfig.go @@ -182,7 +182,13 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World, // XXX: should we export based on a @@ prefix, or a metaparam // like exported => true || exported => (host pattern)||(other pattern?) 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 v = pgraph.NewVertex(res) 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 { 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 v = pgraph.NewVertex(res) graph.AddVertex(v) // call standalone in case not part of an edge