Skip to content

Commit

Permalink
fix endless loop for nested lambda evaluation error
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Feb 17, 2019
1 parent ed9d8b6 commit 4eb6be5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
5 changes: 3 additions & 2 deletions dynaml/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ func short(val interface{}, all bool) string {
case map[string]yaml.Node:
s := "{"
sep := ""
for k, e := range v {
getSortedKeys(v)
for _, k := range getSortedKeys(v) {
if all || k != "_" {
s = fmt.Sprintf("%s%s%s: %s", s, sep, k, short(e.Value(), all))
s = fmt.Sprintf("%s%s%s: %s", s, sep, k, short(v[k].Value(), all))
sep = ", "
}
}
Expand Down
6 changes: 4 additions & 2 deletions flow/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ func (e DefaultEnvironment) Flow(source yaml.Node, shouldOverride bool) (yaml.No
debug.Debug("@@} ---> %+v\n", next)

next = Cleanup(next, updateBinding(next))
if reflect.DeepEqual(result, next) {
b := reflect.DeepEqual(result, next)
//b,r:=yaml.Equals(result, next,[]string{})
if b {
break
}

//fmt.Printf("****** found diff: %s\n", r)
result = next
}
debug.Debug("@@@ Done\n")
Expand Down
25 changes: 23 additions & 2 deletions spiff++.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,22 @@ func main() {
Name: "path",
Usage: "output is taken from given path",
},
cli.StringSliceFlag{
Name: "select",
Usage: "filter dedicated output fields",
Value: &cli.StringSlice{},
},
},
Action: func(c *cli.Context) {
if len(c.Args()) < 1 {
cli.ShowCommandHelp(c, "merge")
os.Exit(1)
}
debug.DebugFlag = c.Bool("debug")
merge(c.Args()[0], c.Bool("partial"), c.Bool("json"), c.Bool("split"), c.String("path"), c.Args()[1:])
merge(c.Args()[0], c.Bool("partial"),
c.Bool("json"), c.Bool("split"),
c.String("path"), c.StringSlice("select"),
c.Args()[1:])
},
},
{
Expand Down Expand Up @@ -108,7 +116,7 @@ func main() {
app.Run(os.Args)
}

func merge(templateFilePath string, partial bool, json, split bool, subpath string, stubFilePaths []string) {
func merge(templateFilePath string, partial bool, json, split bool, subpath string, selection []string, stubFilePaths []string) {
var templateFile []byte
var err error
var stdin = false
Expand Down Expand Up @@ -190,6 +198,19 @@ func merge(templateFilePath string, partial bool, json, split bool, subpath stri
}
flowed = node
}
if len(selection) > 0 {
new := map[string]yaml.Node{}
for _, p := range selection {
comps := strings.Split(p, ".")
node, ok := yaml.FindR(true, flowed, comps...)
if !ok {
log.Fatalln(fmt.Sprintf("path %q not found%s", subpath, doc))
}
new[comps[len(comps)-1]] = node

}
flowed = yaml.NewNode(new, "")
}
if split {
if list, ok := flowed.Value().([]yaml.Node); ok {
for _, d := range list {
Expand Down
61 changes: 61 additions & 0 deletions yaml/equals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package yaml

import (
"fmt"
"reflect"
)

func Equals(a, b Node, path []string) (bool, string) {

if reflect.TypeOf(a.Value()) != reflect.TypeOf(b.Value()) {
return false, fmt.Sprintf("non matching type %T!=%T at %+v", a.Value(), b.Value(), path)

}
if a.Value() == nil && b.Value() == nil {
return true, ""
}
if a.Value() == nil || b.Value() == nil {
return false, fmt.Sprintf("found non-matching nil at %+v", path)
}

if !reflect.DeepEqual(a.GetAnnotation(), b.GetAnnotation()) {
return false, fmt.Sprintf("annotation diff at %+v: %v", path, a.GetAnnotation())
}
switch va := a.Value().(type) {
case []Node:
vb := b.Value().([]Node)
if len(va) != len(vb) {
return false, fmt.Sprintf("list length mismatch %d!=%s at %+v", len(va), len(vb), path)
}
for i, v := range va {
if b, r := Equals(v, vb[i], append(path, fmt.Sprintf("[%d]", i))); !b {
return b, r
}
}
case map[string]Node:
vb := b.Value().(map[string]Node)

for k, v := range va {
_, ok := vb[k]
if !ok {
return false, fmt.Sprintf("key %q missing in b at %+v", k, path)
}
if b, r := Equals(v, vb[k], append(path, k)); !b {
return b, r
}
}
for k := range vb {
_, ok := va[k]
if !ok {
return false, fmt.Sprintf("additional key %q in b at %+v", k, path)
}
}
default:
e := reflect.DeepEqual(a.Value(), b.Value())
if !e {
return false, fmt.Sprintf("diff at %+v: %v!=%v", path, a.Value(), b.Value())
}

}
return true, ""
}

0 comments on commit 4eb6be5

Please sign in to comment.