diff --git a/cmd/yamldiff/yamldiff.go b/cmd/yamldiff/yamldiff.go index 0a2255c..3d3b21c 100644 --- a/cmd/yamldiff/yamldiff.go +++ b/cmd/yamldiff/yamldiff.go @@ -46,12 +46,17 @@ func Type(elem any) TypeId { } } -func subtract(yaml1 yaml.MapSlice, yaml2 yaml.MapSlice) yaml.MapSlice { +// hack to be able to compare slices and dictionires that cannot be put into a map. +func strval(v any) string { + return fmt.Sprintf("%v", v) +} + +func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice { res := make(yaml.MapSlice, 0) for _, item := range yaml2 { k := item.Key - v1 := item.Value - v2 := yaml1.ToMap()[k] + v2 := item.Value + v1 := yaml1.ToMap()[k] switch { case reflect.DeepEqual(v1, v2): // delete is implicit by not copying to the output @@ -61,9 +66,29 @@ func subtract(yaml1 yaml.MapSlice, yaml2 yaml.MapSlice) yaml.MapSlice { res = append(res, item) } case Type(v1) == Slice && Type(v2) == Slice: - if !reflect.DeepEqual(v1, v2) { - res = append(res, item) + v2set := make(map[string]bool) + v1set := make(map[string]bool) + stringToValue := make(map[any]any) + for _, v := range v2.([]any) { + sval := strval(v) + stringToValue[sval] = v + v2set[sval] = true } + for _, v := range v1.([]any) { + v1set[strval(v)] = true + } + s := make([]any, 0) + for k2, _ := range v2set { + if v1set[k2] { + s = append(s, "") + } else { + s = append(s, stringToValue[k2]) + } + } + res = append(res, yaml.MapItem{ + Key: k, + Value: s, + }) default: res = append(res, item) } @@ -77,6 +102,7 @@ func main() { Usage: yamldiff ") Shows changes and additions made in w.r.t. `) + os.Exit(1) } file1 := os.Args[1] file2 := os.Args[2] @@ -84,7 +110,7 @@ Usage: yamldiff ") yaml1 := parse(read(file1)) yaml2 := parse(read(file2)) - subtract(yaml2, yaml1) + yaml2 = subtract(yaml2, yaml1) enc := yaml.NewEncoder(os.Stdout, yaml.UseLiteralStyleIfMultiline(true),