|
| 1 | +/* |
| 2 | +Copyright 2023 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmp |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | +) |
| 26 | + |
| 27 | +// SimpleUnstructuredReporter is a simple reporter for Unstructured objects |
| 28 | +// that only records differences detected during comparison, in a diff-like |
| 29 | +// format. |
| 30 | +type SimpleUnstructuredReporter struct { |
| 31 | + path cmp.Path |
| 32 | + diffs []string |
| 33 | +} |
| 34 | + |
| 35 | +// Report writes a diff entry if rs is not equal. In the format of: |
| 36 | +// |
| 37 | +// spec.replicas |
| 38 | +// -3 |
| 39 | +// +1 |
| 40 | +// |
| 41 | +// spec.template.spec.containers.[0].command.[6] |
| 42 | +// ---deleted=true |
| 43 | +// |
| 44 | +// spec.template.spec.containers.[0].env.[?->1]: |
| 45 | +// +map[name:ADDED] |
| 46 | +func (r *SimpleUnstructuredReporter) Report(rs cmp.Result) { |
| 47 | + if !rs.Equal() { |
| 48 | + vx, vy := r.path.Last().Values() |
| 49 | + isNonEmptyX := isValidAndNonEmpty(vx) |
| 50 | + isNonEmptyY := isValidAndNonEmpty(vy) |
| 51 | + |
| 52 | + if !isNonEmptyX && !isNonEmptyY { |
| 53 | + // Skip empty values. |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + var sb strings.Builder |
| 58 | + sb.WriteString(pathToString(r.path)) |
| 59 | + sb.WriteString("\n") |
| 60 | + if isNonEmptyX { |
| 61 | + sb.WriteString(fmt.Sprintf("-%v", vx)) |
| 62 | + sb.WriteString("\n") |
| 63 | + } |
| 64 | + if isNonEmptyY { |
| 65 | + sb.WriteString(fmt.Sprintf("+%v", vy)) |
| 66 | + sb.WriteString("\n") |
| 67 | + } |
| 68 | + r.diffs = append(r.diffs, sb.String()) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// String returns the diff entries joined together with newline, trimmed from |
| 73 | +// spaces. |
| 74 | +func (r *SimpleUnstructuredReporter) String() string { |
| 75 | + return strings.TrimSpace(strings.Join(r.diffs, "\n")) |
| 76 | +} |
| 77 | + |
| 78 | +func (r *SimpleUnstructuredReporter) PushStep(ps cmp.PathStep) { |
| 79 | + r.path = append(r.path, ps) |
| 80 | +} |
| 81 | + |
| 82 | +func (r *SimpleUnstructuredReporter) PopStep() { |
| 83 | + r.path = r.path[:len(r.path)-1] |
| 84 | +} |
| 85 | + |
| 86 | +func isValidAndNonEmpty(v reflect.Value) bool { |
| 87 | + if !v.IsValid() { |
| 88 | + return false |
| 89 | + } |
| 90 | + switch v.Kind() { |
| 91 | + case reflect.Interface: |
| 92 | + return isValidAndNonEmpty(v.Elem()) |
| 93 | + case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String: |
| 94 | + return v.Len() > 0 |
| 95 | + } |
| 96 | + return true |
| 97 | +} |
| 98 | + |
| 99 | +func pathToString(path cmp.Path) (p string) { |
| 100 | + for _, step := range path { |
| 101 | + switch t := step.(type) { |
| 102 | + case cmp.MapIndex: |
| 103 | + p += fmt.Sprintf(".%v", t.Key()) |
| 104 | + case cmp.SliceIndex: |
| 105 | + p += fmt.Sprintf("%v", t.String()) |
| 106 | + } |
| 107 | + } |
| 108 | + p = strings.TrimPrefix(p, ".") |
| 109 | + return |
| 110 | +} |
0 commit comments