init
This commit is contained in:
5
vendor/github.com/kylelemons/godebug/pretty/.gitignore
generated
vendored
Normal file
5
vendor/github.com/kylelemons/godebug/pretty/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
*.test
|
||||
*.bench
|
||||
*.golden
|
||||
*.txt
|
||||
*.prof
|
25
vendor/github.com/kylelemons/godebug/pretty/doc.go
generated
vendored
Normal file
25
vendor/github.com/kylelemons/godebug/pretty/doc.go
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2013 Google Inc. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package pretty pretty-prints Go structures.
|
||||
//
|
||||
// This package uses reflection to examine a Go value and can
|
||||
// print out in a nice, aligned fashion. It supports three
|
||||
// modes (normal, compact, and extended) for advanced use.
|
||||
//
|
||||
// See the Reflect and Print examples for what the output looks like.
|
||||
package pretty
|
||||
|
||||
// TODO:
|
||||
// - Catch cycles
|
153
vendor/github.com/kylelemons/godebug/pretty/public.go
generated
vendored
Normal file
153
vendor/github.com/kylelemons/godebug/pretty/public.go
generated
vendored
Normal file
@@ -0,0 +1,153 @@
|
||||
// Copyright 2013 Google Inc. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pretty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/kylelemons/godebug/diff"
|
||||
)
|
||||
|
||||
// A Config represents optional configuration parameters for formatting.
|
||||
//
|
||||
// Some options, notably ShortList, dramatically increase the overhead
|
||||
// of pretty-printing a value.
|
||||
type Config struct {
|
||||
// Verbosity options
|
||||
Compact bool // One-line output. Overrides Diffable.
|
||||
Diffable bool // Adds extra newlines for more easily diffable output.
|
||||
|
||||
// Field and value options
|
||||
IncludeUnexported bool // Include unexported fields in output
|
||||
PrintStringers bool // Call String on a fmt.Stringer
|
||||
PrintTextMarshalers bool // Call MarshalText on an encoding.TextMarshaler
|
||||
SkipZeroFields bool // Skip struct fields that have a zero value.
|
||||
|
||||
// Output transforms
|
||||
ShortList int // Maximum character length for short lists if nonzero.
|
||||
|
||||
// Type-specific overrides
|
||||
//
|
||||
// Formatter maps a type to a function that will provide a one-line string
|
||||
// representation of the input value. Conceptually:
|
||||
// Formatter[reflect.TypeOf(v)](v) = "v as a string"
|
||||
//
|
||||
// Note that the first argument need not explicitly match the type, it must
|
||||
// merely be callable with it.
|
||||
//
|
||||
// When processing an input value, if its type exists as a key in Formatter:
|
||||
// 1) If the value is nil, no stringification is performed.
|
||||
// This allows overriding of PrintStringers and PrintTextMarshalers.
|
||||
// 2) The value will be called with the input as its only argument.
|
||||
// The function must return a string as its first return value.
|
||||
//
|
||||
// In addition to func literals, two common values for this will be:
|
||||
// fmt.Sprint (function) func Sprint(...interface{}) string
|
||||
// Type.String (method) func (Type) String() string
|
||||
//
|
||||
// Note that neither of these work if the String method is a pointer
|
||||
// method and the input will be provided as a value. In that case,
|
||||
// use a function that calls .String on the formal value parameter.
|
||||
Formatter map[reflect.Type]interface{}
|
||||
}
|
||||
|
||||
// Default Config objects
|
||||
var (
|
||||
// DefaultFormatter is the default set of overrides for stringification.
|
||||
DefaultFormatter = map[reflect.Type]interface{}{
|
||||
reflect.TypeOf(time.Time{}): fmt.Sprint,
|
||||
reflect.TypeOf(net.IP{}): fmt.Sprint,
|
||||
reflect.TypeOf((*error)(nil)).Elem(): fmt.Sprint,
|
||||
}
|
||||
|
||||
// CompareConfig is the default configuration used for Compare.
|
||||
CompareConfig = &Config{
|
||||
Diffable: true,
|
||||
IncludeUnexported: true,
|
||||
Formatter: DefaultFormatter,
|
||||
}
|
||||
|
||||
// DefaultConfig is the default configuration used for all other top-level functions.
|
||||
DefaultConfig = &Config{
|
||||
Formatter: DefaultFormatter,
|
||||
}
|
||||
)
|
||||
|
||||
func (cfg *Config) fprint(buf *bytes.Buffer, vals ...interface{}) {
|
||||
for i, val := range vals {
|
||||
if i > 0 {
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
cfg.val2node(reflect.ValueOf(val)).WriteTo(buf, "", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
// Print writes the DefaultConfig representation of the given values to standard output.
|
||||
func Print(vals ...interface{}) {
|
||||
DefaultConfig.Print(vals...)
|
||||
}
|
||||
|
||||
// Print writes the configured presentation of the given values to standard output.
|
||||
func (cfg *Config) Print(vals ...interface{}) {
|
||||
fmt.Println(cfg.Sprint(vals...))
|
||||
}
|
||||
|
||||
// Sprint returns a string representation of the given value according to the DefaultConfig.
|
||||
func Sprint(vals ...interface{}) string {
|
||||
return DefaultConfig.Sprint(vals...)
|
||||
}
|
||||
|
||||
// Sprint returns a string representation of the given value according to cfg.
|
||||
func (cfg *Config) Sprint(vals ...interface{}) string {
|
||||
buf := new(bytes.Buffer)
|
||||
cfg.fprint(buf, vals...)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Fprint writes the representation of the given value to the writer according to the DefaultConfig.
|
||||
func Fprint(w io.Writer, vals ...interface{}) (n int64, err error) {
|
||||
return DefaultConfig.Fprint(w, vals...)
|
||||
}
|
||||
|
||||
// Fprint writes the representation of the given value to the writer according to the cfg.
|
||||
func (cfg *Config) Fprint(w io.Writer, vals ...interface{}) (n int64, err error) {
|
||||
buf := new(bytes.Buffer)
|
||||
cfg.fprint(buf, vals...)
|
||||
return buf.WriteTo(w)
|
||||
}
|
||||
|
||||
// Compare returns a string containing a line-by-line unified diff of the
|
||||
// values in a and b, using the CompareConfig.
|
||||
//
|
||||
// Each line in the output is prefixed with '+', '-', or ' ' to indicate which
|
||||
// side it's from. Lines from the a side are marked with '-', lines from the
|
||||
// b side are marked with '+' and lines that are the same on both sides are
|
||||
// marked with ' '.
|
||||
func Compare(a, b interface{}) string {
|
||||
return CompareConfig.Compare(a, b)
|
||||
}
|
||||
|
||||
// Compare returns a string containing a line-by-line unified diff of the
|
||||
// values in got and want according to the cfg.
|
||||
func (cfg *Config) Compare(a, b interface{}) string {
|
||||
diffCfg := *cfg
|
||||
diffCfg.Diffable = true
|
||||
return diff.Diff(cfg.Sprint(a), cfg.Sprint(b))
|
||||
}
|
121
vendor/github.com/kylelemons/godebug/pretty/reflect.go
generated
vendored
Normal file
121
vendor/github.com/kylelemons/godebug/pretty/reflect.go
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
// Copyright 2013 Google Inc. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pretty
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func isZeroVal(val reflect.Value) bool {
|
||||
if !val.CanInterface() {
|
||||
return false
|
||||
}
|
||||
z := reflect.Zero(val.Type()).Interface()
|
||||
return reflect.DeepEqual(val.Interface(), z)
|
||||
}
|
||||
|
||||
func (c *Config) val2node(val reflect.Value) node {
|
||||
// TODO(kevlar): pointer tracking?
|
||||
|
||||
if !val.IsValid() {
|
||||
return rawVal("nil")
|
||||
}
|
||||
|
||||
if val.CanInterface() {
|
||||
v := val.Interface()
|
||||
if formatter, ok := c.Formatter[val.Type()]; ok {
|
||||
if formatter != nil {
|
||||
res := reflect.ValueOf(formatter).Call([]reflect.Value{val})
|
||||
return rawVal(res[0].Interface().(string))
|
||||
}
|
||||
} else {
|
||||
if s, ok := v.(fmt.Stringer); ok && c.PrintStringers {
|
||||
return stringVal(s.String())
|
||||
}
|
||||
if t, ok := v.(encoding.TextMarshaler); ok && c.PrintTextMarshalers {
|
||||
if raw, err := t.MarshalText(); err == nil { // if NOT an error
|
||||
return stringVal(string(raw))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch kind := val.Kind(); kind {
|
||||
case reflect.Ptr, reflect.Interface:
|
||||
if val.IsNil() {
|
||||
return rawVal("nil")
|
||||
}
|
||||
return c.val2node(val.Elem())
|
||||
case reflect.String:
|
||||
return stringVal(val.String())
|
||||
case reflect.Slice, reflect.Array:
|
||||
n := list{}
|
||||
length := val.Len()
|
||||
for i := 0; i < length; i++ {
|
||||
n = append(n, c.val2node(val.Index(i)))
|
||||
}
|
||||
return n
|
||||
case reflect.Map:
|
||||
n := keyvals{}
|
||||
keys := val.MapKeys()
|
||||
for _, key := range keys {
|
||||
// TODO(kevlar): Support arbitrary type keys?
|
||||
n = append(n, keyval{compactString(c.val2node(key)), c.val2node(val.MapIndex(key))})
|
||||
}
|
||||
sort.Sort(n)
|
||||
return n
|
||||
case reflect.Struct:
|
||||
n := keyvals{}
|
||||
typ := val.Type()
|
||||
fields := typ.NumField()
|
||||
for i := 0; i < fields; i++ {
|
||||
sf := typ.Field(i)
|
||||
if !c.IncludeUnexported && sf.PkgPath != "" {
|
||||
continue
|
||||
}
|
||||
field := val.Field(i)
|
||||
if c.SkipZeroFields && isZeroVal(field) {
|
||||
continue
|
||||
}
|
||||
n = append(n, keyval{sf.Name, c.val2node(field)})
|
||||
}
|
||||
return n
|
||||
case reflect.Bool:
|
||||
if val.Bool() {
|
||||
return rawVal("true")
|
||||
}
|
||||
return rawVal("false")
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return rawVal(fmt.Sprintf("%d", val.Int()))
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return rawVal(fmt.Sprintf("%d", val.Uint()))
|
||||
case reflect.Uintptr:
|
||||
return rawVal(fmt.Sprintf("0x%X", val.Uint()))
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return rawVal(fmt.Sprintf("%v", val.Float()))
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
return rawVal(fmt.Sprintf("%v", val.Complex()))
|
||||
}
|
||||
|
||||
// Fall back to the default %#v if we can
|
||||
if val.CanInterface() {
|
||||
return rawVal(fmt.Sprintf("%#v", val.Interface()))
|
||||
}
|
||||
|
||||
return rawVal(val.String())
|
||||
}
|
160
vendor/github.com/kylelemons/godebug/pretty/structure.go
generated
vendored
Normal file
160
vendor/github.com/kylelemons/godebug/pretty/structure.go
generated
vendored
Normal file
@@ -0,0 +1,160 @@
|
||||
// Copyright 2013 Google Inc. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pretty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type node interface {
|
||||
WriteTo(w *bytes.Buffer, indent string, cfg *Config)
|
||||
}
|
||||
|
||||
func compactString(n node) string {
|
||||
switch k := n.(type) {
|
||||
case stringVal:
|
||||
return string(k)
|
||||
case rawVal:
|
||||
return string(k)
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
n.WriteTo(buf, "", &Config{Compact: true})
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
type stringVal string
|
||||
|
||||
func (str stringVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
|
||||
w.WriteString(strconv.Quote(string(str)))
|
||||
}
|
||||
|
||||
type rawVal string
|
||||
|
||||
func (r rawVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
|
||||
w.WriteString(string(r))
|
||||
}
|
||||
|
||||
type keyval struct {
|
||||
key string
|
||||
val node
|
||||
}
|
||||
|
||||
type keyvals []keyval
|
||||
|
||||
func (l keyvals) Len() int { return len(l) }
|
||||
func (l keyvals) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||
func (l keyvals) Less(i, j int) bool { return l[i].key < l[j].key }
|
||||
|
||||
func (l keyvals) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
|
||||
w.WriteByte('{')
|
||||
|
||||
switch {
|
||||
case cfg.Compact:
|
||||
// All on one line:
|
||||
for i, kv := range l {
|
||||
if i > 0 {
|
||||
w.WriteByte(',')
|
||||
}
|
||||
w.WriteString(kv.key)
|
||||
w.WriteByte(':')
|
||||
kv.val.WriteTo(w, indent, cfg)
|
||||
}
|
||||
case cfg.Diffable:
|
||||
w.WriteByte('\n')
|
||||
inner := indent + " "
|
||||
// Each value gets its own line:
|
||||
for _, kv := range l {
|
||||
w.WriteString(inner)
|
||||
w.WriteString(kv.key)
|
||||
w.WriteString(": ")
|
||||
kv.val.WriteTo(w, inner, cfg)
|
||||
w.WriteString(",\n")
|
||||
}
|
||||
w.WriteString(indent)
|
||||
default:
|
||||
keyWidth := 0
|
||||
for _, kv := range l {
|
||||
if kw := len(kv.key); kw > keyWidth {
|
||||
keyWidth = kw
|
||||
}
|
||||
}
|
||||
alignKey := indent + " "
|
||||
alignValue := strings.Repeat(" ", keyWidth)
|
||||
inner := alignKey + alignValue + " "
|
||||
// First and last line shared with bracket:
|
||||
for i, kv := range l {
|
||||
if i > 0 {
|
||||
w.WriteString(",\n")
|
||||
w.WriteString(alignKey)
|
||||
}
|
||||
w.WriteString(kv.key)
|
||||
w.WriteString(": ")
|
||||
w.WriteString(alignValue[len(kv.key):])
|
||||
kv.val.WriteTo(w, inner, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteByte('}')
|
||||
}
|
||||
|
||||
type list []node
|
||||
|
||||
func (l list) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
|
||||
if max := cfg.ShortList; max > 0 {
|
||||
short := compactString(l)
|
||||
if len(short) <= max {
|
||||
w.WriteString(short)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteByte('[')
|
||||
|
||||
switch {
|
||||
case cfg.Compact:
|
||||
// All on one line:
|
||||
for i, v := range l {
|
||||
if i > 0 {
|
||||
w.WriteByte(',')
|
||||
}
|
||||
v.WriteTo(w, indent, cfg)
|
||||
}
|
||||
case cfg.Diffable:
|
||||
w.WriteByte('\n')
|
||||
inner := indent + " "
|
||||
// Each value gets its own line:
|
||||
for _, v := range l {
|
||||
w.WriteString(inner)
|
||||
v.WriteTo(w, inner, cfg)
|
||||
w.WriteString(",\n")
|
||||
}
|
||||
w.WriteString(indent)
|
||||
default:
|
||||
inner := indent + " "
|
||||
// First and last line shared with bracket:
|
||||
for i, v := range l {
|
||||
if i > 0 {
|
||||
w.WriteString(",\n")
|
||||
w.WriteString(inner)
|
||||
}
|
||||
v.WriteTo(w, inner, cfg)
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteByte(']')
|
||||
}
|
Reference in New Issue
Block a user