1/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package diff
18
19import (
20	"bytes"
21	"fmt"
22	"strings"
23	"text/tabwriter"
24
25	"github.com/davecgh/go-spew/spew"
26	"github.com/google/go-cmp/cmp"
27)
28
29// StringDiff diffs a and b and returns a human readable diff.
30func StringDiff(a, b string) string {
31	ba := []byte(a)
32	bb := []byte(b)
33	out := []byte{}
34	i := 0
35	for ; i < len(ba) && i < len(bb); i++ {
36		if ba[i] != bb[i] {
37			break
38		}
39		out = append(out, ba[i])
40	}
41	out = append(out, []byte("\n\nA: ")...)
42	out = append(out, ba[i:]...)
43	out = append(out, []byte("\n\nB: ")...)
44	out = append(out, bb[i:]...)
45	out = append(out, []byte("\n\n")...)
46	return string(out)
47}
48
49func legacyDiff(a, b interface{}) string {
50	return cmp.Diff(a, b)
51}
52
53// ObjectDiff prints the diff of two go objects and fails if the objects
54// contain unhandled unexported fields.
55// DEPRECATED: use github.com/google/go-cmp/cmp.Diff
56func ObjectDiff(a, b interface{}) string {
57	return legacyDiff(a, b)
58}
59
60// ObjectGoPrintDiff prints the diff of two go objects and fails if the objects
61// contain unhandled unexported fields.
62// DEPRECATED: use github.com/google/go-cmp/cmp.Diff
63func ObjectGoPrintDiff(a, b interface{}) string {
64	return legacyDiff(a, b)
65}
66
67// ObjectReflectDiff prints the diff of two go objects and fails if the objects
68// contain unhandled unexported fields.
69// DEPRECATED: use github.com/google/go-cmp/cmp.Diff
70func ObjectReflectDiff(a, b interface{}) string {
71	return legacyDiff(a, b)
72}
73
74// ObjectGoPrintSideBySide prints a and b as textual dumps side by side,
75// enabling easy visual scanning for mismatches.
76func ObjectGoPrintSideBySide(a, b interface{}) string {
77	s := spew.ConfigState{
78		Indent: " ",
79		// Extra deep spew.
80		DisableMethods: true,
81	}
82	sA := s.Sdump(a)
83	sB := s.Sdump(b)
84
85	linesA := strings.Split(sA, "\n")
86	linesB := strings.Split(sB, "\n")
87	width := 0
88	for _, s := range linesA {
89		l := len(s)
90		if l > width {
91			width = l
92		}
93	}
94	for _, s := range linesB {
95		l := len(s)
96		if l > width {
97			width = l
98		}
99	}
100	buf := &bytes.Buffer{}
101	w := tabwriter.NewWriter(buf, width, 0, 1, ' ', 0)
102	max := len(linesA)
103	if len(linesB) > max {
104		max = len(linesB)
105	}
106	for i := 0; i < max; i++ {
107		var a, b string
108		if i < len(linesA) {
109			a = linesA[i]
110		}
111		if i < len(linesB) {
112			b = linesB[i]
113		}
114		fmt.Fprintf(w, "%s\t%s\n", a, b)
115	}
116	w.Flush()
117	return buf.String()
118}
119