diff.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package diff
  14. import (
  15. "bytes"
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "text/tabwriter"
  20. "github.com/google/go-cmp/cmp"
  21. "k8s.io/apimachinery/pkg/util/dump"
  22. )
  23. func legacyDiff(a, b interface{}) string {
  24. return cmp.Diff(a, b)
  25. }
  26. // StringDiff diffs a and b and returns a human readable diff.
  27. // DEPRECATED: use github.com/google/go-cmp/cmp.Diff
  28. func StringDiff(a, b string) string {
  29. return legacyDiff(a, b)
  30. }
  31. // ObjectDiff prints the diff of two go objects and fails if the objects
  32. // contain unhandled unexported fields.
  33. // DEPRECATED: use github.com/google/go-cmp/cmp.Diff
  34. func ObjectDiff(a, b interface{}) string {
  35. return legacyDiff(a, b)
  36. }
  37. // ObjectGoPrintDiff prints the diff of two go objects and fails if the objects
  38. // contain unhandled unexported fields.
  39. // DEPRECATED: use github.com/google/go-cmp/cmp.Diff
  40. func ObjectGoPrintDiff(a, b interface{}) string {
  41. return legacyDiff(a, b)
  42. }
  43. // ObjectReflectDiff prints the diff of two go objects and fails if the objects
  44. // contain unhandled unexported fields.
  45. // DEPRECATED: use github.com/google/go-cmp/cmp.Diff
  46. func ObjectReflectDiff(a, b interface{}) string {
  47. return legacyDiff(a, b)
  48. }
  49. // ObjectGoPrintSideBySide prints a and b as textual dumps side by side,
  50. // enabling easy visual scanning for mismatches.
  51. func ObjectGoPrintSideBySide(a, b interface{}) string {
  52. sA := dump.Pretty(a)
  53. sB := dump.Pretty(b)
  54. linesA := strings.Split(sA, "\n")
  55. linesB := strings.Split(sB, "\n")
  56. width := 0
  57. for _, s := range linesA {
  58. l := len(s)
  59. if l > width {
  60. width = l
  61. }
  62. }
  63. for _, s := range linesB {
  64. l := len(s)
  65. if l > width {
  66. width = l
  67. }
  68. }
  69. buf := &bytes.Buffer{}
  70. w := tabwriter.NewWriter(buf, width, 0, 1, ' ', 0)
  71. max := len(linesA)
  72. if len(linesB) > max {
  73. max = len(linesB)
  74. }
  75. for i := 0; i < max; i++ {
  76. var a, b string
  77. if i < len(linesA) {
  78. a = linesA[i]
  79. }
  80. if i < len(linesB) {
  81. b = linesB[i]
  82. }
  83. fmt.Fprintf(w, "%s\t%s\n", a, b)
  84. }
  85. w.Flush()
  86. return buf.String()
  87. }
  88. // IgnoreUnset is an option that ignores fields that are unset on the right
  89. // hand side of a comparison. This is useful in testing to assert that an
  90. // object is a derivative.
  91. func IgnoreUnset() cmp.Option {
  92. return cmp.Options{
  93. // ignore unset fields in v2
  94. cmp.FilterPath(func(path cmp.Path) bool {
  95. _, v2 := path.Last().Values()
  96. switch v2.Kind() {
  97. case reflect.Slice, reflect.Map:
  98. if v2.IsNil() || v2.Len() == 0 {
  99. return true
  100. }
  101. case reflect.String:
  102. if v2.Len() == 0 {
  103. return true
  104. }
  105. case reflect.Interface, reflect.Pointer:
  106. if v2.IsNil() {
  107. return true
  108. }
  109. }
  110. return false
  111. }, cmp.Ignore()),
  112. // ignore map entries that aren't set in v2
  113. cmp.FilterPath(func(path cmp.Path) bool {
  114. switch i := path.Last().(type) {
  115. case cmp.MapIndex:
  116. if _, v2 := i.Values(); !v2.IsValid() {
  117. fmt.Println("E")
  118. return true
  119. }
  120. }
  121. return false
  122. }, cmp.Ignore()),
  123. }
  124. }