diff.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/davecgh/go-spew/spew"
  21. "github.com/google/go-cmp/cmp"
  22. )
  23. // StringDiff diffs a and b and returns a human readable diff.
  24. func StringDiff(a, b string) string {
  25. ba := []byte(a)
  26. bb := []byte(b)
  27. out := []byte{}
  28. i := 0
  29. for ; i < len(ba) && i < len(bb); i++ {
  30. if ba[i] != bb[i] {
  31. break
  32. }
  33. out = append(out, ba[i])
  34. }
  35. out = append(out, []byte("\n\nA: ")...)
  36. out = append(out, ba[i:]...)
  37. out = append(out, []byte("\n\nB: ")...)
  38. out = append(out, bb[i:]...)
  39. out = append(out, []byte("\n\n")...)
  40. return string(out)
  41. }
  42. func legacyDiff(a, b interface{}) string {
  43. return cmp.Diff(a, b)
  44. }
  45. // ObjectDiff prints the diff of two go objects and fails if the objects
  46. // contain unhandled unexported fields.
  47. // DEPRECATED: use github.com/google/go-cmp/cmp.Diff
  48. func ObjectDiff(a, b interface{}) string {
  49. return legacyDiff(a, b)
  50. }
  51. // ObjectGoPrintDiff prints the diff of two go objects and fails if the objects
  52. // contain unhandled unexported fields.
  53. // DEPRECATED: use github.com/google/go-cmp/cmp.Diff
  54. func ObjectGoPrintDiff(a, b interface{}) string {
  55. return legacyDiff(a, b)
  56. }
  57. // ObjectReflectDiff prints the diff of two go objects and fails if the objects
  58. // contain unhandled unexported fields.
  59. // DEPRECATED: use github.com/google/go-cmp/cmp.Diff
  60. func ObjectReflectDiff(a, b interface{}) string {
  61. return legacyDiff(a, b)
  62. }
  63. // ObjectGoPrintSideBySide prints a and b as textual dumps side by side,
  64. // enabling easy visual scanning for mismatches.
  65. func ObjectGoPrintSideBySide(a, b interface{}) string {
  66. s := spew.ConfigState{
  67. Indent: " ",
  68. // Extra deep spew.
  69. DisableMethods: true,
  70. }
  71. sA := s.Sdump(a)
  72. sB := s.Sdump(b)
  73. linesA := strings.Split(sA, "\n")
  74. linesB := strings.Split(sB, "\n")
  75. width := 0
  76. for _, s := range linesA {
  77. l := len(s)
  78. if l > width {
  79. width = l
  80. }
  81. }
  82. for _, s := range linesB {
  83. l := len(s)
  84. if l > width {
  85. width = l
  86. }
  87. }
  88. buf := &bytes.Buffer{}
  89. w := tabwriter.NewWriter(buf, width, 0, 1, ' ', 0)
  90. max := len(linesA)
  91. if len(linesB) > max {
  92. max = len(linesB)
  93. }
  94. for i := 0; i < max; i++ {
  95. var a, b string
  96. if i < len(linesA) {
  97. a = linesA[i]
  98. }
  99. if i < len(linesB) {
  100. b = linesB[i]
  101. }
  102. fmt.Fprintf(w, "%s\t%s\n", a, b)
  103. }
  104. w.Flush()
  105. return buf.String()
  106. }
  107. // IgnoreUnset is an option that ignores fields that are unset on the right
  108. // hand side of a comparison. This is useful in testing to assert that an
  109. // object is a derivative.
  110. func IgnoreUnset() cmp.Option {
  111. return cmp.Options{
  112. // ignore unset fields in v2
  113. cmp.FilterPath(func(path cmp.Path) bool {
  114. _, v2 := path.Last().Values()
  115. switch v2.Kind() {
  116. case reflect.Slice, reflect.Map:
  117. if v2.IsNil() || v2.Len() == 0 {
  118. return true
  119. }
  120. case reflect.String:
  121. if v2.Len() == 0 {
  122. return true
  123. }
  124. case reflect.Interface, reflect.Ptr:
  125. if v2.IsNil() {
  126. return true
  127. }
  128. }
  129. return false
  130. }, cmp.Ignore()),
  131. // ignore map entries that aren't set in v2
  132. cmp.FilterPath(func(path cmp.Path) bool {
  133. switch i := path.Last().(type) {
  134. case cmp.MapIndex:
  135. if _, v2 := i.Values(); !v2.IsValid() {
  136. fmt.Println("E")
  137. return true
  138. }
  139. }
  140. return false
  141. }, cmp.Ignore()),
  142. }
  143. }