pax_global_header00006660000000000000000000000064121431276760014523gustar00rootroot0000000000000052 comment=bc9499caa0f45ee5edb2f0209fbd61fbf3d9018f golang-pretty-0.0~git20130613/000077500000000000000000000000001214312767600157005ustar00rootroot00000000000000golang-pretty-0.0~git20130613/.gitignore000066400000000000000000000000331214312767600176640ustar00rootroot00000000000000[568].out _go* _test* _obj golang-pretty-0.0~git20130613/License000066400000000000000000000020341214312767600172040ustar00rootroot00000000000000Copyright 2012 Keith Rarick Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-pretty-0.0~git20130613/Readme000066400000000000000000000002501214312767600170150ustar00rootroot00000000000000package pretty import "github.com/kr/pretty" Package pretty provides pretty-printing for Go values. Documentation http://godoc.org/github.com/kr/pretty golang-pretty-0.0~git20130613/diff.go000066400000000000000000000061341214312767600171430ustar00rootroot00000000000000package pretty import ( "fmt" "io" "reflect" ) type sbuf []string func (s *sbuf) Write(b []byte) (int, error) { *s = append(*s, string(b)) return len(b), nil } // Diff returns a slice where each element describes // a difference between a and b. func Diff(a, b interface{}) (desc []string) { Fdiff((*sbuf)(&desc), a, b) return desc } // Fdiff writes to w a description of the differences between a and b. func Fdiff(w io.Writer, a, b interface{}) { diffWriter{w: w}.diff(reflect.ValueOf(a), reflect.ValueOf(b)) } type diffWriter struct { w io.Writer l string // label } func (w diffWriter) printf(f string, a ...interface{}) { var l string if w.l != "" { l = w.l + ": " } fmt.Fprintf(w.w, l+f, a...) } func (w diffWriter) diff(av, bv reflect.Value) { if !av.IsValid() && bv.IsValid() { w.printf("nil != %#v", bv.Interface()) return } if av.IsValid() && !bv.IsValid() { w.printf("%#v != nil", av.Interface()) return } if !av.IsValid() && !bv.IsValid() { return } at := av.Type() bt := bv.Type() if at != bt { w.printf("%v != %v", at, bt) return } // numeric types, including bool if at.Kind() < reflect.Array { a, b := av.Interface(), bv.Interface() if a != b { w.printf("%#v != %#v", a, b) } return } switch at.Kind() { case reflect.String: a, b := av.Interface(), bv.Interface() if a != b { w.printf("%q != %q", a, b) } case reflect.Ptr: switch { case av.IsNil() && !bv.IsNil(): w.printf("nil != %v", bv.Interface()) case !av.IsNil() && bv.IsNil(): w.printf("%v != nil", av.Interface()) case !av.IsNil() && !bv.IsNil(): w.diff(av.Elem(), bv.Elem()) } case reflect.Struct: for i := 0; i < av.NumField(); i++ { w.relabel(at.Field(i).Name).diff(av.Field(i), bv.Field(i)) } case reflect.Map: ak, both, bk := keyDiff(av.MapKeys(), bv.MapKeys()) for _, k := range ak { w := w.relabel(fmt.Sprintf("[%#v]", k.Interface())) w.printf("%q != (missing)", av.MapIndex(k)) } for _, k := range both { w := w.relabel(fmt.Sprintf("[%#v]", k.Interface())) w.diff(av.MapIndex(k), bv.MapIndex(k)) } for _, k := range bk { w := w.relabel(fmt.Sprintf("[%#v]", k.Interface())) w.printf("(missing) != %q", bv.MapIndex(k)) } case reflect.Interface: w.diff(reflect.ValueOf(av.Interface()), reflect.ValueOf(bv.Interface())) default: if !reflect.DeepEqual(av.Interface(), bv.Interface()) { w.printf("%# v != %# v", Formatter(av.Interface()), Formatter(bv.Interface())) } } } func (d diffWriter) relabel(name string) (d1 diffWriter) { d1 = d if d.l != "" && name[0] != '[' { d1.l += "." } d1.l += name return d1 } func keyDiff(a, b []reflect.Value) (ak, both, bk []reflect.Value) { for _, av := range a { inBoth := false for _, bv := range b { if reflect.DeepEqual(av.Interface(), bv.Interface()) { inBoth = true both = append(both, av) break } } if !inBoth { ak = append(ak, av) } } for _, bv := range b { inBoth := false for _, av := range a { if reflect.DeepEqual(av.Interface(), bv.Interface()) { inBoth = true break } } if !inBoth { bk = append(bk, bv) } } return } golang-pretty-0.0~git20130613/diff_test.go000066400000000000000000000027441214312767600202050ustar00rootroot00000000000000package pretty import ( "testing" ) type difftest struct { a interface{} b interface{} exp []string } type S struct { A int S *S I interface{} C []int } var diffs = []difftest{ {a: nil, b: nil}, {a: S{A: 1}, b: S{A: 1}}, {0, "", []string{`int != string`}}, {0, 1, []string{`0 != 1`}}, {S{}, new(S), []string{`pretty.S != *pretty.S`}}, {"a", "b", []string{`"a" != "b"`}}, {S{}, S{A: 1}, []string{`A: 0 != 1`}}, {new(S), &S{A: 1}, []string{`A: 0 != 1`}}, {S{S: new(S)}, S{S: &S{A: 1}}, []string{`S.A: 0 != 1`}}, {S{}, S{I: 0}, []string{`I: nil != 0`}}, {S{I: 1}, S{I: "x"}, []string{`I: int != string`}}, {S{}, S{C: []int{1}}, []string{`C: []int(nil) != []int{1}`}}, {S{C: []int{}}, S{C: []int{1}}, []string{`C: []int{} != []int{1}`}}, {S{}, S{A: 1, S: new(S)}, []string{`A: 0 != 1`, `S: nil != &{0 []}`}}, } func TestDiff(t *testing.T) { for _, tt := range diffs { got := Diff(tt.a, tt.b) eq := len(got) == len(tt.exp) if eq { for i := range got { eq = eq && got[i] == tt.exp[i] } } if !eq { t.Errorf("diffing % #v", tt.a) t.Errorf("with % #v", tt.b) diffdiff(t, got, tt.exp) continue } } } func diffdiff(t *testing.T, got, exp []string) { minus(t, "unexpected:", got, exp) minus(t, "missing:", exp, got) } func minus(t *testing.T, s string, a, b []string) { var i, j int for i = 0; i < len(a); i++ { for j = 0; j < len(b); j++ { if a[i] == b[j] { break } } if j == len(b) { t.Error(s, a[i]) } } } golang-pretty-0.0~git20130613/example_test.go000066400000000000000000000004621214312767600207230ustar00rootroot00000000000000package pretty_test import ( "fmt" "github.com/kr/pretty" ) func Example() { type myType struct { a, b int } var x = []myType{{1, 2}, {3, 4}, {5, 6}} fmt.Printf("%# v", pretty.Formatter(x)) // output: // []pretty_test.myType{ // {a:1, b:2}, // {a:3, b:4}, // {a:5, b:6}, // } } golang-pretty-0.0~git20130613/formatter.go000066400000000000000000000150501214312767600202330ustar00rootroot00000000000000package pretty import ( "fmt" "github.com/kr/text" "io" "reflect" "strconv" "text/tabwriter" ) const ( limit = 50 ) type formatter struct { x interface{} force bool quote bool } // Formatter makes a wrapper, f, that will format x as go source with line // breaks and tabs. Object f responds to the "%v" formatting verb when both the // "#" and " " (space) flags are set, for example: // // fmt.Sprintf("%# v", Formatter(x)) // // If one of these two flags is not set, or any other verb is used, f will // format x according to the usual rules of package fmt. // In particular, if x satisfies fmt.Formatter, then x.Format will be called. func Formatter(x interface{}) (f fmt.Formatter) { return formatter{x: x, quote: true} } func (fo formatter) String() string { return fmt.Sprint(fo.x) // unwrap it } func (fo formatter) passThrough(f fmt.State, c rune) { s := "%" for i := 0; i < 128; i++ { if f.Flag(i) { s += string(i) } } if w, ok := f.Width(); ok { s += fmt.Sprintf("%d", w) } if p, ok := f.Precision(); ok { s += fmt.Sprintf(".%d", p) } s += string(c) fmt.Fprintf(f, s, fo.x) } func (fo formatter) Format(f fmt.State, c rune) { if fo.force || c == 'v' && f.Flag('#') && f.Flag(' ') { w := tabwriter.NewWriter(f, 4, 4, 1, ' ', 0) p := &printer{tw: w, Writer: w} p.printValue(reflect.ValueOf(fo.x), true, fo.quote) w.Flush() return } fo.passThrough(f, c) } type printer struct { io.Writer tw *tabwriter.Writer } func (p *printer) indent() *printer { q := *p q.tw = tabwriter.NewWriter(p.Writer, 4, 4, 1, ' ', 0) q.Writer = text.NewIndentWriter(q.tw, []byte{'\t'}) return &q } func (p *printer) printInline(v reflect.Value, x interface{}, showType bool) { if showType { io.WriteString(p, v.Type().String()) fmt.Fprintf(p, "(%#v)", x) } else { fmt.Fprintf(p, "%#v", x) } } func (p *printer) printValue(v reflect.Value, showType, quote bool) { switch v.Kind() { case reflect.Bool: p.printInline(v, v.Bool(), showType) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: p.printInline(v, v.Int(), showType) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: p.printInline(v, v.Uint(), showType) case reflect.Float32, reflect.Float64: p.printInline(v, v.Float(), showType) case reflect.Complex64, reflect.Complex128: fmt.Fprintf(p, "%#v", v.Complex()) case reflect.String: p.fmtString(v.String(), quote) case reflect.Map: t := v.Type() if showType { io.WriteString(p, t.String()) } writeByte(p, '{') if nonzero(v) { expand := !canInline(v.Type()) pp := p if expand { writeByte(p, '\n') pp = p.indent() } keys := v.MapKeys() for i := 0; i < v.Len(); i++ { showTypeInStruct := true k := keys[i] mv := v.MapIndex(k) pp.printValue(k, false, true) writeByte(pp, ':') if expand { writeByte(pp, '\t') } showTypeInStruct = t.Elem().Kind() == reflect.Interface pp.printValue(mv, showTypeInStruct, true) if expand { io.WriteString(pp, ",\n") } else if i < v.Len()-1 { io.WriteString(pp, ", ") } } if expand { pp.tw.Flush() } } writeByte(p, '}') case reflect.Struct: t := v.Type() if showType { io.WriteString(p, t.String()) } writeByte(p, '{') if nonzero(v) { expand := !canInline(v.Type()) pp := p if expand { writeByte(p, '\n') pp = p.indent() } for i := 0; i < v.NumField(); i++ { showTypeInStruct := true if f := t.Field(i); f.Name != "" { io.WriteString(pp, f.Name) writeByte(pp, ':') if expand { writeByte(pp, '\t') } showTypeInStruct = f.Type.Kind() == reflect.Interface } pp.printValue(getField(v, i), showTypeInStruct, true) if expand { io.WriteString(pp, ",\n") } else if i < v.NumField()-1 { io.WriteString(pp, ", ") } } if expand { pp.tw.Flush() } } writeByte(p, '}') case reflect.Interface: switch e := v.Elem(); { case e.Kind() == reflect.Invalid: io.WriteString(p, "nil") case e.IsValid(): p.printValue(e, showType, true) default: io.WriteString(p, v.Type().String()) io.WriteString(p, "(nil)") } case reflect.Array, reflect.Slice: t := v.Type() if showType { io.WriteString(p, t.String()) } if v.Kind() == reflect.Slice && v.IsNil() && showType { io.WriteString(p, "(nil)") break } if v.Kind() == reflect.Slice && v.IsNil() { io.WriteString(p, "nil") break } writeByte(p, '{') expand := !canInline(v.Type()) pp := p if expand { writeByte(p, '\n') pp = p.indent() } for i := 0; i < v.Len(); i++ { showTypeInSlice := t.Elem().Kind() == reflect.Interface pp.printValue(v.Index(i), showTypeInSlice, true) if expand { io.WriteString(pp, ",\n") } else if i < v.Len()-1 { io.WriteString(pp, ", ") } } if expand { pp.tw.Flush() } writeByte(p, '}') case reflect.Ptr: e := v.Elem() if !e.IsValid() { writeByte(p, '(') io.WriteString(p, v.Type().String()) io.WriteString(p, ")(nil)") } else { writeByte(p, '&') p.printValue(e, true, true) } case reflect.Chan: x := v.Pointer() if showType { writeByte(p, '(') io.WriteString(p, v.Type().String()) fmt.Fprintf(p, ")(%#v)", x) } else { fmt.Fprintf(p, "%#v", x) } case reflect.Func: io.WriteString(p, v.Type().String()) io.WriteString(p, " {...}") case reflect.UnsafePointer: p.printInline(v, v.Pointer(), showType) case reflect.Invalid: io.WriteString(p, "nil") } } func canInline(t reflect.Type) bool { switch t.Kind() { case reflect.Map: return !canExpand(t.Elem()) case reflect.Struct: for i := 0; i < t.NumField(); i++ { if canExpand(t.Field(i).Type) { return false } } return true case reflect.Interface: return false case reflect.Array, reflect.Slice: return !canExpand(t.Elem()) case reflect.Ptr: return false case reflect.Chan, reflect.Func, reflect.UnsafePointer: return false } return true } func canExpand(t reflect.Type) bool { switch t.Kind() { case reflect.Map, reflect.Struct, reflect.Interface, reflect.Array, reflect.Slice, reflect.Ptr: return true } return false } func (p *printer) fmtString(s string, quote bool) { if quote { s = strconv.Quote(s) } io.WriteString(p, s) } func tryDeepEqual(a, b interface{}) bool { defer func() { recover() }() return reflect.DeepEqual(a, b) } func writeByte(w io.Writer, b byte) { w.Write([]byte{b}) } func getField(v reflect.Value, i int) reflect.Value { val := v.Field(i) if val.Kind() == reflect.Interface && !val.IsNil() { val = val.Elem() } return val } golang-pretty-0.0~git20130613/formatter_test.go000066400000000000000000000055141214312767600212760ustar00rootroot00000000000000package pretty import ( "fmt" "io" "testing" "unsafe" ) type test struct { v interface{} s string } type LongStructTypeName struct { longFieldName interface{} otherLongFieldName interface{} } type SA struct { t *T } type T struct { x, y int } type F int func (f F) Format(s fmt.State, c rune) { fmt.Fprintf(s, "F(%d)", int(f)) } var long = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" var gosyntax = []test{ {nil, `nil`}, {"", `""`}, {"a", `"a"`}, {1, "int(1)"}, {1.0, "float64(1)"}, {[]int(nil), "[]int(nil)"}, {[0]int{}, "[0]int{}"}, {complex(1, 0), "(1+0i)"}, //{make(chan int), "(chan int)(0x1234)"}, {unsafe.Pointer(uintptr(1)), "unsafe.Pointer(0x1)"}, {func(int) {}, "func(int) {...}"}, {map[int]int{1: 1}, "map[int]int{1:1}"}, {int32(1), "int32(1)"}, {io.EOF, `&errors.errorString{s:"EOF"}`}, {[]string{"a"}, `[]string{"a"}`}, { []string{long}, `[]string{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}`, }, {F(5), "pretty.F(5)"}, { SA{&T{1, 2}}, `pretty.SA{ t: &pretty.T{x:1, y:2}, }`, }, { map[int][]byte{1: []byte{}}, `map[int][]uint8{ 1: {}, }`, }, { map[int]T{1: T{}}, `map[int]pretty.T{ 1: {}, }`, }, { long, `"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"`, }, { LongStructTypeName{ longFieldName: LongStructTypeName{}, otherLongFieldName: long, }, `pretty.LongStructTypeName{ longFieldName: pretty.LongStructTypeName{}, otherLongFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", }`, }, { &LongStructTypeName{ longFieldName: &LongStructTypeName{}, otherLongFieldName: (*LongStructTypeName)(nil), }, `&pretty.LongStructTypeName{ longFieldName: &pretty.LongStructTypeName{}, otherLongFieldName: (*pretty.LongStructTypeName)(nil), }`, }, { []LongStructTypeName{ {nil, nil}, {3, 3}, {long, nil}, }, `[]pretty.LongStructTypeName{ {}, { longFieldName: int(3), otherLongFieldName: int(3), }, { longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", otherLongFieldName: nil, }, }`, }, { []interface{}{ LongStructTypeName{nil, nil}, []byte{1, 2, 3}, T{3, 4}, LongStructTypeName{long, nil}, }, `[]interface {}{ pretty.LongStructTypeName{}, []uint8{0x1, 0x2, 0x3}, pretty.T{x:3, y:4}, pretty.LongStructTypeName{ longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", otherLongFieldName: nil, }, }`, }, } func TestGoSyntax(t *testing.T) { for _, tt := range gosyntax { s := fmt.Sprintf("%# v", Formatter(tt.v)) if tt.s != s { t.Errorf("expected %q", tt.s) t.Errorf("got %q", s) t.Errorf("expraw\n%s", tt.s) t.Errorf("gotraw\n%s", s) } } } golang-pretty-0.0~git20130613/pretty.go000066400000000000000000000055251214312767600175650ustar00rootroot00000000000000// Package pretty provides pretty-printing for Go values. This is // useful during debugging, to avoid wrapping long output lines in // the terminal. // // It provides a function, Formatter, that can be used with any // function that accepts a format string. It also provides // convenience wrappers for functions in packages fmt and log. package pretty import ( "fmt" "io" "log" ) // Errorf is a convenience wrapper for fmt.Errorf. // // Calling Errorf(f, x, y) is equivalent to // fmt.Errorf(f, Formatter(x), Formatter(y)). func Errorf(format string, a ...interface{}) error { return fmt.Errorf(format, wrap(a, false)...) } // Fprintf is a convenience wrapper for fmt.Fprintf. // // Calling Fprintf(w, f, x, y) is equivalent to // fmt.Fprintf(w, f, Formatter(x), Formatter(y)). func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error error) { return fmt.Fprintf(w, format, wrap(a, false)...) } // Log is a convenience wrapper for log.Printf. // // Calling Log(x, y) is equivalent to // log.Print(Formatter(x), Formatter(y)), but each operand is // formatted with "%# v". func Log(a ...interface{}) { log.Print(wrap(a, true)...) } // Logf is a convenience wrapper for log.Printf. // // Calling Logf(f, x, y) is equivalent to // log.Printf(f, Formatter(x), Formatter(y)). func Logf(format string, a ...interface{}) { log.Printf(format, wrap(a, false)...) } // Logln is a convenience wrapper for log.Printf. // // Calling Logln(x, y) is equivalent to // log.Println(Formatter(x), Formatter(y)), but each operand is // formatted with "%# v". func Logln(a ...interface{}) { log.Println(wrap(a, true)...) } // Print pretty-prints its operands and writes to standard output. // // Calling Print(x, y) is equivalent to // fmt.Print(Formatter(x), Formatter(y)), but each operand is // formatted with "%# v". func Print(a ...interface{}) (n int, errno error) { return fmt.Print(wrap(a, true)...) } // Printf is a convenience wrapper for fmt.Printf. // // Calling Printf(f, x, y) is equivalent to // fmt.Printf(f, Formatter(x), Formatter(y)). func Printf(format string, a ...interface{}) (n int, errno error) { return fmt.Printf(format, wrap(a, false)...) } // Println pretty-prints its operands and writes to standard output. // // Calling Print(x, y) is equivalent to // fmt.Println(Formatter(x), Formatter(y)), but each operand is // formatted with "%# v". func Println(a ...interface{}) (n int, errno error) { return fmt.Println(wrap(a, true)...) } // Sprintf is a convenience wrapper for fmt.Sprintf. // // Calling Sprintf(f, x, y) is equivalent to // fmt.Sprintf(f, Formatter(x), Formatter(y)). func Sprintf(format string, a ...interface{}) string { return fmt.Sprintf(format, wrap(a, false)...) } func wrap(a []interface{}, force bool) []interface{} { w := make([]interface{}, len(a)) for i, x := range a { w[i] = formatter{x: x, force: force} } return w } golang-pretty-0.0~git20130613/zero.go000066400000000000000000000017401214312767600172100ustar00rootroot00000000000000package pretty import ( "reflect" ) func nonzero(v reflect.Value) bool { switch v.Kind() { case reflect.Bool: return v.Bool() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return v.Int() != 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return v.Uint() != 0 case reflect.Float32, reflect.Float64: return v.Float() != 0 case reflect.Complex64, reflect.Complex128: return v.Complex() != complex(0, 0) case reflect.String: return v.String() != "" case reflect.Struct: for i := 0; i < v.NumField(); i++ { if nonzero(getField(v, i)) { return true } } return false case reflect.Array: for i := 0; i < v.Len(); i++ { if nonzero(v.Index(i)) { return true } } return false case reflect.Map, reflect.Interface, reflect.Slice, reflect.Ptr, reflect.Chan, reflect.Func: return !v.IsNil() case reflect.UnsafePointer: return v.Pointer() != 0 } return true }