-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hidde Beydals <[email protected]>
- Loading branch information
Showing
4 changed files
with
408 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2023 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmp | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
// SimpleUnstructuredReporter is a simple reporter for Unstructured objects | ||
// that only records differences detected during comparison, in a diff-like | ||
// format. | ||
type SimpleUnstructuredReporter struct { | ||
path cmp.Path | ||
diffs []string | ||
} | ||
|
||
// Report writes a diff entry if rs is not equal. In the format of: | ||
// | ||
// spec.replicas | ||
// -3 | ||
// +1 | ||
// | ||
// spec.template.spec.containers.[0].command.[6] | ||
// ---deleted=true | ||
// | ||
// spec.template.spec.containers.[0].env.[?->1]: | ||
// +map[name:ADDED] | ||
func (r *SimpleUnstructuredReporter) Report(rs cmp.Result) { | ||
if !rs.Equal() { | ||
vx, vy := r.path.Last().Values() | ||
isNonEmptyX := isValidAndNonEmpty(vx) | ||
isNonEmptyY := isValidAndNonEmpty(vy) | ||
|
||
if !isNonEmptyX && !isNonEmptyY { | ||
// Skip empty values. | ||
return | ||
} | ||
|
||
var sb strings.Builder | ||
sb.WriteString(pathToString(r.path)) | ||
sb.WriteString("\n") | ||
if isNonEmptyX { | ||
sb.WriteString(fmt.Sprintf("-%v", vx)) | ||
sb.WriteString("\n") | ||
} | ||
if isNonEmptyY { | ||
sb.WriteString(fmt.Sprintf("+%v", vy)) | ||
sb.WriteString("\n") | ||
} | ||
r.diffs = append(r.diffs, sb.String()) | ||
} | ||
} | ||
|
||
// String returns the diff entries joined together with newline, trimmed from | ||
// spaces. | ||
func (r *SimpleUnstructuredReporter) String() string { | ||
return strings.TrimSpace(strings.Join(r.diffs, "\n")) | ||
} | ||
|
||
func (r *SimpleUnstructuredReporter) PushStep(ps cmp.PathStep) { | ||
r.path = append(r.path, ps) | ||
} | ||
|
||
func (r *SimpleUnstructuredReporter) PopStep() { | ||
r.path = r.path[:len(r.path)-1] | ||
} | ||
|
||
func isValidAndNonEmpty(v reflect.Value) bool { | ||
if !v.IsValid() { | ||
return false | ||
} | ||
switch v.Kind() { | ||
case reflect.Interface: | ||
return isValidAndNonEmpty(v.Elem()) | ||
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String: | ||
return v.Len() > 0 | ||
} | ||
return true | ||
} | ||
|
||
func pathToString(path cmp.Path) (p string) { | ||
for _, step := range path { | ||
switch t := step.(type) { | ||
case cmp.MapIndex: | ||
p += fmt.Sprintf(".%v", t.Key()) | ||
case cmp.SliceIndex: | ||
p += fmt.Sprintf("%v", t.String()) | ||
} | ||
} | ||
p = strings.TrimPrefix(p, ".") | ||
return | ||
} |
Oops, something went wrong.