github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/diff.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     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 util
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"strings"
    24  	"text/tabwriter"
    25  
    26  	"github.com/davecgh/go-spew/spew"
    27  )
    28  
    29  // StringDiff diffs a and b and returns a human readable diff.
    30  func 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  
    49  // ObjectDiff writes the two objects out as JSON and prints out the identical part of
    50  // the objects followed by the remaining part of 'a' and finally the remaining part of 'b'.
    51  // For debugging tests.
    52  func ObjectDiff(a, b interface{}) string {
    53  	ab, err := json.Marshal(a)
    54  	if err != nil {
    55  		panic(fmt.Sprintf("a: %v", err))
    56  	}
    57  	bb, err := json.Marshal(b)
    58  	if err != nil {
    59  		panic(fmt.Sprintf("b: %v", err))
    60  	}
    61  	return StringDiff(string(ab), string(bb))
    62  }
    63  
    64  // ObjectGoPrintDiff is like ObjectDiff, but uses go-spew to print the objects,
    65  // which shows absolutely everything by recursing into every single pointer
    66  // (go's %#v formatters OTOH stop at a certain point). This is needed when you
    67  // can't figure out why reflect.DeepEqual is returning false and nothing is
    68  // showing you differences. This will.
    69  func ObjectGoPrintDiff(a, b interface{}) string {
    70  	s := spew.ConfigState{DisableMethods: true}
    71  	return StringDiff(
    72  		s.Sprintf("%#v", a),
    73  		s.Sprintf("%#v", b),
    74  	)
    75  }
    76  
    77  // ObjectGoPrintSideBySide prints a and b as textual dumps side by side,
    78  // enabling easy visual scanning for mismatches.
    79  func ObjectGoPrintSideBySide(a, b interface{}) string {
    80  	s := spew.ConfigState{
    81  		Indent: " ",
    82  		// Extra deep spew.
    83  		DisableMethods: true,
    84  	}
    85  	sA := s.Sdump(a)
    86  	sB := s.Sdump(b)
    87  
    88  	linesA := strings.Split(sA, "\n")
    89  	linesB := strings.Split(sB, "\n")
    90  	width := 0
    91  	for _, s := range linesA {
    92  		l := len(s)
    93  		if l > width {
    94  			width = l
    95  		}
    96  	}
    97  	for _, s := range linesB {
    98  		l := len(s)
    99  		if l > width {
   100  			width = l
   101  		}
   102  	}
   103  	buf := &bytes.Buffer{}
   104  	w := tabwriter.NewWriter(buf, width, 0, 1, ' ', 0)
   105  	max := len(linesA)
   106  	if len(linesB) > max {
   107  		max = len(linesB)
   108  	}
   109  	for i := 0; i < max; i++ {
   110  		var a, b string
   111  		if i < len(linesA) {
   112  			a = linesA[i]
   113  		}
   114  		if i < len(linesB) {
   115  			b = linesB[i]
   116  		}
   117  		fmt.Fprintf(w, "%s\t%s\n", a, b)
   118  	}
   119  	w.Flush()
   120  	return buf.String()
   121  }