Update dependencies, enable pruning for vendor/

So, `dep` got an nice new feature to remove tests and non-go files from
`vendor/`, and this brings the size of the vendor directory from ~300MiB
down to ~20MiB. We don that now.
This commit is contained in:
Alexander Neumann 2018-08-01 19:43:44 +02:00
parent 3422c1ca83
commit bff635bc5f
6741 changed files with 26942 additions and 4902033 deletions

View file

@ -8,15 +8,11 @@ package value
import (
"fmt"
"reflect"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// formatFakePointers controls whether to substitute pointer addresses with nil.
// This is used for deterministic testing.
var formatFakePointers = false
var stringerIface = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
// Format formats the value v as a string.
@ -26,28 +22,35 @@ var stringerIface = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
// * Avoids printing struct fields that are zero
// * Prints a nil-slice as being nil, not empty
// * Prints map entries in deterministic order
func Format(v reflect.Value, useStringer bool) string {
return formatAny(v, formatConfig{useStringer, true, true, !formatFakePointers}, nil)
func Format(v reflect.Value, conf FormatConfig) string {
conf.printType = true
conf.followPointers = true
conf.realPointers = true
return formatAny(v, conf, nil)
}
type formatConfig struct {
useStringer bool // Should the String method be used if available?
printType bool // Should we print the type before the value?
followPointers bool // Should we recursively follow pointers?
realPointers bool // Should we print the real address of pointers?
type FormatConfig struct {
UseStringer bool // Should the String method be used if available?
printType bool // Should we print the type before the value?
PrintPrimitiveType bool // Should we print the type of primitives?
followPointers bool // Should we recursively follow pointers?
realPointers bool // Should we print the real address of pointers?
}
func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) string {
func formatAny(v reflect.Value, conf FormatConfig, visited map[uintptr]bool) string {
// TODO: Should this be a multi-line printout in certain situations?
if !v.IsValid() {
return "<non-existent>"
}
if conf.useStringer && v.Type().Implements(stringerIface) && v.CanInterface() {
if conf.UseStringer && v.Type().Implements(stringerIface) && v.CanInterface() {
if (v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface) && v.IsNil() {
return "<nil>"
}
return fmt.Sprintf("%q", v.Interface().(fmt.Stringer).String())
const stringerPrefix = "s" // Indicates that the String method was used
s := v.Interface().(fmt.Stringer).String()
return stringerPrefix + formatString(s)
}
switch v.Kind() {
@ -66,7 +69,7 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
case reflect.Complex64, reflect.Complex128:
return formatPrimitive(v.Type(), v.Complex(), conf)
case reflect.String:
return formatPrimitive(v.Type(), fmt.Sprintf("%q", v), conf)
return formatPrimitive(v.Type(), formatString(v.String()), conf)
case reflect.UnsafePointer, reflect.Chan, reflect.Func:
return formatPointer(v, conf)
case reflect.Ptr:
@ -127,11 +130,13 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
visited = insertPointer(visited, v.Pointer())
var ss []string
subConf := conf
subConf.printType = v.Type().Elem().Kind() == reflect.Interface
keyConf, valConf := conf, conf
keyConf.printType = v.Type().Key().Kind() == reflect.Interface
keyConf.followPointers = false
valConf.printType = v.Type().Elem().Kind() == reflect.Interface
for _, k := range SortKeys(v.MapKeys()) {
sk := formatAny(k, formatConfig{realPointers: conf.realPointers}, visited)
sv := formatAny(v.MapIndex(k), subConf, visited)
sk := formatAny(k, keyConf, visited)
sv := formatAny(v.MapIndex(k), valConf, visited)
ss = append(ss, fmt.Sprintf("%s: %s", sk, sv))
}
s := fmt.Sprintf("{%s}", strings.Join(ss, ", "))
@ -149,7 +154,7 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
continue // Elide zero value fields
}
name := v.Type().Field(i).Name
subConf.useStringer = conf.useStringer && isExported(name)
subConf.UseStringer = conf.UseStringer
s := formatAny(vv, subConf, visited)
ss = append(ss, fmt.Sprintf("%s: %s", name, s))
}
@ -163,14 +168,33 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
}
}
func formatPrimitive(t reflect.Type, v interface{}, conf formatConfig) string {
if conf.printType && t.PkgPath() != "" {
func formatString(s string) string {
// Use quoted string if it the same length as a raw string literal.
// Otherwise, attempt to use the raw string form.
qs := strconv.Quote(s)
if len(qs) == 1+len(s)+1 {
return qs
}
// Disallow newlines to ensure output is a single line.
// Only allow printable runes for readability purposes.
rawInvalid := func(r rune) bool {
return r == '`' || r == '\n' || !unicode.IsPrint(r)
}
if strings.IndexFunc(s, rawInvalid) < 0 {
return "`" + s + "`"
}
return qs
}
func formatPrimitive(t reflect.Type, v interface{}, conf FormatConfig) string {
if conf.printType && (conf.PrintPrimitiveType || t.PkgPath() != "") {
return fmt.Sprintf("%v(%v)", t, v)
}
return fmt.Sprintf("%v", v)
}
func formatPointer(v reflect.Value, conf formatConfig) string {
func formatPointer(v reflect.Value, conf FormatConfig) string {
p := v.Pointer()
if !conf.realPointers {
p = 0 // For deterministic printing purposes
@ -251,9 +275,3 @@ func isZero(v reflect.Value) bool {
}
return false
}
// isExported reports whether the identifier is exported.
func isExported(id string) bool {
r, _ := utf8.DecodeRuneInString(id)
return unicode.IsUpper(r)
}

View file

@ -1,91 +0,0 @@
// Copyright 2017, The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.
package value
import (
"bytes"
"io"
"reflect"
"testing"
)
func TestFormat(t *testing.T) {
type key struct {
a int
b string
c chan bool
}
tests := []struct {
in interface{}
want string
}{{
in: []int{},
want: "[]int{}",
}, {
in: []int(nil),
want: "[]int(nil)",
}, {
in: []int{1, 2, 3, 4, 5},
want: "[]int{1, 2, 3, 4, 5}",
}, {
in: []interface{}{1, true, "hello", struct{ A, B int }{1, 2}},
want: "[]interface {}{1, true, \"hello\", struct { A int; B int }{A: 1, B: 2}}",
}, {
in: []struct{ A, B int }{{1, 2}, {0, 4}, {}},
want: "[]struct { A int; B int }{{A: 1, B: 2}, {B: 4}, {}}",
}, {
in: map[*int]string{new(int): "hello"},
want: "map[*int]string{0x00: \"hello\"}",
}, {
in: map[key]string{{}: "hello"},
want: "map[value.key]string{{}: \"hello\"}",
}, {
in: map[key]string{{a: 5, b: "key", c: make(chan bool)}: "hello"},
want: "map[value.key]string{{a: 5, b: \"key\", c: (chan bool)(0x00)}: \"hello\"}",
}, {
in: map[io.Reader]string{new(bytes.Reader): "hello"},
want: "map[io.Reader]string{0x00: \"hello\"}",
}, {
in: func() interface{} {
var a = []interface{}{nil}
a[0] = a
return a
}(),
want: "[]interface {}{([]interface {})(0x00)}",
}, {
in: func() interface{} {
type A *A
var a A
a = &a
return a
}(),
want: "&(value.A)(0x00)",
}, {
in: func() interface{} {
type A map[*A]A
a := make(A)
a[&a] = a
return a
}(),
want: "value.A{0x00: 0x00}",
}, {
in: func() interface{} {
var a [2]interface{}
a[0] = &a
return a
}(),
want: "[2]interface {}{&[2]interface {}{(*[2]interface {})(0x00), interface {}(nil)}, interface {}(nil)}",
}}
formatFakePointers = true
defer func() { formatFakePointers = false }()
for i, tt := range tests {
got := Format(reflect.ValueOf(tt.in), true)
if got != tt.want {
t.Errorf("test %d, Format():\ngot %q\nwant %q", i, got, tt.want)
}
}
}

View file

@ -24,7 +24,7 @@ func SortKeys(vs []reflect.Value) []reflect.Value {
// Deduplicate keys (fails for NaNs).
vs2 := vs[:1]
for _, v := range vs[1:] {
if v.Interface() != vs2[len(vs2)-1].Interface() {
if isLess(vs2[len(vs2)-1], v) {
vs2 = append(vs2, v)
}
}

View file

@ -1,152 +0,0 @@
// Copyright 2017, The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.
package value_test
import (
"math"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/internal/value"
)
func TestSortKeys(t *testing.T) {
type (
MyString string
MyArray [2]int
MyStruct struct {
A MyString
B MyArray
C chan float64
}
EmptyStruct struct{}
)
opts := []cmp.Option{
cmp.Comparer(func(x, y float64) bool {
if math.IsNaN(x) && math.IsNaN(y) {
return true
}
return x == y
}),
cmp.Comparer(func(x, y complex128) bool {
rx, ix, ry, iy := real(x), imag(x), real(y), imag(y)
if math.IsNaN(rx) && math.IsNaN(ry) {
rx, ry = 0, 0
}
if math.IsNaN(ix) && math.IsNaN(iy) {
ix, iy = 0, 0
}
return rx == ry && ix == iy
}),
cmp.Comparer(func(x, y chan bool) bool { return true }),
cmp.Comparer(func(x, y chan int) bool { return true }),
cmp.Comparer(func(x, y chan float64) bool { return true }),
cmp.Comparer(func(x, y chan interface{}) bool { return true }),
cmp.Comparer(func(x, y *int) bool { return true }),
}
tests := []struct {
in map[interface{}]bool // Set of keys to sort
want []interface{}
}{{
in: map[interface{}]bool{1: true, 2: true, 3: true},
want: []interface{}{1, 2, 3},
}, {
in: map[interface{}]bool{
nil: true,
true: true,
false: true,
-5: true,
-55: true,
-555: true,
uint(1): true,
uint(11): true,
uint(111): true,
"abc": true,
"abcd": true,
"abcde": true,
"foo": true,
"bar": true,
MyString("abc"): true,
MyString("abcd"): true,
MyString("abcde"): true,
new(int): true,
new(int): true,
make(chan bool): true,
make(chan bool): true,
make(chan int): true,
make(chan interface{}): true,
math.Inf(+1): true,
math.Inf(-1): true,
1.2345: true,
12.345: true,
123.45: true,
1234.5: true,
0 + 0i: true,
1 + 0i: true,
2 + 0i: true,
0 + 1i: true,
0 + 2i: true,
0 + 3i: true,
[2]int{2, 3}: true,
[2]int{4, 0}: true,
[2]int{2, 4}: true,
MyArray([2]int{2, 4}): true,
EmptyStruct{}: true,
MyStruct{
"bravo", [2]int{2, 3}, make(chan float64),
}: true,
MyStruct{
"alpha", [2]int{3, 3}, make(chan float64),
}: true,
},
want: []interface{}{
nil, false, true,
-555, -55, -5, uint(1), uint(11), uint(111),
math.Inf(-1), 1.2345, 12.345, 123.45, 1234.5, math.Inf(+1),
(0 + 0i), (0 + 1i), (0 + 2i), (0 + 3i), (1 + 0i), (2 + 0i),
[2]int{2, 3}, [2]int{2, 4}, [2]int{4, 0}, MyArray([2]int{2, 4}),
make(chan bool), make(chan bool), make(chan int), make(chan interface{}),
new(int), new(int),
"abc", "abcd", "abcde", "bar", "foo",
MyString("abc"), MyString("abcd"), MyString("abcde"),
EmptyStruct{},
MyStruct{"alpha", [2]int{3, 3}, make(chan float64)},
MyStruct{"bravo", [2]int{2, 3}, make(chan float64)},
},
}, {
// NaN values cannot be properly deduplicated.
// This is okay since map entries with NaN in the keys cannot be
// retrieved anyways.
in: map[interface{}]bool{
math.NaN(): true,
math.NaN(): true,
complex(0, math.NaN()): true,
complex(0, math.NaN()): true,
complex(math.NaN(), 0): true,
complex(math.NaN(), 0): true,
complex(math.NaN(), math.NaN()): true,
},
want: []interface{}{
math.NaN(), math.NaN(), math.NaN(), math.NaN(),
complex(math.NaN(), math.NaN()), complex(math.NaN(), math.NaN()),
complex(math.NaN(), 0), complex(math.NaN(), 0), complex(math.NaN(), 0), complex(math.NaN(), 0),
complex(0, math.NaN()), complex(0, math.NaN()), complex(0, math.NaN()), complex(0, math.NaN()),
},
}}
for i, tt := range tests {
keys := append(reflect.ValueOf(tt.in).MapKeys(), reflect.ValueOf(tt.in).MapKeys()...)
var got []interface{}
for _, k := range value.SortKeys(keys) {
got = append(got, k.Interface())
}
if d := cmp.Diff(got, tt.want, opts...); d != "" {
t.Errorf("test %d, Sort() mismatch (-got +want):\n%s", i, d)
}
}
}