├── .gitignore ├── LICENSE ├── README.md ├── bench_test.go ├── go.mod ├── go.sum ├── paralyze.go ├── paralyze_test.go └── vendor ├── github.com ├── davecgh │ └── go-spew │ │ ├── LICENSE │ │ └── spew │ │ ├── bypass.go │ │ ├── bypasssafe.go │ │ ├── common.go │ │ ├── config.go │ │ ├── doc.go │ │ ├── dump.go │ │ ├── format.go │ │ └── spew.go ├── pmezard │ └── go-difflib │ │ ├── LICENSE │ │ └── difflib │ │ └── difflib.go └── stretchr │ └── testify │ ├── LICENSE │ └── assert │ ├── assertion_format.go │ ├── assertion_format.go.tmpl │ ├── assertion_forward.go │ ├── assertion_forward.go.tmpl │ ├── assertions.go │ ├── doc.go │ ├── errors.go │ ├── forward_assertions.go │ └── http_assertions.go └── modules.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ian Lozinski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | paralyze 2 | ======== 3 | [![GoDoc](http://godoc.org/github.com/i/paralyze?status.png)](http://godoc.org/github.com/i/paralyze) 4 | 5 | parallelize things 6 | 7 | how to get 8 | ------------ 9 | 10 | go get github.com/i/paralyze 11 | 12 | vanilla parallelization 13 | --------- 14 | 15 | ```go 16 | package main 17 | 18 | import ( 19 | "fmt" 20 | "time" 21 | 22 | "github.com/i/paralyze" 23 | ) 24 | 25 | func main() { 26 | fn1 := func() (interface{}, error) { 27 | time.Sleep(time.Second) 28 | return "OK!", nil 29 | } 30 | fn2 := func() (interface{}, error) { 31 | time.Sleep(time.Second) 32 | return "RAD!", nil 33 | } 34 | fn3 := func() (interface{}, error) { 35 | return nil, fmt.Errorf("failure!") 36 | } 37 | 38 | results, errs := paralyze.Paralyze(fn1, fn2, fn3) 39 | fmt.Println(results) // prints [ OK! RAD! ] 40 | fmt.Println(errs) // prints [ failure!] 41 | } 42 | 43 | ``` 44 | 45 | parallelization with a map 46 | --------- 47 | 48 | ```go 49 | package main 50 | 51 | import ( 52 | "fmt" 53 | "time" 54 | 55 | "github.com/i/paralyze" 56 | ) 57 | 58 | func main() { 59 | results := ParalyzeM(map[string]Paralyzable{ 60 | "goodie": func() (interface{}, error) { 61 | return "wowe!", nil 62 | }, 63 | "bad": func() (interface{}, error) { 64 | return nil, fmt.Errorf("you're the worst") 65 | }, 66 | }) 67 | 68 | fmt.Println(results["goodie"]["res"]) // prints wowe! 69 | fmt.Println(results["goodie"]["err"]) // prints 70 | fmt.Println(results["bad"]["res"]) // prints 71 | fmt.Println(results["bad"]["err"]) // prints you're the worst! 72 | } 73 | 74 | ``` 75 | 76 | parallelization with timeouts 77 | --------- 78 | 79 | ```go 80 | package main 81 | 82 | import ( 83 | "fmt" 84 | "time" 85 | 86 | "github.com/i/paralyze" 87 | ) 88 | 89 | func sleepAndSay(t time.Duration) paralyze.Paralyzable{ 90 | return func() (interface{}, error) { 91 | time.Sleep(t) 92 | return fmt.Sprintf("I waited for this long: %v", t), nil 93 | } 94 | } 95 | 96 | func main() { 97 | results, errs := paralyze.ParalyzeWithTimeout( 98 | time.Second, // wait for one second before giving up 99 | sleepAndSay(500*time.Millisecond), 100 | sleepAndSay(2*time.Second), 101 | ) 102 | fmt.Println(results) // prints [I waited for this long: 500ms ] 103 | fmt.Println(errs) // prints [ timed out] 104 | } 105 | 106 | ``` 107 | 108 | parallelization using context 109 | --------- 110 | 111 | ```go 112 | package main 113 | 114 | import ( 115 | "fmt" 116 | "time" 117 | 118 | "golang.org/x/net/context" 119 | "golang.org/x/net/context/ctxhttp" 120 | 121 | "github.com/i/paralyze" 122 | ) 123 | 124 | func newRequestMaker(url string) ParalyzableCtx { 125 | return func(ctx context.Context) (interface{}, error) { 126 | return ctxhttp.Get(ctx, nil, url) 127 | } 128 | } 129 | 130 | func main() { 131 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) 132 | defer cancel() 133 | 134 | responses, errors := paralyze.ParalyzeWithContext( 135 | ctx, 136 | newRequestMaker("https://google.com"), 137 | newRequestMaker("http://rms.sexy"), 138 | ) 139 | 140 | // do something great 141 | } 142 | 143 | ``` 144 | 145 | contibuting 146 | --------- 147 | fork the repo and open a PR 148 | 149 | license 150 | --------- 151 | MIT 152 | -------------------------------------------------------------------------------- /bench_test.go: -------------------------------------------------------------------------------- 1 | package paralyze 2 | 3 | import "testing" 4 | 5 | var ( 6 | fasterFn = func() (interface{}, error) { return 55, nil } 7 | ) 8 | 9 | func BenchmarkWithChannelAlloc(test *testing.B) { 10 | for i := 0; i < test.N; i++ { 11 | ParalyzeWithTimeout(0, 12 | fasterFn, 13 | fasterFn, 14 | fasterFn, 15 | fasterFn, 16 | fasterFn, 17 | fasterFn, 18 | ) 19 | } 20 | } 21 | 22 | func BenchmarkWithoutChannelAlloc(test *testing.B) { 23 | for i := 0; i < test.N; i++ { 24 | Paralyze( 25 | fasterFn, 26 | fasterFn, 27 | fasterFn, 28 | fasterFn, 29 | fasterFn, 30 | fasterFn, 31 | ) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/i/paralyze 2 | 3 | require ( 4 | github.com/davecgh/go-spew v1.1.1 // indirect 5 | github.com/stretchr/testify v1.3.0 6 | ) 7 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 4 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 6 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 7 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 8 | github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= 9 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 10 | -------------------------------------------------------------------------------- /paralyze.go: -------------------------------------------------------------------------------- 1 | package paralyze 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "sync" 7 | "time" 8 | ) 9 | 10 | // Paralyzable is a type of function that can be paralyzed. Since most 11 | // functions don't carry this signature, a common pattern is to wrap an 12 | // existing function in a Paralyzable function. 13 | type Paralyzable func() (interface{}, error) 14 | 15 | // ParalyzableCtx is the same as a Paralyzable function, except it accepts a 16 | // context.Context. Functions that implement this type should respect the 17 | // documentation found here: https://godoc.org/context 18 | type ParalyzableCtx func(context.Context) (interface{}, error) 19 | 20 | // General errors that can be returned from the paralyze package 21 | var ( 22 | ErrTimedOut = errors.New("timed out") 23 | ErrCanceled = errors.New("canceled") 24 | ) 25 | 26 | // Paralyze parallelizes a function and returns a slice containing results and 27 | // a slice containing errors. The results at each index are not mutually exclusive, 28 | // that is if results[i] is not nil, errors[i] is not guaranteed to be nil. 29 | func Paralyze(funcs ...Paralyzable) (results []interface{}, errors []error) { 30 | var wg sync.WaitGroup 31 | results = make([]interface{}, len(funcs)) 32 | errors = make([]error, len(funcs)) 33 | wg.Add(len(funcs)) 34 | 35 | var panik interface{} 36 | var panikOnce sync.Once 37 | 38 | for i, fn := range funcs { 39 | go func(i int, fn Paralyzable) { 40 | defer func() { 41 | if r := recover(); r != nil { 42 | panikOnce.Do(func() { panik = r }) 43 | } 44 | }() 45 | defer wg.Done() 46 | results[i], errors[i] = fn() 47 | }(i, fn) 48 | } 49 | wg.Wait() 50 | 51 | if panik != nil { 52 | panic(panik) 53 | } 54 | 55 | return results, errors 56 | } 57 | 58 | type ResErr struct { 59 | Res interface{} 60 | Err error 61 | } 62 | 63 | // ParalyzeM parallelizes a map of strings to functions. The return type is a 64 | // map of keys to a map containing two keys: res and err. 65 | func ParalyzeM(m map[string]Paralyzable) map[string]ResErr { 66 | var names []string 67 | var fns []Paralyzable 68 | 69 | for name, fn := range m { 70 | names = append(names, name) 71 | fns = append(fns, fn) 72 | } 73 | 74 | res := make(map[string]ResErr) 75 | results, errs := Paralyze(fns...) 76 | for i := range results { 77 | name := names[i] 78 | res[name] = ResErr{ 79 | Res: results[i], 80 | Err: errs[i], 81 | } 82 | } 83 | 84 | return res 85 | } 86 | 87 | // ParalyzeWithTimeout does the same as Paralyze, but it accepts a timeout. If 88 | // the timeout is exceeded before all paralyzed functions are complete, the 89 | // unfinished results will be discarded without being cancelled. Any complete 90 | // tasks will be unaffected. 91 | func ParalyzeWithTimeout(timeout time.Duration, funcs ...Paralyzable) ([]interface{}, []error) { 92 | if timeout == 0 { 93 | return Paralyze(funcs...) 94 | } 95 | 96 | cancel := make(chan struct{}) 97 | go time.AfterFunc(timeout, func() { close(cancel) }) 98 | 99 | results, errors := ParalyzeWithCancel(cancel, funcs...) 100 | for i, err := range errors { 101 | if err == ErrCanceled { 102 | errors[i] = ErrTimedOut 103 | } 104 | } 105 | return results, errors 106 | } 107 | 108 | // ParalyzeWithCancel does the same as Paralyze, but it accepts a channel that 109 | // allows the function to respond before the paralyzed functions are finished. 110 | // Any functions that are still oustanding will have errors set as ErrCanceled. 111 | func ParalyzeWithCancel(cancel <-chan struct{}, funcs ...Paralyzable) ([]interface{}, []error) { 112 | var wg sync.WaitGroup 113 | results := make([]interface{}, len(funcs)) 114 | errors := make([]error, len(funcs)) 115 | wg.Add(len(funcs)) 116 | 117 | for i, fn := range funcs { 118 | go func(i int, fn func() chan ResErr) { 119 | defer wg.Done() 120 | ch := fn() 121 | select { 122 | case resErr := <-ch: 123 | results[i] = resErr.Res 124 | errors[i] = resErr.Err 125 | case <-cancel: 126 | errors[i] = ErrCanceled 127 | } 128 | }(i, convert(fn)) 129 | } 130 | wg.Wait() 131 | return results, errors 132 | } 133 | 134 | // ParalyzeWithContext takes a slice of functions that accept a 135 | // context.Context. These functions are responsible for releasing resources 136 | // (closing connections, etc.) and should respect ctx.Done(). 137 | func ParalyzeWithContext(ctx context.Context, funcs ...ParalyzableCtx) ([]interface{}, []error) { 138 | var wg sync.WaitGroup 139 | results := make([]interface{}, len(funcs)) 140 | errors := make([]error, len(funcs)) 141 | 142 | wg.Add(len(funcs)) 143 | for i, fn := range funcs { 144 | go func(i int, fn ParalyzableCtx) { 145 | defer wg.Done() 146 | results[i], errors[i] = fn(ctx) 147 | }(i, fn) 148 | } 149 | wg.Wait() 150 | return results, errors 151 | } 152 | 153 | func convert(fn func() (interface{}, error)) func() chan ResErr { 154 | return func() chan ResErr { 155 | ch := make(chan ResErr, 1) 156 | go func() { 157 | res, err := fn() 158 | ch <- ResErr{res, err} 159 | }() 160 | return ch 161 | } 162 | } 163 | 164 | type paralyzer struct { 165 | maxConcurrency int 166 | } 167 | 168 | func ParalyzeLimit(limit int, tasks ...Paralyzable) ([]interface{}, []error) { 169 | var wg sync.WaitGroup 170 | sem := make(chan struct{}, limit) 171 | results := make([]interface{}, len(tasks)) 172 | errors := make([]error, len(tasks)) 173 | wg.Add(len(tasks)) 174 | 175 | var panik interface{} 176 | var panikOnce sync.Once 177 | 178 | for i, fn := range tasks { 179 | sem <- struct{}{} 180 | go func(i int, fn Paralyzable) { 181 | defer func() { 182 | wg.Done() 183 | <-sem 184 | if r := recover(); r != nil { 185 | panikOnce.Do(func() { panik = r }) 186 | } 187 | }() 188 | results[i], errors[i] = fn() 189 | }(i, fn) 190 | } 191 | wg.Wait() 192 | 193 | if panik != nil { 194 | panic(panik) 195 | } 196 | 197 | return results, errors 198 | } 199 | -------------------------------------------------------------------------------- /paralyze_test.go: -------------------------------------------------------------------------------- 1 | package paralyze 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | "testing" 8 | "time" 9 | 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | var ( 14 | someError = errors.New("some error") 15 | 16 | slowFn = func() (interface{}, error) { time.Sleep(time.Second); return "ok", nil } 17 | fastFn = func() (interface{}, error) { return 55, nil } 18 | errFn = func() (interface{}, error) { return nil, someError } 19 | ) 20 | 21 | func TestParalyze(t *testing.T) { 22 | results, errs := Paralyze(slowFn, fastFn, errFn) 23 | 24 | // Make sure both slices returned are the correct length 25 | assert.Equal(t, 3, len(results)) 26 | assert.Equal(t, 3, len(errs)) 27 | 28 | // Assert that return values are in the correct order 29 | assert.Equal(t, "ok", results[0]) 30 | assert.Equal(t, 55, results[1]) 31 | assert.Nil(t, results[2]) 32 | 33 | // Assert that errors are 34 | assert.Nil(t, errs[0]) 35 | assert.Nil(t, errs[1]) 36 | assert.Equal(t, someError, errs[2]) 37 | } 38 | 39 | func TestParalyzeWithTimeout(t *testing.T) { 40 | results, errs := ParalyzeWithTimeout(time.Second/2, slowFn, fastFn, errFn) 41 | 42 | // Make sure both slices returned are the correct length 43 | assert.Equal(t, 3, len(results)) 44 | assert.Equal(t, 3, len(errs)) 45 | 46 | // Assert that return values are in the correct order 47 | assert.Nil(t, results[0]) 48 | assert.Equal(t, 55, results[1]) 49 | assert.Nil(t, results[2]) 50 | 51 | // Assert that errors are 52 | assert.Error(t, errs[0]) 53 | assert.Nil(t, errs[1]) 54 | assert.Equal(t, someError, errs[2]) 55 | } 56 | 57 | func TestParalyzeWithCtx(t *testing.T) { 58 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) 59 | defer cancel() 60 | 61 | results, errors := ParalyzeWithContext( 62 | ctx, 63 | fnCreator(500*time.Millisecond), 64 | fnCreator(1200*time.Millisecond), 65 | ) 66 | 67 | assert.Equal(t, "success", results[0]) 68 | assert.NoError(t, errors[0]) 69 | 70 | assert.Nil(t, results[1]) 71 | assert.Error(t, errors[1]) 72 | } 73 | 74 | func TestParalyzeM(t *testing.T) { 75 | errBadThing := errors.New("bad thing") 76 | 77 | results := ParalyzeM(map[string]Paralyzable{ 78 | "foo": func() (interface{}, error) { 79 | return "foo", nil 80 | }, 81 | "err": func() (interface{}, error) { 82 | return nil, errBadThing 83 | }, 84 | }) 85 | 86 | assert.Equal(t, "foo", results["foo"].Res, "foo") 87 | assert.Nil(t, results["foo"].Err) 88 | assert.Nil(t, results["err"].Res) 89 | assert.Equal(t, errBadThing, results["err"].Err) 90 | } 91 | 92 | func fnCreator(wait time.Duration) ParalyzableCtx { 93 | return func(ctx context.Context) (interface{}, error) { 94 | select { 95 | case <-time.After(wait): 96 | return "success", nil 97 | case <-ctx.Done(): 98 | // clean up resources 99 | return nil, fmt.Errorf("timed out") 100 | } 101 | } 102 | } 103 | 104 | func TestParalyzePanic(t *testing.T) { 105 | assert.Panics(t, func() { 106 | Paralyze( 107 | func() (interface{}, error) { 108 | panic("whoops") 109 | return nil, nil 110 | }, 111 | ) 112 | }) 113 | } 114 | 115 | func TestParalyzeLimit(t *testing.T) { 116 | results, errs := ParalyzeLimit(2, slowFn, fastFn, errFn) 117 | 118 | // Make sure both slices returned are the correct length 119 | assert.Equal(t, 3, len(results)) 120 | assert.Equal(t, 3, len(errs)) 121 | 122 | // Assert that return values are in the correct order 123 | assert.Equal(t, "ok", results[0]) 124 | assert.Equal(t, 55, results[1]) 125 | assert.Nil(t, results[2]) 126 | 127 | // Assert that errors are 128 | assert.Nil(t, errs[0]) 129 | assert.Nil(t, errs[1]) 130 | assert.Equal(t, someError, errs[2]) 131 | } 132 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2012-2016 Dave Collins 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/bypass.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2016 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when the code is not running on Google App Engine, compiled by GopherJS, and 17 | // "-tags safe" is not added to the go build command line. The "disableunsafe" 18 | // tag is deprecated and thus should not be used. 19 | // Go versions prior to 1.4 are disabled because they use a different layout 20 | // for interfaces which make the implementation of unsafeReflectValue more complex. 21 | // +build !js,!appengine,!safe,!disableunsafe,go1.4 22 | 23 | package spew 24 | 25 | import ( 26 | "reflect" 27 | "unsafe" 28 | ) 29 | 30 | const ( 31 | // UnsafeDisabled is a build-time constant which specifies whether or 32 | // not access to the unsafe package is available. 33 | UnsafeDisabled = false 34 | 35 | // ptrSize is the size of a pointer on the current arch. 36 | ptrSize = unsafe.Sizeof((*byte)(nil)) 37 | ) 38 | 39 | type flag uintptr 40 | 41 | var ( 42 | // flagRO indicates whether the value field of a reflect.Value 43 | // is read-only. 44 | flagRO flag 45 | 46 | // flagAddr indicates whether the address of the reflect.Value's 47 | // value may be taken. 48 | flagAddr flag 49 | ) 50 | 51 | // flagKindMask holds the bits that make up the kind 52 | // part of the flags field. In all the supported versions, 53 | // it is in the lower 5 bits. 54 | const flagKindMask = flag(0x1f) 55 | 56 | // Different versions of Go have used different 57 | // bit layouts for the flags type. This table 58 | // records the known combinations. 59 | var okFlags = []struct { 60 | ro, addr flag 61 | }{{ 62 | // From Go 1.4 to 1.5 63 | ro: 1 << 5, 64 | addr: 1 << 7, 65 | }, { 66 | // Up to Go tip. 67 | ro: 1<<5 | 1<<6, 68 | addr: 1 << 8, 69 | }} 70 | 71 | var flagValOffset = func() uintptr { 72 | field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") 73 | if !ok { 74 | panic("reflect.Value has no flag field") 75 | } 76 | return field.Offset 77 | }() 78 | 79 | // flagField returns a pointer to the flag field of a reflect.Value. 80 | func flagField(v *reflect.Value) *flag { 81 | return (*flag)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + flagValOffset)) 82 | } 83 | 84 | // unsafeReflectValue converts the passed reflect.Value into a one that bypasses 85 | // the typical safety restrictions preventing access to unaddressable and 86 | // unexported data. It works by digging the raw pointer to the underlying 87 | // value out of the protected value and generating a new unprotected (unsafe) 88 | // reflect.Value to it. 89 | // 90 | // This allows us to check for implementations of the Stringer and error 91 | // interfaces to be used for pretty printing ordinarily unaddressable and 92 | // inaccessible values such as unexported struct fields. 93 | func unsafeReflectValue(v reflect.Value) reflect.Value { 94 | if !v.IsValid() || (v.CanInterface() && v.CanAddr()) { 95 | return v 96 | } 97 | flagFieldPtr := flagField(&v) 98 | *flagFieldPtr &^= flagRO 99 | *flagFieldPtr |= flagAddr 100 | return v 101 | } 102 | 103 | // Sanity checks against future reflect package changes 104 | // to the type or semantics of the Value.flag field. 105 | func init() { 106 | field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") 107 | if !ok { 108 | panic("reflect.Value has no flag field") 109 | } 110 | if field.Type.Kind() != reflect.TypeOf(flag(0)).Kind() { 111 | panic("reflect.Value flag field has changed kind") 112 | } 113 | type t0 int 114 | var t struct { 115 | A t0 116 | // t0 will have flagEmbedRO set. 117 | t0 118 | // a will have flagStickyRO set 119 | a t0 120 | } 121 | vA := reflect.ValueOf(t).FieldByName("A") 122 | va := reflect.ValueOf(t).FieldByName("a") 123 | vt0 := reflect.ValueOf(t).FieldByName("t0") 124 | 125 | // Infer flagRO from the difference between the flags 126 | // for the (otherwise identical) fields in t. 127 | flagPublic := *flagField(&vA) 128 | flagWithRO := *flagField(&va) | *flagField(&vt0) 129 | flagRO = flagPublic ^ flagWithRO 130 | 131 | // Infer flagAddr from the difference between a value 132 | // taken from a pointer and not. 133 | vPtrA := reflect.ValueOf(&t).Elem().FieldByName("A") 134 | flagNoPtr := *flagField(&vA) 135 | flagPtr := *flagField(&vPtrA) 136 | flagAddr = flagNoPtr ^ flagPtr 137 | 138 | // Check that the inferred flags tally with one of the known versions. 139 | for _, f := range okFlags { 140 | if flagRO == f.ro && flagAddr == f.addr { 141 | return 142 | } 143 | } 144 | panic("reflect.Value read-only flag has changed semantics") 145 | } 146 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/bypasssafe.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2016 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when the code is running on Google App Engine, compiled by GopherJS, or 17 | // "-tags safe" is added to the go build command line. The "disableunsafe" 18 | // tag is deprecated and thus should not be used. 19 | // +build js appengine safe disableunsafe !go1.4 20 | 21 | package spew 22 | 23 | import "reflect" 24 | 25 | const ( 26 | // UnsafeDisabled is a build-time constant which specifies whether or 27 | // not access to the unsafe package is available. 28 | UnsafeDisabled = true 29 | ) 30 | 31 | // unsafeReflectValue typically converts the passed reflect.Value into a one 32 | // that bypasses the typical safety restrictions preventing access to 33 | // unaddressable and unexported data. However, doing this relies on access to 34 | // the unsafe package. This is a stub version which simply returns the passed 35 | // reflect.Value when the unsafe package is not available. 36 | func unsafeReflectValue(v reflect.Value) reflect.Value { 37 | return v 38 | } 39 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/common.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "io" 23 | "reflect" 24 | "sort" 25 | "strconv" 26 | ) 27 | 28 | // Some constants in the form of bytes to avoid string overhead. This mirrors 29 | // the technique used in the fmt package. 30 | var ( 31 | panicBytes = []byte("(PANIC=") 32 | plusBytes = []byte("+") 33 | iBytes = []byte("i") 34 | trueBytes = []byte("true") 35 | falseBytes = []byte("false") 36 | interfaceBytes = []byte("(interface {})") 37 | commaNewlineBytes = []byte(",\n") 38 | newlineBytes = []byte("\n") 39 | openBraceBytes = []byte("{") 40 | openBraceNewlineBytes = []byte("{\n") 41 | closeBraceBytes = []byte("}") 42 | asteriskBytes = []byte("*") 43 | colonBytes = []byte(":") 44 | colonSpaceBytes = []byte(": ") 45 | openParenBytes = []byte("(") 46 | closeParenBytes = []byte(")") 47 | spaceBytes = []byte(" ") 48 | pointerChainBytes = []byte("->") 49 | nilAngleBytes = []byte("") 50 | maxNewlineBytes = []byte("\n") 51 | maxShortBytes = []byte("") 52 | circularBytes = []byte("") 53 | circularShortBytes = []byte("") 54 | invalidAngleBytes = []byte("") 55 | openBracketBytes = []byte("[") 56 | closeBracketBytes = []byte("]") 57 | percentBytes = []byte("%") 58 | precisionBytes = []byte(".") 59 | openAngleBytes = []byte("<") 60 | closeAngleBytes = []byte(">") 61 | openMapBytes = []byte("map[") 62 | closeMapBytes = []byte("]") 63 | lenEqualsBytes = []byte("len=") 64 | capEqualsBytes = []byte("cap=") 65 | ) 66 | 67 | // hexDigits is used to map a decimal value to a hex digit. 68 | var hexDigits = "0123456789abcdef" 69 | 70 | // catchPanic handles any panics that might occur during the handleMethods 71 | // calls. 72 | func catchPanic(w io.Writer, v reflect.Value) { 73 | if err := recover(); err != nil { 74 | w.Write(panicBytes) 75 | fmt.Fprintf(w, "%v", err) 76 | w.Write(closeParenBytes) 77 | } 78 | } 79 | 80 | // handleMethods attempts to call the Error and String methods on the underlying 81 | // type the passed reflect.Value represents and outputes the result to Writer w. 82 | // 83 | // It handles panics in any called methods by catching and displaying the error 84 | // as the formatted value. 85 | func handleMethods(cs *ConfigState, w io.Writer, v reflect.Value) (handled bool) { 86 | // We need an interface to check if the type implements the error or 87 | // Stringer interface. However, the reflect package won't give us an 88 | // interface on certain things like unexported struct fields in order 89 | // to enforce visibility rules. We use unsafe, when it's available, 90 | // to bypass these restrictions since this package does not mutate the 91 | // values. 92 | if !v.CanInterface() { 93 | if UnsafeDisabled { 94 | return false 95 | } 96 | 97 | v = unsafeReflectValue(v) 98 | } 99 | 100 | // Choose whether or not to do error and Stringer interface lookups against 101 | // the base type or a pointer to the base type depending on settings. 102 | // Technically calling one of these methods with a pointer receiver can 103 | // mutate the value, however, types which choose to satisify an error or 104 | // Stringer interface with a pointer receiver should not be mutating their 105 | // state inside these interface methods. 106 | if !cs.DisablePointerMethods && !UnsafeDisabled && !v.CanAddr() { 107 | v = unsafeReflectValue(v) 108 | } 109 | if v.CanAddr() { 110 | v = v.Addr() 111 | } 112 | 113 | // Is it an error or Stringer? 114 | switch iface := v.Interface().(type) { 115 | case error: 116 | defer catchPanic(w, v) 117 | if cs.ContinueOnMethod { 118 | w.Write(openParenBytes) 119 | w.Write([]byte(iface.Error())) 120 | w.Write(closeParenBytes) 121 | w.Write(spaceBytes) 122 | return false 123 | } 124 | 125 | w.Write([]byte(iface.Error())) 126 | return true 127 | 128 | case fmt.Stringer: 129 | defer catchPanic(w, v) 130 | if cs.ContinueOnMethod { 131 | w.Write(openParenBytes) 132 | w.Write([]byte(iface.String())) 133 | w.Write(closeParenBytes) 134 | w.Write(spaceBytes) 135 | return false 136 | } 137 | w.Write([]byte(iface.String())) 138 | return true 139 | } 140 | return false 141 | } 142 | 143 | // printBool outputs a boolean value as true or false to Writer w. 144 | func printBool(w io.Writer, val bool) { 145 | if val { 146 | w.Write(trueBytes) 147 | } else { 148 | w.Write(falseBytes) 149 | } 150 | } 151 | 152 | // printInt outputs a signed integer value to Writer w. 153 | func printInt(w io.Writer, val int64, base int) { 154 | w.Write([]byte(strconv.FormatInt(val, base))) 155 | } 156 | 157 | // printUint outputs an unsigned integer value to Writer w. 158 | func printUint(w io.Writer, val uint64, base int) { 159 | w.Write([]byte(strconv.FormatUint(val, base))) 160 | } 161 | 162 | // printFloat outputs a floating point value using the specified precision, 163 | // which is expected to be 32 or 64bit, to Writer w. 164 | func printFloat(w io.Writer, val float64, precision int) { 165 | w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision))) 166 | } 167 | 168 | // printComplex outputs a complex value using the specified float precision 169 | // for the real and imaginary parts to Writer w. 170 | func printComplex(w io.Writer, c complex128, floatPrecision int) { 171 | r := real(c) 172 | w.Write(openParenBytes) 173 | w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision))) 174 | i := imag(c) 175 | if i >= 0 { 176 | w.Write(plusBytes) 177 | } 178 | w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision))) 179 | w.Write(iBytes) 180 | w.Write(closeParenBytes) 181 | } 182 | 183 | // printHexPtr outputs a uintptr formatted as hexadecimal with a leading '0x' 184 | // prefix to Writer w. 185 | func printHexPtr(w io.Writer, p uintptr) { 186 | // Null pointer. 187 | num := uint64(p) 188 | if num == 0 { 189 | w.Write(nilAngleBytes) 190 | return 191 | } 192 | 193 | // Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix 194 | buf := make([]byte, 18) 195 | 196 | // It's simpler to construct the hex string right to left. 197 | base := uint64(16) 198 | i := len(buf) - 1 199 | for num >= base { 200 | buf[i] = hexDigits[num%base] 201 | num /= base 202 | i-- 203 | } 204 | buf[i] = hexDigits[num] 205 | 206 | // Add '0x' prefix. 207 | i-- 208 | buf[i] = 'x' 209 | i-- 210 | buf[i] = '0' 211 | 212 | // Strip unused leading bytes. 213 | buf = buf[i:] 214 | w.Write(buf) 215 | } 216 | 217 | // valuesSorter implements sort.Interface to allow a slice of reflect.Value 218 | // elements to be sorted. 219 | type valuesSorter struct { 220 | values []reflect.Value 221 | strings []string // either nil or same len and values 222 | cs *ConfigState 223 | } 224 | 225 | // newValuesSorter initializes a valuesSorter instance, which holds a set of 226 | // surrogate keys on which the data should be sorted. It uses flags in 227 | // ConfigState to decide if and how to populate those surrogate keys. 228 | func newValuesSorter(values []reflect.Value, cs *ConfigState) sort.Interface { 229 | vs := &valuesSorter{values: values, cs: cs} 230 | if canSortSimply(vs.values[0].Kind()) { 231 | return vs 232 | } 233 | if !cs.DisableMethods { 234 | vs.strings = make([]string, len(values)) 235 | for i := range vs.values { 236 | b := bytes.Buffer{} 237 | if !handleMethods(cs, &b, vs.values[i]) { 238 | vs.strings = nil 239 | break 240 | } 241 | vs.strings[i] = b.String() 242 | } 243 | } 244 | if vs.strings == nil && cs.SpewKeys { 245 | vs.strings = make([]string, len(values)) 246 | for i := range vs.values { 247 | vs.strings[i] = Sprintf("%#v", vs.values[i].Interface()) 248 | } 249 | } 250 | return vs 251 | } 252 | 253 | // canSortSimply tests whether a reflect.Kind is a primitive that can be sorted 254 | // directly, or whether it should be considered for sorting by surrogate keys 255 | // (if the ConfigState allows it). 256 | func canSortSimply(kind reflect.Kind) bool { 257 | // This switch parallels valueSortLess, except for the default case. 258 | switch kind { 259 | case reflect.Bool: 260 | return true 261 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 262 | return true 263 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 264 | return true 265 | case reflect.Float32, reflect.Float64: 266 | return true 267 | case reflect.String: 268 | return true 269 | case reflect.Uintptr: 270 | return true 271 | case reflect.Array: 272 | return true 273 | } 274 | return false 275 | } 276 | 277 | // Len returns the number of values in the slice. It is part of the 278 | // sort.Interface implementation. 279 | func (s *valuesSorter) Len() int { 280 | return len(s.values) 281 | } 282 | 283 | // Swap swaps the values at the passed indices. It is part of the 284 | // sort.Interface implementation. 285 | func (s *valuesSorter) Swap(i, j int) { 286 | s.values[i], s.values[j] = s.values[j], s.values[i] 287 | if s.strings != nil { 288 | s.strings[i], s.strings[j] = s.strings[j], s.strings[i] 289 | } 290 | } 291 | 292 | // valueSortLess returns whether the first value should sort before the second 293 | // value. It is used by valueSorter.Less as part of the sort.Interface 294 | // implementation. 295 | func valueSortLess(a, b reflect.Value) bool { 296 | switch a.Kind() { 297 | case reflect.Bool: 298 | return !a.Bool() && b.Bool() 299 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 300 | return a.Int() < b.Int() 301 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 302 | return a.Uint() < b.Uint() 303 | case reflect.Float32, reflect.Float64: 304 | return a.Float() < b.Float() 305 | case reflect.String: 306 | return a.String() < b.String() 307 | case reflect.Uintptr: 308 | return a.Uint() < b.Uint() 309 | case reflect.Array: 310 | // Compare the contents of both arrays. 311 | l := a.Len() 312 | for i := 0; i < l; i++ { 313 | av := a.Index(i) 314 | bv := b.Index(i) 315 | if av.Interface() == bv.Interface() { 316 | continue 317 | } 318 | return valueSortLess(av, bv) 319 | } 320 | } 321 | return a.String() < b.String() 322 | } 323 | 324 | // Less returns whether the value at index i should sort before the 325 | // value at index j. It is part of the sort.Interface implementation. 326 | func (s *valuesSorter) Less(i, j int) bool { 327 | if s.strings == nil { 328 | return valueSortLess(s.values[i], s.values[j]) 329 | } 330 | return s.strings[i] < s.strings[j] 331 | } 332 | 333 | // sortValues is a sort function that handles both native types and any type that 334 | // can be converted to error or Stringer. Other inputs are sorted according to 335 | // their Value.String() value to ensure display stability. 336 | func sortValues(values []reflect.Value, cs *ConfigState) { 337 | if len(values) == 0 { 338 | return 339 | } 340 | sort.Sort(newValuesSorter(values, cs)) 341 | } 342 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "io" 23 | "os" 24 | ) 25 | 26 | // ConfigState houses the configuration options used by spew to format and 27 | // display values. There is a global instance, Config, that is used to control 28 | // all top-level Formatter and Dump functionality. Each ConfigState instance 29 | // provides methods equivalent to the top-level functions. 30 | // 31 | // The zero value for ConfigState provides no indentation. You would typically 32 | // want to set it to a space or a tab. 33 | // 34 | // Alternatively, you can use NewDefaultConfig to get a ConfigState instance 35 | // with default settings. See the documentation of NewDefaultConfig for default 36 | // values. 37 | type ConfigState struct { 38 | // Indent specifies the string to use for each indentation level. The 39 | // global config instance that all top-level functions use set this to a 40 | // single space by default. If you would like more indentation, you might 41 | // set this to a tab with "\t" or perhaps two spaces with " ". 42 | Indent string 43 | 44 | // MaxDepth controls the maximum number of levels to descend into nested 45 | // data structures. The default, 0, means there is no limit. 46 | // 47 | // NOTE: Circular data structures are properly detected, so it is not 48 | // necessary to set this value unless you specifically want to limit deeply 49 | // nested data structures. 50 | MaxDepth int 51 | 52 | // DisableMethods specifies whether or not error and Stringer interfaces are 53 | // invoked for types that implement them. 54 | DisableMethods bool 55 | 56 | // DisablePointerMethods specifies whether or not to check for and invoke 57 | // error and Stringer interfaces on types which only accept a pointer 58 | // receiver when the current type is not a pointer. 59 | // 60 | // NOTE: This might be an unsafe action since calling one of these methods 61 | // with a pointer receiver could technically mutate the value, however, 62 | // in practice, types which choose to satisify an error or Stringer 63 | // interface with a pointer receiver should not be mutating their state 64 | // inside these interface methods. As a result, this option relies on 65 | // access to the unsafe package, so it will not have any effect when 66 | // running in environments without access to the unsafe package such as 67 | // Google App Engine or with the "safe" build tag specified. 68 | DisablePointerMethods bool 69 | 70 | // DisablePointerAddresses specifies whether to disable the printing of 71 | // pointer addresses. This is useful when diffing data structures in tests. 72 | DisablePointerAddresses bool 73 | 74 | // DisableCapacities specifies whether to disable the printing of capacities 75 | // for arrays, slices, maps and channels. This is useful when diffing 76 | // data structures in tests. 77 | DisableCapacities bool 78 | 79 | // ContinueOnMethod specifies whether or not recursion should continue once 80 | // a custom error or Stringer interface is invoked. The default, false, 81 | // means it will print the results of invoking the custom error or Stringer 82 | // interface and return immediately instead of continuing to recurse into 83 | // the internals of the data type. 84 | // 85 | // NOTE: This flag does not have any effect if method invocation is disabled 86 | // via the DisableMethods or DisablePointerMethods options. 87 | ContinueOnMethod bool 88 | 89 | // SortKeys specifies map keys should be sorted before being printed. Use 90 | // this to have a more deterministic, diffable output. Note that only 91 | // native types (bool, int, uint, floats, uintptr and string) and types 92 | // that support the error or Stringer interfaces (if methods are 93 | // enabled) are supported, with other types sorted according to the 94 | // reflect.Value.String() output which guarantees display stability. 95 | SortKeys bool 96 | 97 | // SpewKeys specifies that, as a last resort attempt, map keys should 98 | // be spewed to strings and sorted by those strings. This is only 99 | // considered if SortKeys is true. 100 | SpewKeys bool 101 | } 102 | 103 | // Config is the active configuration of the top-level functions. 104 | // The configuration can be changed by modifying the contents of spew.Config. 105 | var Config = ConfigState{Indent: " "} 106 | 107 | // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were 108 | // passed with a Formatter interface returned by c.NewFormatter. It returns 109 | // the formatted string as a value that satisfies error. See NewFormatter 110 | // for formatting details. 111 | // 112 | // This function is shorthand for the following syntax: 113 | // 114 | // fmt.Errorf(format, c.NewFormatter(a), c.NewFormatter(b)) 115 | func (c *ConfigState) Errorf(format string, a ...interface{}) (err error) { 116 | return fmt.Errorf(format, c.convertArgs(a)...) 117 | } 118 | 119 | // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were 120 | // passed with a Formatter interface returned by c.NewFormatter. It returns 121 | // the number of bytes written and any write error encountered. See 122 | // NewFormatter for formatting details. 123 | // 124 | // This function is shorthand for the following syntax: 125 | // 126 | // fmt.Fprint(w, c.NewFormatter(a), c.NewFormatter(b)) 127 | func (c *ConfigState) Fprint(w io.Writer, a ...interface{}) (n int, err error) { 128 | return fmt.Fprint(w, c.convertArgs(a)...) 129 | } 130 | 131 | // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were 132 | // passed with a Formatter interface returned by c.NewFormatter. It returns 133 | // the number of bytes written and any write error encountered. See 134 | // NewFormatter for formatting details. 135 | // 136 | // This function is shorthand for the following syntax: 137 | // 138 | // fmt.Fprintf(w, format, c.NewFormatter(a), c.NewFormatter(b)) 139 | func (c *ConfigState) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { 140 | return fmt.Fprintf(w, format, c.convertArgs(a)...) 141 | } 142 | 143 | // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it 144 | // passed with a Formatter interface returned by c.NewFormatter. See 145 | // NewFormatter for formatting details. 146 | // 147 | // This function is shorthand for the following syntax: 148 | // 149 | // fmt.Fprintln(w, c.NewFormatter(a), c.NewFormatter(b)) 150 | func (c *ConfigState) Fprintln(w io.Writer, a ...interface{}) (n int, err error) { 151 | return fmt.Fprintln(w, c.convertArgs(a)...) 152 | } 153 | 154 | // Print is a wrapper for fmt.Print that treats each argument as if it were 155 | // passed with a Formatter interface returned by c.NewFormatter. It returns 156 | // the number of bytes written and any write error encountered. See 157 | // NewFormatter for formatting details. 158 | // 159 | // This function is shorthand for the following syntax: 160 | // 161 | // fmt.Print(c.NewFormatter(a), c.NewFormatter(b)) 162 | func (c *ConfigState) Print(a ...interface{}) (n int, err error) { 163 | return fmt.Print(c.convertArgs(a)...) 164 | } 165 | 166 | // Printf is a wrapper for fmt.Printf that treats each argument as if it were 167 | // passed with a Formatter interface returned by c.NewFormatter. It returns 168 | // the number of bytes written and any write error encountered. See 169 | // NewFormatter for formatting details. 170 | // 171 | // This function is shorthand for the following syntax: 172 | // 173 | // fmt.Printf(format, c.NewFormatter(a), c.NewFormatter(b)) 174 | func (c *ConfigState) Printf(format string, a ...interface{}) (n int, err error) { 175 | return fmt.Printf(format, c.convertArgs(a)...) 176 | } 177 | 178 | // Println is a wrapper for fmt.Println that treats each argument as if it were 179 | // passed with a Formatter interface returned by c.NewFormatter. It returns 180 | // the number of bytes written and any write error encountered. See 181 | // NewFormatter for formatting details. 182 | // 183 | // This function is shorthand for the following syntax: 184 | // 185 | // fmt.Println(c.NewFormatter(a), c.NewFormatter(b)) 186 | func (c *ConfigState) Println(a ...interface{}) (n int, err error) { 187 | return fmt.Println(c.convertArgs(a)...) 188 | } 189 | 190 | // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were 191 | // passed with a Formatter interface returned by c.NewFormatter. It returns 192 | // the resulting string. See NewFormatter for formatting details. 193 | // 194 | // This function is shorthand for the following syntax: 195 | // 196 | // fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b)) 197 | func (c *ConfigState) Sprint(a ...interface{}) string { 198 | return fmt.Sprint(c.convertArgs(a)...) 199 | } 200 | 201 | // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were 202 | // passed with a Formatter interface returned by c.NewFormatter. It returns 203 | // the resulting string. See NewFormatter for formatting details. 204 | // 205 | // This function is shorthand for the following syntax: 206 | // 207 | // fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b)) 208 | func (c *ConfigState) Sprintf(format string, a ...interface{}) string { 209 | return fmt.Sprintf(format, c.convertArgs(a)...) 210 | } 211 | 212 | // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it 213 | // were passed with a Formatter interface returned by c.NewFormatter. It 214 | // returns the resulting string. See NewFormatter for formatting details. 215 | // 216 | // This function is shorthand for the following syntax: 217 | // 218 | // fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b)) 219 | func (c *ConfigState) Sprintln(a ...interface{}) string { 220 | return fmt.Sprintln(c.convertArgs(a)...) 221 | } 222 | 223 | /* 224 | NewFormatter returns a custom formatter that satisfies the fmt.Formatter 225 | interface. As a result, it integrates cleanly with standard fmt package 226 | printing functions. The formatter is useful for inline printing of smaller data 227 | types similar to the standard %v format specifier. 228 | 229 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 230 | addresses), %#v (adds types), and %#+v (adds types and pointer addresses) verb 231 | combinations. Any other verbs such as %x and %q will be sent to the the 232 | standard fmt package for formatting. In addition, the custom formatter ignores 233 | the width and precision arguments (however they will still work on the format 234 | specifiers not handled by the custom formatter). 235 | 236 | Typically this function shouldn't be called directly. It is much easier to make 237 | use of the custom formatter by calling one of the convenience functions such as 238 | c.Printf, c.Println, or c.Printf. 239 | */ 240 | func (c *ConfigState) NewFormatter(v interface{}) fmt.Formatter { 241 | return newFormatter(c, v) 242 | } 243 | 244 | // Fdump formats and displays the passed arguments to io.Writer w. It formats 245 | // exactly the same as Dump. 246 | func (c *ConfigState) Fdump(w io.Writer, a ...interface{}) { 247 | fdump(c, w, a...) 248 | } 249 | 250 | /* 251 | Dump displays the passed parameters to standard out with newlines, customizable 252 | indentation, and additional debug information such as complete types and all 253 | pointer addresses used to indirect to the final value. It provides the 254 | following features over the built-in printing facilities provided by the fmt 255 | package: 256 | 257 | * Pointers are dereferenced and followed 258 | * Circular data structures are detected and handled properly 259 | * Custom Stringer/error interfaces are optionally invoked, including 260 | on unexported types 261 | * Custom types which only implement the Stringer/error interfaces via 262 | a pointer receiver are optionally invoked when passing non-pointer 263 | variables 264 | * Byte arrays and slices are dumped like the hexdump -C command which 265 | includes offsets, byte values in hex, and ASCII output 266 | 267 | The configuration options are controlled by modifying the public members 268 | of c. See ConfigState for options documentation. 269 | 270 | See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to 271 | get the formatted result as a string. 272 | */ 273 | func (c *ConfigState) Dump(a ...interface{}) { 274 | fdump(c, os.Stdout, a...) 275 | } 276 | 277 | // Sdump returns a string with the passed arguments formatted exactly the same 278 | // as Dump. 279 | func (c *ConfigState) Sdump(a ...interface{}) string { 280 | var buf bytes.Buffer 281 | fdump(c, &buf, a...) 282 | return buf.String() 283 | } 284 | 285 | // convertArgs accepts a slice of arguments and returns a slice of the same 286 | // length with each argument converted to a spew Formatter interface using 287 | // the ConfigState associated with s. 288 | func (c *ConfigState) convertArgs(args []interface{}) (formatters []interface{}) { 289 | formatters = make([]interface{}, len(args)) 290 | for index, arg := range args { 291 | formatters[index] = newFormatter(c, arg) 292 | } 293 | return formatters 294 | } 295 | 296 | // NewDefaultConfig returns a ConfigState with the following default settings. 297 | // 298 | // Indent: " " 299 | // MaxDepth: 0 300 | // DisableMethods: false 301 | // DisablePointerMethods: false 302 | // ContinueOnMethod: false 303 | // SortKeys: false 304 | func NewDefaultConfig() *ConfigState { 305 | return &ConfigState{Indent: " "} 306 | } 307 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /* 18 | Package spew implements a deep pretty printer for Go data structures to aid in 19 | debugging. 20 | 21 | A quick overview of the additional features spew provides over the built-in 22 | printing facilities for Go data types are as follows: 23 | 24 | * Pointers are dereferenced and followed 25 | * Circular data structures are detected and handled properly 26 | * Custom Stringer/error interfaces are optionally invoked, including 27 | on unexported types 28 | * Custom types which only implement the Stringer/error interfaces via 29 | a pointer receiver are optionally invoked when passing non-pointer 30 | variables 31 | * Byte arrays and slices are dumped like the hexdump -C command which 32 | includes offsets, byte values in hex, and ASCII output (only when using 33 | Dump style) 34 | 35 | There are two different approaches spew allows for dumping Go data structures: 36 | 37 | * Dump style which prints with newlines, customizable indentation, 38 | and additional debug information such as types and all pointer addresses 39 | used to indirect to the final value 40 | * A custom Formatter interface that integrates cleanly with the standard fmt 41 | package and replaces %v, %+v, %#v, and %#+v to provide inline printing 42 | similar to the default %v while providing the additional functionality 43 | outlined above and passing unsupported format verbs such as %x and %q 44 | along to fmt 45 | 46 | Quick Start 47 | 48 | This section demonstrates how to quickly get started with spew. See the 49 | sections below for further details on formatting and configuration options. 50 | 51 | To dump a variable with full newlines, indentation, type, and pointer 52 | information use Dump, Fdump, or Sdump: 53 | spew.Dump(myVar1, myVar2, ...) 54 | spew.Fdump(someWriter, myVar1, myVar2, ...) 55 | str := spew.Sdump(myVar1, myVar2, ...) 56 | 57 | Alternatively, if you would prefer to use format strings with a compacted inline 58 | printing style, use the convenience wrappers Printf, Fprintf, etc with 59 | %v (most compact), %+v (adds pointer addresses), %#v (adds types), or 60 | %#+v (adds types and pointer addresses): 61 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 62 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 63 | spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 64 | spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 65 | 66 | Configuration Options 67 | 68 | Configuration of spew is handled by fields in the ConfigState type. For 69 | convenience, all of the top-level functions use a global state available 70 | via the spew.Config global. 71 | 72 | It is also possible to create a ConfigState instance that provides methods 73 | equivalent to the top-level functions. This allows concurrent configuration 74 | options. See the ConfigState documentation for more details. 75 | 76 | The following configuration options are available: 77 | * Indent 78 | String to use for each indentation level for Dump functions. 79 | It is a single space by default. A popular alternative is "\t". 80 | 81 | * MaxDepth 82 | Maximum number of levels to descend into nested data structures. 83 | There is no limit by default. 84 | 85 | * DisableMethods 86 | Disables invocation of error and Stringer interface methods. 87 | Method invocation is enabled by default. 88 | 89 | * DisablePointerMethods 90 | Disables invocation of error and Stringer interface methods on types 91 | which only accept pointer receivers from non-pointer variables. 92 | Pointer method invocation is enabled by default. 93 | 94 | * DisablePointerAddresses 95 | DisablePointerAddresses specifies whether to disable the printing of 96 | pointer addresses. This is useful when diffing data structures in tests. 97 | 98 | * DisableCapacities 99 | DisableCapacities specifies whether to disable the printing of 100 | capacities for arrays, slices, maps and channels. This is useful when 101 | diffing data structures in tests. 102 | 103 | * ContinueOnMethod 104 | Enables recursion into types after invoking error and Stringer interface 105 | methods. Recursion after method invocation is disabled by default. 106 | 107 | * SortKeys 108 | Specifies map keys should be sorted before being printed. Use 109 | this to have a more deterministic, diffable output. Note that 110 | only native types (bool, int, uint, floats, uintptr and string) 111 | and types which implement error or Stringer interfaces are 112 | supported with other types sorted according to the 113 | reflect.Value.String() output which guarantees display 114 | stability. Natural map order is used by default. 115 | 116 | * SpewKeys 117 | Specifies that, as a last resort attempt, map keys should be 118 | spewed to strings and sorted by those strings. This is only 119 | considered if SortKeys is true. 120 | 121 | Dump Usage 122 | 123 | Simply call spew.Dump with a list of variables you want to dump: 124 | 125 | spew.Dump(myVar1, myVar2, ...) 126 | 127 | You may also call spew.Fdump if you would prefer to output to an arbitrary 128 | io.Writer. For example, to dump to standard error: 129 | 130 | spew.Fdump(os.Stderr, myVar1, myVar2, ...) 131 | 132 | A third option is to call spew.Sdump to get the formatted output as a string: 133 | 134 | str := spew.Sdump(myVar1, myVar2, ...) 135 | 136 | Sample Dump Output 137 | 138 | See the Dump example for details on the setup of the types and variables being 139 | shown here. 140 | 141 | (main.Foo) { 142 | unexportedField: (*main.Bar)(0xf84002e210)({ 143 | flag: (main.Flag) flagTwo, 144 | data: (uintptr) 145 | }), 146 | ExportedField: (map[interface {}]interface {}) (len=1) { 147 | (string) (len=3) "one": (bool) true 148 | } 149 | } 150 | 151 | Byte (and uint8) arrays and slices are displayed uniquely like the hexdump -C 152 | command as shown. 153 | ([]uint8) (len=32 cap=32) { 154 | 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 155 | 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 156 | 00000020 31 32 |12| 157 | } 158 | 159 | Custom Formatter 160 | 161 | Spew provides a custom formatter that implements the fmt.Formatter interface 162 | so that it integrates cleanly with standard fmt package printing functions. The 163 | formatter is useful for inline printing of smaller data types similar to the 164 | standard %v format specifier. 165 | 166 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 167 | addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb 168 | combinations. Any other verbs such as %x and %q will be sent to the the 169 | standard fmt package for formatting. In addition, the custom formatter ignores 170 | the width and precision arguments (however they will still work on the format 171 | specifiers not handled by the custom formatter). 172 | 173 | Custom Formatter Usage 174 | 175 | The simplest way to make use of the spew custom formatter is to call one of the 176 | convenience functions such as spew.Printf, spew.Println, or spew.Printf. The 177 | functions have syntax you are most likely already familiar with: 178 | 179 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 180 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 181 | spew.Println(myVar, myVar2) 182 | spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 183 | spew.Fprintf(os.Stderr, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 184 | 185 | See the Index for the full list convenience functions. 186 | 187 | Sample Formatter Output 188 | 189 | Double pointer to a uint8: 190 | %v: <**>5 191 | %+v: <**>(0xf8400420d0->0xf8400420c8)5 192 | %#v: (**uint8)5 193 | %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5 194 | 195 | Pointer to circular struct with a uint8 field and a pointer to itself: 196 | %v: <*>{1 <*>} 197 | %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)} 198 | %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)} 199 | %#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)} 200 | 201 | See the Printf example for details on the setup of variables being shown 202 | here. 203 | 204 | Errors 205 | 206 | Since it is possible for custom Stringer/error interfaces to panic, spew 207 | detects them and handles them internally by printing the panic information 208 | inline with the output. Since spew is intended to provide deep pretty printing 209 | capabilities on structures, it intentionally does not return any errors. 210 | */ 211 | package spew 212 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/dump.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "encoding/hex" 22 | "fmt" 23 | "io" 24 | "os" 25 | "reflect" 26 | "regexp" 27 | "strconv" 28 | "strings" 29 | ) 30 | 31 | var ( 32 | // uint8Type is a reflect.Type representing a uint8. It is used to 33 | // convert cgo types to uint8 slices for hexdumping. 34 | uint8Type = reflect.TypeOf(uint8(0)) 35 | 36 | // cCharRE is a regular expression that matches a cgo char. 37 | // It is used to detect character arrays to hexdump them. 38 | cCharRE = regexp.MustCompile(`^.*\._Ctype_char$`) 39 | 40 | // cUnsignedCharRE is a regular expression that matches a cgo unsigned 41 | // char. It is used to detect unsigned character arrays to hexdump 42 | // them. 43 | cUnsignedCharRE = regexp.MustCompile(`^.*\._Ctype_unsignedchar$`) 44 | 45 | // cUint8tCharRE is a regular expression that matches a cgo uint8_t. 46 | // It is used to detect uint8_t arrays to hexdump them. 47 | cUint8tCharRE = regexp.MustCompile(`^.*\._Ctype_uint8_t$`) 48 | ) 49 | 50 | // dumpState contains information about the state of a dump operation. 51 | type dumpState struct { 52 | w io.Writer 53 | depth int 54 | pointers map[uintptr]int 55 | ignoreNextType bool 56 | ignoreNextIndent bool 57 | cs *ConfigState 58 | } 59 | 60 | // indent performs indentation according to the depth level and cs.Indent 61 | // option. 62 | func (d *dumpState) indent() { 63 | if d.ignoreNextIndent { 64 | d.ignoreNextIndent = false 65 | return 66 | } 67 | d.w.Write(bytes.Repeat([]byte(d.cs.Indent), d.depth)) 68 | } 69 | 70 | // unpackValue returns values inside of non-nil interfaces when possible. 71 | // This is useful for data types like structs, arrays, slices, and maps which 72 | // can contain varying types packed inside an interface. 73 | func (d *dumpState) unpackValue(v reflect.Value) reflect.Value { 74 | if v.Kind() == reflect.Interface && !v.IsNil() { 75 | v = v.Elem() 76 | } 77 | return v 78 | } 79 | 80 | // dumpPtr handles formatting of pointers by indirecting them as necessary. 81 | func (d *dumpState) dumpPtr(v reflect.Value) { 82 | // Remove pointers at or below the current depth from map used to detect 83 | // circular refs. 84 | for k, depth := range d.pointers { 85 | if depth >= d.depth { 86 | delete(d.pointers, k) 87 | } 88 | } 89 | 90 | // Keep list of all dereferenced pointers to show later. 91 | pointerChain := make([]uintptr, 0) 92 | 93 | // Figure out how many levels of indirection there are by dereferencing 94 | // pointers and unpacking interfaces down the chain while detecting circular 95 | // references. 96 | nilFound := false 97 | cycleFound := false 98 | indirects := 0 99 | ve := v 100 | for ve.Kind() == reflect.Ptr { 101 | if ve.IsNil() { 102 | nilFound = true 103 | break 104 | } 105 | indirects++ 106 | addr := ve.Pointer() 107 | pointerChain = append(pointerChain, addr) 108 | if pd, ok := d.pointers[addr]; ok && pd < d.depth { 109 | cycleFound = true 110 | indirects-- 111 | break 112 | } 113 | d.pointers[addr] = d.depth 114 | 115 | ve = ve.Elem() 116 | if ve.Kind() == reflect.Interface { 117 | if ve.IsNil() { 118 | nilFound = true 119 | break 120 | } 121 | ve = ve.Elem() 122 | } 123 | } 124 | 125 | // Display type information. 126 | d.w.Write(openParenBytes) 127 | d.w.Write(bytes.Repeat(asteriskBytes, indirects)) 128 | d.w.Write([]byte(ve.Type().String())) 129 | d.w.Write(closeParenBytes) 130 | 131 | // Display pointer information. 132 | if !d.cs.DisablePointerAddresses && len(pointerChain) > 0 { 133 | d.w.Write(openParenBytes) 134 | for i, addr := range pointerChain { 135 | if i > 0 { 136 | d.w.Write(pointerChainBytes) 137 | } 138 | printHexPtr(d.w, addr) 139 | } 140 | d.w.Write(closeParenBytes) 141 | } 142 | 143 | // Display dereferenced value. 144 | d.w.Write(openParenBytes) 145 | switch { 146 | case nilFound: 147 | d.w.Write(nilAngleBytes) 148 | 149 | case cycleFound: 150 | d.w.Write(circularBytes) 151 | 152 | default: 153 | d.ignoreNextType = true 154 | d.dump(ve) 155 | } 156 | d.w.Write(closeParenBytes) 157 | } 158 | 159 | // dumpSlice handles formatting of arrays and slices. Byte (uint8 under 160 | // reflection) arrays and slices are dumped in hexdump -C fashion. 161 | func (d *dumpState) dumpSlice(v reflect.Value) { 162 | // Determine whether this type should be hex dumped or not. Also, 163 | // for types which should be hexdumped, try to use the underlying data 164 | // first, then fall back to trying to convert them to a uint8 slice. 165 | var buf []uint8 166 | doConvert := false 167 | doHexDump := false 168 | numEntries := v.Len() 169 | if numEntries > 0 { 170 | vt := v.Index(0).Type() 171 | vts := vt.String() 172 | switch { 173 | // C types that need to be converted. 174 | case cCharRE.MatchString(vts): 175 | fallthrough 176 | case cUnsignedCharRE.MatchString(vts): 177 | fallthrough 178 | case cUint8tCharRE.MatchString(vts): 179 | doConvert = true 180 | 181 | // Try to use existing uint8 slices and fall back to converting 182 | // and copying if that fails. 183 | case vt.Kind() == reflect.Uint8: 184 | // We need an addressable interface to convert the type 185 | // to a byte slice. However, the reflect package won't 186 | // give us an interface on certain things like 187 | // unexported struct fields in order to enforce 188 | // visibility rules. We use unsafe, when available, to 189 | // bypass these restrictions since this package does not 190 | // mutate the values. 191 | vs := v 192 | if !vs.CanInterface() || !vs.CanAddr() { 193 | vs = unsafeReflectValue(vs) 194 | } 195 | if !UnsafeDisabled { 196 | vs = vs.Slice(0, numEntries) 197 | 198 | // Use the existing uint8 slice if it can be 199 | // type asserted. 200 | iface := vs.Interface() 201 | if slice, ok := iface.([]uint8); ok { 202 | buf = slice 203 | doHexDump = true 204 | break 205 | } 206 | } 207 | 208 | // The underlying data needs to be converted if it can't 209 | // be type asserted to a uint8 slice. 210 | doConvert = true 211 | } 212 | 213 | // Copy and convert the underlying type if needed. 214 | if doConvert && vt.ConvertibleTo(uint8Type) { 215 | // Convert and copy each element into a uint8 byte 216 | // slice. 217 | buf = make([]uint8, numEntries) 218 | for i := 0; i < numEntries; i++ { 219 | vv := v.Index(i) 220 | buf[i] = uint8(vv.Convert(uint8Type).Uint()) 221 | } 222 | doHexDump = true 223 | } 224 | } 225 | 226 | // Hexdump the entire slice as needed. 227 | if doHexDump { 228 | indent := strings.Repeat(d.cs.Indent, d.depth) 229 | str := indent + hex.Dump(buf) 230 | str = strings.Replace(str, "\n", "\n"+indent, -1) 231 | str = strings.TrimRight(str, d.cs.Indent) 232 | d.w.Write([]byte(str)) 233 | return 234 | } 235 | 236 | // Recursively call dump for each item. 237 | for i := 0; i < numEntries; i++ { 238 | d.dump(d.unpackValue(v.Index(i))) 239 | if i < (numEntries - 1) { 240 | d.w.Write(commaNewlineBytes) 241 | } else { 242 | d.w.Write(newlineBytes) 243 | } 244 | } 245 | } 246 | 247 | // dump is the main workhorse for dumping a value. It uses the passed reflect 248 | // value to figure out what kind of object we are dealing with and formats it 249 | // appropriately. It is a recursive function, however circular data structures 250 | // are detected and handled properly. 251 | func (d *dumpState) dump(v reflect.Value) { 252 | // Handle invalid reflect values immediately. 253 | kind := v.Kind() 254 | if kind == reflect.Invalid { 255 | d.w.Write(invalidAngleBytes) 256 | return 257 | } 258 | 259 | // Handle pointers specially. 260 | if kind == reflect.Ptr { 261 | d.indent() 262 | d.dumpPtr(v) 263 | return 264 | } 265 | 266 | // Print type information unless already handled elsewhere. 267 | if !d.ignoreNextType { 268 | d.indent() 269 | d.w.Write(openParenBytes) 270 | d.w.Write([]byte(v.Type().String())) 271 | d.w.Write(closeParenBytes) 272 | d.w.Write(spaceBytes) 273 | } 274 | d.ignoreNextType = false 275 | 276 | // Display length and capacity if the built-in len and cap functions 277 | // work with the value's kind and the len/cap itself is non-zero. 278 | valueLen, valueCap := 0, 0 279 | switch v.Kind() { 280 | case reflect.Array, reflect.Slice, reflect.Chan: 281 | valueLen, valueCap = v.Len(), v.Cap() 282 | case reflect.Map, reflect.String: 283 | valueLen = v.Len() 284 | } 285 | if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 { 286 | d.w.Write(openParenBytes) 287 | if valueLen != 0 { 288 | d.w.Write(lenEqualsBytes) 289 | printInt(d.w, int64(valueLen), 10) 290 | } 291 | if !d.cs.DisableCapacities && valueCap != 0 { 292 | if valueLen != 0 { 293 | d.w.Write(spaceBytes) 294 | } 295 | d.w.Write(capEqualsBytes) 296 | printInt(d.w, int64(valueCap), 10) 297 | } 298 | d.w.Write(closeParenBytes) 299 | d.w.Write(spaceBytes) 300 | } 301 | 302 | // Call Stringer/error interfaces if they exist and the handle methods flag 303 | // is enabled 304 | if !d.cs.DisableMethods { 305 | if (kind != reflect.Invalid) && (kind != reflect.Interface) { 306 | if handled := handleMethods(d.cs, d.w, v); handled { 307 | return 308 | } 309 | } 310 | } 311 | 312 | switch kind { 313 | case reflect.Invalid: 314 | // Do nothing. We should never get here since invalid has already 315 | // been handled above. 316 | 317 | case reflect.Bool: 318 | printBool(d.w, v.Bool()) 319 | 320 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 321 | printInt(d.w, v.Int(), 10) 322 | 323 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 324 | printUint(d.w, v.Uint(), 10) 325 | 326 | case reflect.Float32: 327 | printFloat(d.w, v.Float(), 32) 328 | 329 | case reflect.Float64: 330 | printFloat(d.w, v.Float(), 64) 331 | 332 | case reflect.Complex64: 333 | printComplex(d.w, v.Complex(), 32) 334 | 335 | case reflect.Complex128: 336 | printComplex(d.w, v.Complex(), 64) 337 | 338 | case reflect.Slice: 339 | if v.IsNil() { 340 | d.w.Write(nilAngleBytes) 341 | break 342 | } 343 | fallthrough 344 | 345 | case reflect.Array: 346 | d.w.Write(openBraceNewlineBytes) 347 | d.depth++ 348 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 349 | d.indent() 350 | d.w.Write(maxNewlineBytes) 351 | } else { 352 | d.dumpSlice(v) 353 | } 354 | d.depth-- 355 | d.indent() 356 | d.w.Write(closeBraceBytes) 357 | 358 | case reflect.String: 359 | d.w.Write([]byte(strconv.Quote(v.String()))) 360 | 361 | case reflect.Interface: 362 | // The only time we should get here is for nil interfaces due to 363 | // unpackValue calls. 364 | if v.IsNil() { 365 | d.w.Write(nilAngleBytes) 366 | } 367 | 368 | case reflect.Ptr: 369 | // Do nothing. We should never get here since pointers have already 370 | // been handled above. 371 | 372 | case reflect.Map: 373 | // nil maps should be indicated as different than empty maps 374 | if v.IsNil() { 375 | d.w.Write(nilAngleBytes) 376 | break 377 | } 378 | 379 | d.w.Write(openBraceNewlineBytes) 380 | d.depth++ 381 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 382 | d.indent() 383 | d.w.Write(maxNewlineBytes) 384 | } else { 385 | numEntries := v.Len() 386 | keys := v.MapKeys() 387 | if d.cs.SortKeys { 388 | sortValues(keys, d.cs) 389 | } 390 | for i, key := range keys { 391 | d.dump(d.unpackValue(key)) 392 | d.w.Write(colonSpaceBytes) 393 | d.ignoreNextIndent = true 394 | d.dump(d.unpackValue(v.MapIndex(key))) 395 | if i < (numEntries - 1) { 396 | d.w.Write(commaNewlineBytes) 397 | } else { 398 | d.w.Write(newlineBytes) 399 | } 400 | } 401 | } 402 | d.depth-- 403 | d.indent() 404 | d.w.Write(closeBraceBytes) 405 | 406 | case reflect.Struct: 407 | d.w.Write(openBraceNewlineBytes) 408 | d.depth++ 409 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 410 | d.indent() 411 | d.w.Write(maxNewlineBytes) 412 | } else { 413 | vt := v.Type() 414 | numFields := v.NumField() 415 | for i := 0; i < numFields; i++ { 416 | d.indent() 417 | vtf := vt.Field(i) 418 | d.w.Write([]byte(vtf.Name)) 419 | d.w.Write(colonSpaceBytes) 420 | d.ignoreNextIndent = true 421 | d.dump(d.unpackValue(v.Field(i))) 422 | if i < (numFields - 1) { 423 | d.w.Write(commaNewlineBytes) 424 | } else { 425 | d.w.Write(newlineBytes) 426 | } 427 | } 428 | } 429 | d.depth-- 430 | d.indent() 431 | d.w.Write(closeBraceBytes) 432 | 433 | case reflect.Uintptr: 434 | printHexPtr(d.w, uintptr(v.Uint())) 435 | 436 | case reflect.UnsafePointer, reflect.Chan, reflect.Func: 437 | printHexPtr(d.w, v.Pointer()) 438 | 439 | // There were not any other types at the time this code was written, but 440 | // fall back to letting the default fmt package handle it in case any new 441 | // types are added. 442 | default: 443 | if v.CanInterface() { 444 | fmt.Fprintf(d.w, "%v", v.Interface()) 445 | } else { 446 | fmt.Fprintf(d.w, "%v", v.String()) 447 | } 448 | } 449 | } 450 | 451 | // fdump is a helper function to consolidate the logic from the various public 452 | // methods which take varying writers and config states. 453 | func fdump(cs *ConfigState, w io.Writer, a ...interface{}) { 454 | for _, arg := range a { 455 | if arg == nil { 456 | w.Write(interfaceBytes) 457 | w.Write(spaceBytes) 458 | w.Write(nilAngleBytes) 459 | w.Write(newlineBytes) 460 | continue 461 | } 462 | 463 | d := dumpState{w: w, cs: cs} 464 | d.pointers = make(map[uintptr]int) 465 | d.dump(reflect.ValueOf(arg)) 466 | d.w.Write(newlineBytes) 467 | } 468 | } 469 | 470 | // Fdump formats and displays the passed arguments to io.Writer w. It formats 471 | // exactly the same as Dump. 472 | func Fdump(w io.Writer, a ...interface{}) { 473 | fdump(&Config, w, a...) 474 | } 475 | 476 | // Sdump returns a string with the passed arguments formatted exactly the same 477 | // as Dump. 478 | func Sdump(a ...interface{}) string { 479 | var buf bytes.Buffer 480 | fdump(&Config, &buf, a...) 481 | return buf.String() 482 | } 483 | 484 | /* 485 | Dump displays the passed parameters to standard out with newlines, customizable 486 | indentation, and additional debug information such as complete types and all 487 | pointer addresses used to indirect to the final value. It provides the 488 | following features over the built-in printing facilities provided by the fmt 489 | package: 490 | 491 | * Pointers are dereferenced and followed 492 | * Circular data structures are detected and handled properly 493 | * Custom Stringer/error interfaces are optionally invoked, including 494 | on unexported types 495 | * Custom types which only implement the Stringer/error interfaces via 496 | a pointer receiver are optionally invoked when passing non-pointer 497 | variables 498 | * Byte arrays and slices are dumped like the hexdump -C command which 499 | includes offsets, byte values in hex, and ASCII output 500 | 501 | The configuration options are controlled by an exported package global, 502 | spew.Config. See ConfigState for options documentation. 503 | 504 | See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to 505 | get the formatted result as a string. 506 | */ 507 | func Dump(a ...interface{}) { 508 | fdump(&Config, os.Stdout, a...) 509 | } 510 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/format.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "reflect" 23 | "strconv" 24 | "strings" 25 | ) 26 | 27 | // supportedFlags is a list of all the character flags supported by fmt package. 28 | const supportedFlags = "0-+# " 29 | 30 | // formatState implements the fmt.Formatter interface and contains information 31 | // about the state of a formatting operation. The NewFormatter function can 32 | // be used to get a new Formatter which can be used directly as arguments 33 | // in standard fmt package printing calls. 34 | type formatState struct { 35 | value interface{} 36 | fs fmt.State 37 | depth int 38 | pointers map[uintptr]int 39 | ignoreNextType bool 40 | cs *ConfigState 41 | } 42 | 43 | // buildDefaultFormat recreates the original format string without precision 44 | // and width information to pass in to fmt.Sprintf in the case of an 45 | // unrecognized type. Unless new types are added to the language, this 46 | // function won't ever be called. 47 | func (f *formatState) buildDefaultFormat() (format string) { 48 | buf := bytes.NewBuffer(percentBytes) 49 | 50 | for _, flag := range supportedFlags { 51 | if f.fs.Flag(int(flag)) { 52 | buf.WriteRune(flag) 53 | } 54 | } 55 | 56 | buf.WriteRune('v') 57 | 58 | format = buf.String() 59 | return format 60 | } 61 | 62 | // constructOrigFormat recreates the original format string including precision 63 | // and width information to pass along to the standard fmt package. This allows 64 | // automatic deferral of all format strings this package doesn't support. 65 | func (f *formatState) constructOrigFormat(verb rune) (format string) { 66 | buf := bytes.NewBuffer(percentBytes) 67 | 68 | for _, flag := range supportedFlags { 69 | if f.fs.Flag(int(flag)) { 70 | buf.WriteRune(flag) 71 | } 72 | } 73 | 74 | if width, ok := f.fs.Width(); ok { 75 | buf.WriteString(strconv.Itoa(width)) 76 | } 77 | 78 | if precision, ok := f.fs.Precision(); ok { 79 | buf.Write(precisionBytes) 80 | buf.WriteString(strconv.Itoa(precision)) 81 | } 82 | 83 | buf.WriteRune(verb) 84 | 85 | format = buf.String() 86 | return format 87 | } 88 | 89 | // unpackValue returns values inside of non-nil interfaces when possible and 90 | // ensures that types for values which have been unpacked from an interface 91 | // are displayed when the show types flag is also set. 92 | // This is useful for data types like structs, arrays, slices, and maps which 93 | // can contain varying types packed inside an interface. 94 | func (f *formatState) unpackValue(v reflect.Value) reflect.Value { 95 | if v.Kind() == reflect.Interface { 96 | f.ignoreNextType = false 97 | if !v.IsNil() { 98 | v = v.Elem() 99 | } 100 | } 101 | return v 102 | } 103 | 104 | // formatPtr handles formatting of pointers by indirecting them as necessary. 105 | func (f *formatState) formatPtr(v reflect.Value) { 106 | // Display nil if top level pointer is nil. 107 | showTypes := f.fs.Flag('#') 108 | if v.IsNil() && (!showTypes || f.ignoreNextType) { 109 | f.fs.Write(nilAngleBytes) 110 | return 111 | } 112 | 113 | // Remove pointers at or below the current depth from map used to detect 114 | // circular refs. 115 | for k, depth := range f.pointers { 116 | if depth >= f.depth { 117 | delete(f.pointers, k) 118 | } 119 | } 120 | 121 | // Keep list of all dereferenced pointers to possibly show later. 122 | pointerChain := make([]uintptr, 0) 123 | 124 | // Figure out how many levels of indirection there are by derferencing 125 | // pointers and unpacking interfaces down the chain while detecting circular 126 | // references. 127 | nilFound := false 128 | cycleFound := false 129 | indirects := 0 130 | ve := v 131 | for ve.Kind() == reflect.Ptr { 132 | if ve.IsNil() { 133 | nilFound = true 134 | break 135 | } 136 | indirects++ 137 | addr := ve.Pointer() 138 | pointerChain = append(pointerChain, addr) 139 | if pd, ok := f.pointers[addr]; ok && pd < f.depth { 140 | cycleFound = true 141 | indirects-- 142 | break 143 | } 144 | f.pointers[addr] = f.depth 145 | 146 | ve = ve.Elem() 147 | if ve.Kind() == reflect.Interface { 148 | if ve.IsNil() { 149 | nilFound = true 150 | break 151 | } 152 | ve = ve.Elem() 153 | } 154 | } 155 | 156 | // Display type or indirection level depending on flags. 157 | if showTypes && !f.ignoreNextType { 158 | f.fs.Write(openParenBytes) 159 | f.fs.Write(bytes.Repeat(asteriskBytes, indirects)) 160 | f.fs.Write([]byte(ve.Type().String())) 161 | f.fs.Write(closeParenBytes) 162 | } else { 163 | if nilFound || cycleFound { 164 | indirects += strings.Count(ve.Type().String(), "*") 165 | } 166 | f.fs.Write(openAngleBytes) 167 | f.fs.Write([]byte(strings.Repeat("*", indirects))) 168 | f.fs.Write(closeAngleBytes) 169 | } 170 | 171 | // Display pointer information depending on flags. 172 | if f.fs.Flag('+') && (len(pointerChain) > 0) { 173 | f.fs.Write(openParenBytes) 174 | for i, addr := range pointerChain { 175 | if i > 0 { 176 | f.fs.Write(pointerChainBytes) 177 | } 178 | printHexPtr(f.fs, addr) 179 | } 180 | f.fs.Write(closeParenBytes) 181 | } 182 | 183 | // Display dereferenced value. 184 | switch { 185 | case nilFound: 186 | f.fs.Write(nilAngleBytes) 187 | 188 | case cycleFound: 189 | f.fs.Write(circularShortBytes) 190 | 191 | default: 192 | f.ignoreNextType = true 193 | f.format(ve) 194 | } 195 | } 196 | 197 | // format is the main workhorse for providing the Formatter interface. It 198 | // uses the passed reflect value to figure out what kind of object we are 199 | // dealing with and formats it appropriately. It is a recursive function, 200 | // however circular data structures are detected and handled properly. 201 | func (f *formatState) format(v reflect.Value) { 202 | // Handle invalid reflect values immediately. 203 | kind := v.Kind() 204 | if kind == reflect.Invalid { 205 | f.fs.Write(invalidAngleBytes) 206 | return 207 | } 208 | 209 | // Handle pointers specially. 210 | if kind == reflect.Ptr { 211 | f.formatPtr(v) 212 | return 213 | } 214 | 215 | // Print type information unless already handled elsewhere. 216 | if !f.ignoreNextType && f.fs.Flag('#') { 217 | f.fs.Write(openParenBytes) 218 | f.fs.Write([]byte(v.Type().String())) 219 | f.fs.Write(closeParenBytes) 220 | } 221 | f.ignoreNextType = false 222 | 223 | // Call Stringer/error interfaces if they exist and the handle methods 224 | // flag is enabled. 225 | if !f.cs.DisableMethods { 226 | if (kind != reflect.Invalid) && (kind != reflect.Interface) { 227 | if handled := handleMethods(f.cs, f.fs, v); handled { 228 | return 229 | } 230 | } 231 | } 232 | 233 | switch kind { 234 | case reflect.Invalid: 235 | // Do nothing. We should never get here since invalid has already 236 | // been handled above. 237 | 238 | case reflect.Bool: 239 | printBool(f.fs, v.Bool()) 240 | 241 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 242 | printInt(f.fs, v.Int(), 10) 243 | 244 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 245 | printUint(f.fs, v.Uint(), 10) 246 | 247 | case reflect.Float32: 248 | printFloat(f.fs, v.Float(), 32) 249 | 250 | case reflect.Float64: 251 | printFloat(f.fs, v.Float(), 64) 252 | 253 | case reflect.Complex64: 254 | printComplex(f.fs, v.Complex(), 32) 255 | 256 | case reflect.Complex128: 257 | printComplex(f.fs, v.Complex(), 64) 258 | 259 | case reflect.Slice: 260 | if v.IsNil() { 261 | f.fs.Write(nilAngleBytes) 262 | break 263 | } 264 | fallthrough 265 | 266 | case reflect.Array: 267 | f.fs.Write(openBracketBytes) 268 | f.depth++ 269 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 270 | f.fs.Write(maxShortBytes) 271 | } else { 272 | numEntries := v.Len() 273 | for i := 0; i < numEntries; i++ { 274 | if i > 0 { 275 | f.fs.Write(spaceBytes) 276 | } 277 | f.ignoreNextType = true 278 | f.format(f.unpackValue(v.Index(i))) 279 | } 280 | } 281 | f.depth-- 282 | f.fs.Write(closeBracketBytes) 283 | 284 | case reflect.String: 285 | f.fs.Write([]byte(v.String())) 286 | 287 | case reflect.Interface: 288 | // The only time we should get here is for nil interfaces due to 289 | // unpackValue calls. 290 | if v.IsNil() { 291 | f.fs.Write(nilAngleBytes) 292 | } 293 | 294 | case reflect.Ptr: 295 | // Do nothing. We should never get here since pointers have already 296 | // been handled above. 297 | 298 | case reflect.Map: 299 | // nil maps should be indicated as different than empty maps 300 | if v.IsNil() { 301 | f.fs.Write(nilAngleBytes) 302 | break 303 | } 304 | 305 | f.fs.Write(openMapBytes) 306 | f.depth++ 307 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 308 | f.fs.Write(maxShortBytes) 309 | } else { 310 | keys := v.MapKeys() 311 | if f.cs.SortKeys { 312 | sortValues(keys, f.cs) 313 | } 314 | for i, key := range keys { 315 | if i > 0 { 316 | f.fs.Write(spaceBytes) 317 | } 318 | f.ignoreNextType = true 319 | f.format(f.unpackValue(key)) 320 | f.fs.Write(colonBytes) 321 | f.ignoreNextType = true 322 | f.format(f.unpackValue(v.MapIndex(key))) 323 | } 324 | } 325 | f.depth-- 326 | f.fs.Write(closeMapBytes) 327 | 328 | case reflect.Struct: 329 | numFields := v.NumField() 330 | f.fs.Write(openBraceBytes) 331 | f.depth++ 332 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 333 | f.fs.Write(maxShortBytes) 334 | } else { 335 | vt := v.Type() 336 | for i := 0; i < numFields; i++ { 337 | if i > 0 { 338 | f.fs.Write(spaceBytes) 339 | } 340 | vtf := vt.Field(i) 341 | if f.fs.Flag('+') || f.fs.Flag('#') { 342 | f.fs.Write([]byte(vtf.Name)) 343 | f.fs.Write(colonBytes) 344 | } 345 | f.format(f.unpackValue(v.Field(i))) 346 | } 347 | } 348 | f.depth-- 349 | f.fs.Write(closeBraceBytes) 350 | 351 | case reflect.Uintptr: 352 | printHexPtr(f.fs, uintptr(v.Uint())) 353 | 354 | case reflect.UnsafePointer, reflect.Chan, reflect.Func: 355 | printHexPtr(f.fs, v.Pointer()) 356 | 357 | // There were not any other types at the time this code was written, but 358 | // fall back to letting the default fmt package handle it if any get added. 359 | default: 360 | format := f.buildDefaultFormat() 361 | if v.CanInterface() { 362 | fmt.Fprintf(f.fs, format, v.Interface()) 363 | } else { 364 | fmt.Fprintf(f.fs, format, v.String()) 365 | } 366 | } 367 | } 368 | 369 | // Format satisfies the fmt.Formatter interface. See NewFormatter for usage 370 | // details. 371 | func (f *formatState) Format(fs fmt.State, verb rune) { 372 | f.fs = fs 373 | 374 | // Use standard formatting for verbs that are not v. 375 | if verb != 'v' { 376 | format := f.constructOrigFormat(verb) 377 | fmt.Fprintf(fs, format, f.value) 378 | return 379 | } 380 | 381 | if f.value == nil { 382 | if fs.Flag('#') { 383 | fs.Write(interfaceBytes) 384 | } 385 | fs.Write(nilAngleBytes) 386 | return 387 | } 388 | 389 | f.format(reflect.ValueOf(f.value)) 390 | } 391 | 392 | // newFormatter is a helper function to consolidate the logic from the various 393 | // public methods which take varying config states. 394 | func newFormatter(cs *ConfigState, v interface{}) fmt.Formatter { 395 | fs := &formatState{value: v, cs: cs} 396 | fs.pointers = make(map[uintptr]int) 397 | return fs 398 | } 399 | 400 | /* 401 | NewFormatter returns a custom formatter that satisfies the fmt.Formatter 402 | interface. As a result, it integrates cleanly with standard fmt package 403 | printing functions. The formatter is useful for inline printing of smaller data 404 | types similar to the standard %v format specifier. 405 | 406 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 407 | addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb 408 | combinations. Any other verbs such as %x and %q will be sent to the the 409 | standard fmt package for formatting. In addition, the custom formatter ignores 410 | the width and precision arguments (however they will still work on the format 411 | specifiers not handled by the custom formatter). 412 | 413 | Typically this function shouldn't be called directly. It is much easier to make 414 | use of the custom formatter by calling one of the convenience functions such as 415 | Printf, Println, or Fprintf. 416 | */ 417 | func NewFormatter(v interface{}) fmt.Formatter { 418 | return newFormatter(&Config, v) 419 | } 420 | -------------------------------------------------------------------------------- /vendor/github.com/davecgh/go-spew/spew/spew.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "fmt" 21 | "io" 22 | ) 23 | 24 | // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were 25 | // passed with a default Formatter interface returned by NewFormatter. It 26 | // returns the formatted string as a value that satisfies error. See 27 | // NewFormatter for formatting details. 28 | // 29 | // This function is shorthand for the following syntax: 30 | // 31 | // fmt.Errorf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 32 | func Errorf(format string, a ...interface{}) (err error) { 33 | return fmt.Errorf(format, convertArgs(a)...) 34 | } 35 | 36 | // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were 37 | // passed with a default Formatter interface returned by NewFormatter. It 38 | // returns the number of bytes written and any write error encountered. See 39 | // NewFormatter for formatting details. 40 | // 41 | // This function is shorthand for the following syntax: 42 | // 43 | // fmt.Fprint(w, spew.NewFormatter(a), spew.NewFormatter(b)) 44 | func Fprint(w io.Writer, a ...interface{}) (n int, err error) { 45 | return fmt.Fprint(w, convertArgs(a)...) 46 | } 47 | 48 | // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were 49 | // passed with a default Formatter interface returned by NewFormatter. It 50 | // returns the number of bytes written and any write error encountered. See 51 | // NewFormatter for formatting details. 52 | // 53 | // This function is shorthand for the following syntax: 54 | // 55 | // fmt.Fprintf(w, format, spew.NewFormatter(a), spew.NewFormatter(b)) 56 | func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { 57 | return fmt.Fprintf(w, format, convertArgs(a)...) 58 | } 59 | 60 | // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it 61 | // passed with a default Formatter interface returned by NewFormatter. See 62 | // NewFormatter for formatting details. 63 | // 64 | // This function is shorthand for the following syntax: 65 | // 66 | // fmt.Fprintln(w, spew.NewFormatter(a), spew.NewFormatter(b)) 67 | func Fprintln(w io.Writer, a ...interface{}) (n int, err error) { 68 | return fmt.Fprintln(w, convertArgs(a)...) 69 | } 70 | 71 | // Print is a wrapper for fmt.Print that treats each argument as if it were 72 | // passed with a default Formatter interface returned by NewFormatter. It 73 | // returns the number of bytes written and any write error encountered. See 74 | // NewFormatter for formatting details. 75 | // 76 | // This function is shorthand for the following syntax: 77 | // 78 | // fmt.Print(spew.NewFormatter(a), spew.NewFormatter(b)) 79 | func Print(a ...interface{}) (n int, err error) { 80 | return fmt.Print(convertArgs(a)...) 81 | } 82 | 83 | // Printf is a wrapper for fmt.Printf that treats each argument as if it were 84 | // passed with a default Formatter interface returned by NewFormatter. It 85 | // returns the number of bytes written and any write error encountered. See 86 | // NewFormatter for formatting details. 87 | // 88 | // This function is shorthand for the following syntax: 89 | // 90 | // fmt.Printf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 91 | func Printf(format string, a ...interface{}) (n int, err error) { 92 | return fmt.Printf(format, convertArgs(a)...) 93 | } 94 | 95 | // Println is a wrapper for fmt.Println that treats each argument as if it were 96 | // passed with a default Formatter interface returned by NewFormatter. It 97 | // returns the number of bytes written and any write error encountered. See 98 | // NewFormatter for formatting details. 99 | // 100 | // This function is shorthand for the following syntax: 101 | // 102 | // fmt.Println(spew.NewFormatter(a), spew.NewFormatter(b)) 103 | func Println(a ...interface{}) (n int, err error) { 104 | return fmt.Println(convertArgs(a)...) 105 | } 106 | 107 | // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were 108 | // passed with a default Formatter interface returned by NewFormatter. It 109 | // returns the resulting string. See NewFormatter for formatting details. 110 | // 111 | // This function is shorthand for the following syntax: 112 | // 113 | // fmt.Sprint(spew.NewFormatter(a), spew.NewFormatter(b)) 114 | func Sprint(a ...interface{}) string { 115 | return fmt.Sprint(convertArgs(a)...) 116 | } 117 | 118 | // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were 119 | // passed with a default Formatter interface returned by NewFormatter. It 120 | // returns the resulting string. See NewFormatter for formatting details. 121 | // 122 | // This function is shorthand for the following syntax: 123 | // 124 | // fmt.Sprintf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 125 | func Sprintf(format string, a ...interface{}) string { 126 | return fmt.Sprintf(format, convertArgs(a)...) 127 | } 128 | 129 | // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it 130 | // were passed with a default Formatter interface returned by NewFormatter. It 131 | // returns the resulting string. See NewFormatter for formatting details. 132 | // 133 | // This function is shorthand for the following syntax: 134 | // 135 | // fmt.Sprintln(spew.NewFormatter(a), spew.NewFormatter(b)) 136 | func Sprintln(a ...interface{}) string { 137 | return fmt.Sprintln(convertArgs(a)...) 138 | } 139 | 140 | // convertArgs accepts a slice of arguments and returns a slice of the same 141 | // length with each argument converted to a default spew Formatter interface. 142 | func convertArgs(args []interface{}) (formatters []interface{}) { 143 | formatters = make([]interface{}, len(args)) 144 | for index, arg := range args { 145 | formatters[index] = NewFormatter(arg) 146 | } 147 | return formatters 148 | } 149 | -------------------------------------------------------------------------------- /vendor/github.com/pmezard/go-difflib/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Patrick Mezard 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | The names of its contributors may not be used to endorse or promote 14 | products derived from this software without specific prior written 15 | permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 18 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 20 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 23 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /vendor/github.com/pmezard/go-difflib/difflib/difflib.go: -------------------------------------------------------------------------------- 1 | // Package difflib is a partial port of Python difflib module. 2 | // 3 | // It provides tools to compare sequences of strings and generate textual diffs. 4 | // 5 | // The following class and functions have been ported: 6 | // 7 | // - SequenceMatcher 8 | // 9 | // - unified_diff 10 | // 11 | // - context_diff 12 | // 13 | // Getting unified diffs was the main goal of the port. Keep in mind this code 14 | // is mostly suitable to output text differences in a human friendly way, there 15 | // are no guarantees generated diffs are consumable by patch(1). 16 | package difflib 17 | 18 | import ( 19 | "bufio" 20 | "bytes" 21 | "fmt" 22 | "io" 23 | "strings" 24 | ) 25 | 26 | func min(a, b int) int { 27 | if a < b { 28 | return a 29 | } 30 | return b 31 | } 32 | 33 | func max(a, b int) int { 34 | if a > b { 35 | return a 36 | } 37 | return b 38 | } 39 | 40 | func calculateRatio(matches, length int) float64 { 41 | if length > 0 { 42 | return 2.0 * float64(matches) / float64(length) 43 | } 44 | return 1.0 45 | } 46 | 47 | type Match struct { 48 | A int 49 | B int 50 | Size int 51 | } 52 | 53 | type OpCode struct { 54 | Tag byte 55 | I1 int 56 | I2 int 57 | J1 int 58 | J2 int 59 | } 60 | 61 | // SequenceMatcher compares sequence of strings. The basic 62 | // algorithm predates, and is a little fancier than, an algorithm 63 | // published in the late 1980's by Ratcliff and Obershelp under the 64 | // hyperbolic name "gestalt pattern matching". The basic idea is to find 65 | // the longest contiguous matching subsequence that contains no "junk" 66 | // elements (R-O doesn't address junk). The same idea is then applied 67 | // recursively to the pieces of the sequences to the left and to the right 68 | // of the matching subsequence. This does not yield minimal edit 69 | // sequences, but does tend to yield matches that "look right" to people. 70 | // 71 | // SequenceMatcher tries to compute a "human-friendly diff" between two 72 | // sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the 73 | // longest *contiguous* & junk-free matching subsequence. That's what 74 | // catches peoples' eyes. The Windows(tm) windiff has another interesting 75 | // notion, pairing up elements that appear uniquely in each sequence. 76 | // That, and the method here, appear to yield more intuitive difference 77 | // reports than does diff. This method appears to be the least vulnerable 78 | // to synching up on blocks of "junk lines", though (like blank lines in 79 | // ordinary text files, or maybe "

" lines in HTML files). That may be 80 | // because this is the only method of the 3 that has a *concept* of 81 | // "junk" . 82 | // 83 | // Timing: Basic R-O is cubic time worst case and quadratic time expected 84 | // case. SequenceMatcher is quadratic time for the worst case and has 85 | // expected-case behavior dependent in a complicated way on how many 86 | // elements the sequences have in common; best case time is linear. 87 | type SequenceMatcher struct { 88 | a []string 89 | b []string 90 | b2j map[string][]int 91 | IsJunk func(string) bool 92 | autoJunk bool 93 | bJunk map[string]struct{} 94 | matchingBlocks []Match 95 | fullBCount map[string]int 96 | bPopular map[string]struct{} 97 | opCodes []OpCode 98 | } 99 | 100 | func NewMatcher(a, b []string) *SequenceMatcher { 101 | m := SequenceMatcher{autoJunk: true} 102 | m.SetSeqs(a, b) 103 | return &m 104 | } 105 | 106 | func NewMatcherWithJunk(a, b []string, autoJunk bool, 107 | isJunk func(string) bool) *SequenceMatcher { 108 | 109 | m := SequenceMatcher{IsJunk: isJunk, autoJunk: autoJunk} 110 | m.SetSeqs(a, b) 111 | return &m 112 | } 113 | 114 | // Set two sequences to be compared. 115 | func (m *SequenceMatcher) SetSeqs(a, b []string) { 116 | m.SetSeq1(a) 117 | m.SetSeq2(b) 118 | } 119 | 120 | // Set the first sequence to be compared. The second sequence to be compared is 121 | // not changed. 122 | // 123 | // SequenceMatcher computes and caches detailed information about the second 124 | // sequence, so if you want to compare one sequence S against many sequences, 125 | // use .SetSeq2(s) once and call .SetSeq1(x) repeatedly for each of the other 126 | // sequences. 127 | // 128 | // See also SetSeqs() and SetSeq2(). 129 | func (m *SequenceMatcher) SetSeq1(a []string) { 130 | if &a == &m.a { 131 | return 132 | } 133 | m.a = a 134 | m.matchingBlocks = nil 135 | m.opCodes = nil 136 | } 137 | 138 | // Set the second sequence to be compared. The first sequence to be compared is 139 | // not changed. 140 | func (m *SequenceMatcher) SetSeq2(b []string) { 141 | if &b == &m.b { 142 | return 143 | } 144 | m.b = b 145 | m.matchingBlocks = nil 146 | m.opCodes = nil 147 | m.fullBCount = nil 148 | m.chainB() 149 | } 150 | 151 | func (m *SequenceMatcher) chainB() { 152 | // Populate line -> index mapping 153 | b2j := map[string][]int{} 154 | for i, s := range m.b { 155 | indices := b2j[s] 156 | indices = append(indices, i) 157 | b2j[s] = indices 158 | } 159 | 160 | // Purge junk elements 161 | m.bJunk = map[string]struct{}{} 162 | if m.IsJunk != nil { 163 | junk := m.bJunk 164 | for s, _ := range b2j { 165 | if m.IsJunk(s) { 166 | junk[s] = struct{}{} 167 | } 168 | } 169 | for s, _ := range junk { 170 | delete(b2j, s) 171 | } 172 | } 173 | 174 | // Purge remaining popular elements 175 | popular := map[string]struct{}{} 176 | n := len(m.b) 177 | if m.autoJunk && n >= 200 { 178 | ntest := n/100 + 1 179 | for s, indices := range b2j { 180 | if len(indices) > ntest { 181 | popular[s] = struct{}{} 182 | } 183 | } 184 | for s, _ := range popular { 185 | delete(b2j, s) 186 | } 187 | } 188 | m.bPopular = popular 189 | m.b2j = b2j 190 | } 191 | 192 | func (m *SequenceMatcher) isBJunk(s string) bool { 193 | _, ok := m.bJunk[s] 194 | return ok 195 | } 196 | 197 | // Find longest matching block in a[alo:ahi] and b[blo:bhi]. 198 | // 199 | // If IsJunk is not defined: 200 | // 201 | // Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where 202 | // alo <= i <= i+k <= ahi 203 | // blo <= j <= j+k <= bhi 204 | // and for all (i',j',k') meeting those conditions, 205 | // k >= k' 206 | // i <= i' 207 | // and if i == i', j <= j' 208 | // 209 | // In other words, of all maximal matching blocks, return one that 210 | // starts earliest in a, and of all those maximal matching blocks that 211 | // start earliest in a, return the one that starts earliest in b. 212 | // 213 | // If IsJunk is defined, first the longest matching block is 214 | // determined as above, but with the additional restriction that no 215 | // junk element appears in the block. Then that block is extended as 216 | // far as possible by matching (only) junk elements on both sides. So 217 | // the resulting block never matches on junk except as identical junk 218 | // happens to be adjacent to an "interesting" match. 219 | // 220 | // If no blocks match, return (alo, blo, 0). 221 | func (m *SequenceMatcher) findLongestMatch(alo, ahi, blo, bhi int) Match { 222 | // CAUTION: stripping common prefix or suffix would be incorrect. 223 | // E.g., 224 | // ab 225 | // acab 226 | // Longest matching block is "ab", but if common prefix is 227 | // stripped, it's "a" (tied with "b"). UNIX(tm) diff does so 228 | // strip, so ends up claiming that ab is changed to acab by 229 | // inserting "ca" in the middle. That's minimal but unintuitive: 230 | // "it's obvious" that someone inserted "ac" at the front. 231 | // Windiff ends up at the same place as diff, but by pairing up 232 | // the unique 'b's and then matching the first two 'a's. 233 | besti, bestj, bestsize := alo, blo, 0 234 | 235 | // find longest junk-free match 236 | // during an iteration of the loop, j2len[j] = length of longest 237 | // junk-free match ending with a[i-1] and b[j] 238 | j2len := map[int]int{} 239 | for i := alo; i != ahi; i++ { 240 | // look at all instances of a[i] in b; note that because 241 | // b2j has no junk keys, the loop is skipped if a[i] is junk 242 | newj2len := map[int]int{} 243 | for _, j := range m.b2j[m.a[i]] { 244 | // a[i] matches b[j] 245 | if j < blo { 246 | continue 247 | } 248 | if j >= bhi { 249 | break 250 | } 251 | k := j2len[j-1] + 1 252 | newj2len[j] = k 253 | if k > bestsize { 254 | besti, bestj, bestsize = i-k+1, j-k+1, k 255 | } 256 | } 257 | j2len = newj2len 258 | } 259 | 260 | // Extend the best by non-junk elements on each end. In particular, 261 | // "popular" non-junk elements aren't in b2j, which greatly speeds 262 | // the inner loop above, but also means "the best" match so far 263 | // doesn't contain any junk *or* popular non-junk elements. 264 | for besti > alo && bestj > blo && !m.isBJunk(m.b[bestj-1]) && 265 | m.a[besti-1] == m.b[bestj-1] { 266 | besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 267 | } 268 | for besti+bestsize < ahi && bestj+bestsize < bhi && 269 | !m.isBJunk(m.b[bestj+bestsize]) && 270 | m.a[besti+bestsize] == m.b[bestj+bestsize] { 271 | bestsize += 1 272 | } 273 | 274 | // Now that we have a wholly interesting match (albeit possibly 275 | // empty!), we may as well suck up the matching junk on each 276 | // side of it too. Can't think of a good reason not to, and it 277 | // saves post-processing the (possibly considerable) expense of 278 | // figuring out what to do with it. In the case of an empty 279 | // interesting match, this is clearly the right thing to do, 280 | // because no other kind of match is possible in the regions. 281 | for besti > alo && bestj > blo && m.isBJunk(m.b[bestj-1]) && 282 | m.a[besti-1] == m.b[bestj-1] { 283 | besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 284 | } 285 | for besti+bestsize < ahi && bestj+bestsize < bhi && 286 | m.isBJunk(m.b[bestj+bestsize]) && 287 | m.a[besti+bestsize] == m.b[bestj+bestsize] { 288 | bestsize += 1 289 | } 290 | 291 | return Match{A: besti, B: bestj, Size: bestsize} 292 | } 293 | 294 | // Return list of triples describing matching subsequences. 295 | // 296 | // Each triple is of the form (i, j, n), and means that 297 | // a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in 298 | // i and in j. It's also guaranteed that if (i, j, n) and (i', j', n') are 299 | // adjacent triples in the list, and the second is not the last triple in the 300 | // list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe 301 | // adjacent equal blocks. 302 | // 303 | // The last triple is a dummy, (len(a), len(b), 0), and is the only 304 | // triple with n==0. 305 | func (m *SequenceMatcher) GetMatchingBlocks() []Match { 306 | if m.matchingBlocks != nil { 307 | return m.matchingBlocks 308 | } 309 | 310 | var matchBlocks func(alo, ahi, blo, bhi int, matched []Match) []Match 311 | matchBlocks = func(alo, ahi, blo, bhi int, matched []Match) []Match { 312 | match := m.findLongestMatch(alo, ahi, blo, bhi) 313 | i, j, k := match.A, match.B, match.Size 314 | if match.Size > 0 { 315 | if alo < i && blo < j { 316 | matched = matchBlocks(alo, i, blo, j, matched) 317 | } 318 | matched = append(matched, match) 319 | if i+k < ahi && j+k < bhi { 320 | matched = matchBlocks(i+k, ahi, j+k, bhi, matched) 321 | } 322 | } 323 | return matched 324 | } 325 | matched := matchBlocks(0, len(m.a), 0, len(m.b), nil) 326 | 327 | // It's possible that we have adjacent equal blocks in the 328 | // matching_blocks list now. 329 | nonAdjacent := []Match{} 330 | i1, j1, k1 := 0, 0, 0 331 | for _, b := range matched { 332 | // Is this block adjacent to i1, j1, k1? 333 | i2, j2, k2 := b.A, b.B, b.Size 334 | if i1+k1 == i2 && j1+k1 == j2 { 335 | // Yes, so collapse them -- this just increases the length of 336 | // the first block by the length of the second, and the first 337 | // block so lengthened remains the block to compare against. 338 | k1 += k2 339 | } else { 340 | // Not adjacent. Remember the first block (k1==0 means it's 341 | // the dummy we started with), and make the second block the 342 | // new block to compare against. 343 | if k1 > 0 { 344 | nonAdjacent = append(nonAdjacent, Match{i1, j1, k1}) 345 | } 346 | i1, j1, k1 = i2, j2, k2 347 | } 348 | } 349 | if k1 > 0 { 350 | nonAdjacent = append(nonAdjacent, Match{i1, j1, k1}) 351 | } 352 | 353 | nonAdjacent = append(nonAdjacent, Match{len(m.a), len(m.b), 0}) 354 | m.matchingBlocks = nonAdjacent 355 | return m.matchingBlocks 356 | } 357 | 358 | // Return list of 5-tuples describing how to turn a into b. 359 | // 360 | // Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple 361 | // has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the 362 | // tuple preceding it, and likewise for j1 == the previous j2. 363 | // 364 | // The tags are characters, with these meanings: 365 | // 366 | // 'r' (replace): a[i1:i2] should be replaced by b[j1:j2] 367 | // 368 | // 'd' (delete): a[i1:i2] should be deleted, j1==j2 in this case. 369 | // 370 | // 'i' (insert): b[j1:j2] should be inserted at a[i1:i1], i1==i2 in this case. 371 | // 372 | // 'e' (equal): a[i1:i2] == b[j1:j2] 373 | func (m *SequenceMatcher) GetOpCodes() []OpCode { 374 | if m.opCodes != nil { 375 | return m.opCodes 376 | } 377 | i, j := 0, 0 378 | matching := m.GetMatchingBlocks() 379 | opCodes := make([]OpCode, 0, len(matching)) 380 | for _, m := range matching { 381 | // invariant: we've pumped out correct diffs to change 382 | // a[:i] into b[:j], and the next matching block is 383 | // a[ai:ai+size] == b[bj:bj+size]. So we need to pump 384 | // out a diff to change a[i:ai] into b[j:bj], pump out 385 | // the matching block, and move (i,j) beyond the match 386 | ai, bj, size := m.A, m.B, m.Size 387 | tag := byte(0) 388 | if i < ai && j < bj { 389 | tag = 'r' 390 | } else if i < ai { 391 | tag = 'd' 392 | } else if j < bj { 393 | tag = 'i' 394 | } 395 | if tag > 0 { 396 | opCodes = append(opCodes, OpCode{tag, i, ai, j, bj}) 397 | } 398 | i, j = ai+size, bj+size 399 | // the list of matching blocks is terminated by a 400 | // sentinel with size 0 401 | if size > 0 { 402 | opCodes = append(opCodes, OpCode{'e', ai, i, bj, j}) 403 | } 404 | } 405 | m.opCodes = opCodes 406 | return m.opCodes 407 | } 408 | 409 | // Isolate change clusters by eliminating ranges with no changes. 410 | // 411 | // Return a generator of groups with up to n lines of context. 412 | // Each group is in the same format as returned by GetOpCodes(). 413 | func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode { 414 | if n < 0 { 415 | n = 3 416 | } 417 | codes := m.GetOpCodes() 418 | if len(codes) == 0 { 419 | codes = []OpCode{OpCode{'e', 0, 1, 0, 1}} 420 | } 421 | // Fixup leading and trailing groups if they show no changes. 422 | if codes[0].Tag == 'e' { 423 | c := codes[0] 424 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 425 | codes[0] = OpCode{c.Tag, max(i1, i2-n), i2, max(j1, j2-n), j2} 426 | } 427 | if codes[len(codes)-1].Tag == 'e' { 428 | c := codes[len(codes)-1] 429 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 430 | codes[len(codes)-1] = OpCode{c.Tag, i1, min(i2, i1+n), j1, min(j2, j1+n)} 431 | } 432 | nn := n + n 433 | groups := [][]OpCode{} 434 | group := []OpCode{} 435 | for _, c := range codes { 436 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 437 | // End the current group and start a new one whenever 438 | // there is a large range with no changes. 439 | if c.Tag == 'e' && i2-i1 > nn { 440 | group = append(group, OpCode{c.Tag, i1, min(i2, i1+n), 441 | j1, min(j2, j1+n)}) 442 | groups = append(groups, group) 443 | group = []OpCode{} 444 | i1, j1 = max(i1, i2-n), max(j1, j2-n) 445 | } 446 | group = append(group, OpCode{c.Tag, i1, i2, j1, j2}) 447 | } 448 | if len(group) > 0 && !(len(group) == 1 && group[0].Tag == 'e') { 449 | groups = append(groups, group) 450 | } 451 | return groups 452 | } 453 | 454 | // Return a measure of the sequences' similarity (float in [0,1]). 455 | // 456 | // Where T is the total number of elements in both sequences, and 457 | // M is the number of matches, this is 2.0*M / T. 458 | // Note that this is 1 if the sequences are identical, and 0 if 459 | // they have nothing in common. 460 | // 461 | // .Ratio() is expensive to compute if you haven't already computed 462 | // .GetMatchingBlocks() or .GetOpCodes(), in which case you may 463 | // want to try .QuickRatio() or .RealQuickRation() first to get an 464 | // upper bound. 465 | func (m *SequenceMatcher) Ratio() float64 { 466 | matches := 0 467 | for _, m := range m.GetMatchingBlocks() { 468 | matches += m.Size 469 | } 470 | return calculateRatio(matches, len(m.a)+len(m.b)) 471 | } 472 | 473 | // Return an upper bound on ratio() relatively quickly. 474 | // 475 | // This isn't defined beyond that it is an upper bound on .Ratio(), and 476 | // is faster to compute. 477 | func (m *SequenceMatcher) QuickRatio() float64 { 478 | // viewing a and b as multisets, set matches to the cardinality 479 | // of their intersection; this counts the number of matches 480 | // without regard to order, so is clearly an upper bound 481 | if m.fullBCount == nil { 482 | m.fullBCount = map[string]int{} 483 | for _, s := range m.b { 484 | m.fullBCount[s] = m.fullBCount[s] + 1 485 | } 486 | } 487 | 488 | // avail[x] is the number of times x appears in 'b' less the 489 | // number of times we've seen it in 'a' so far ... kinda 490 | avail := map[string]int{} 491 | matches := 0 492 | for _, s := range m.a { 493 | n, ok := avail[s] 494 | if !ok { 495 | n = m.fullBCount[s] 496 | } 497 | avail[s] = n - 1 498 | if n > 0 { 499 | matches += 1 500 | } 501 | } 502 | return calculateRatio(matches, len(m.a)+len(m.b)) 503 | } 504 | 505 | // Return an upper bound on ratio() very quickly. 506 | // 507 | // This isn't defined beyond that it is an upper bound on .Ratio(), and 508 | // is faster to compute than either .Ratio() or .QuickRatio(). 509 | func (m *SequenceMatcher) RealQuickRatio() float64 { 510 | la, lb := len(m.a), len(m.b) 511 | return calculateRatio(min(la, lb), la+lb) 512 | } 513 | 514 | // Convert range to the "ed" format 515 | func formatRangeUnified(start, stop int) string { 516 | // Per the diff spec at http://www.unix.org/single_unix_specification/ 517 | beginning := start + 1 // lines start numbering with one 518 | length := stop - start 519 | if length == 1 { 520 | return fmt.Sprintf("%d", beginning) 521 | } 522 | if length == 0 { 523 | beginning -= 1 // empty ranges begin at line just before the range 524 | } 525 | return fmt.Sprintf("%d,%d", beginning, length) 526 | } 527 | 528 | // Unified diff parameters 529 | type UnifiedDiff struct { 530 | A []string // First sequence lines 531 | FromFile string // First file name 532 | FromDate string // First file time 533 | B []string // Second sequence lines 534 | ToFile string // Second file name 535 | ToDate string // Second file time 536 | Eol string // Headers end of line, defaults to LF 537 | Context int // Number of context lines 538 | } 539 | 540 | // Compare two sequences of lines; generate the delta as a unified diff. 541 | // 542 | // Unified diffs are a compact way of showing line changes and a few 543 | // lines of context. The number of context lines is set by 'n' which 544 | // defaults to three. 545 | // 546 | // By default, the diff control lines (those with ---, +++, or @@) are 547 | // created with a trailing newline. This is helpful so that inputs 548 | // created from file.readlines() result in diffs that are suitable for 549 | // file.writelines() since both the inputs and outputs have trailing 550 | // newlines. 551 | // 552 | // For inputs that do not have trailing newlines, set the lineterm 553 | // argument to "" so that the output will be uniformly newline free. 554 | // 555 | // The unidiff format normally has a header for filenames and modification 556 | // times. Any or all of these may be specified using strings for 557 | // 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. 558 | // The modification times are normally expressed in the ISO 8601 format. 559 | func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error { 560 | buf := bufio.NewWriter(writer) 561 | defer buf.Flush() 562 | wf := func(format string, args ...interface{}) error { 563 | _, err := buf.WriteString(fmt.Sprintf(format, args...)) 564 | return err 565 | } 566 | ws := func(s string) error { 567 | _, err := buf.WriteString(s) 568 | return err 569 | } 570 | 571 | if len(diff.Eol) == 0 { 572 | diff.Eol = "\n" 573 | } 574 | 575 | started := false 576 | m := NewMatcher(diff.A, diff.B) 577 | for _, g := range m.GetGroupedOpCodes(diff.Context) { 578 | if !started { 579 | started = true 580 | fromDate := "" 581 | if len(diff.FromDate) > 0 { 582 | fromDate = "\t" + diff.FromDate 583 | } 584 | toDate := "" 585 | if len(diff.ToDate) > 0 { 586 | toDate = "\t" + diff.ToDate 587 | } 588 | if diff.FromFile != "" || diff.ToFile != "" { 589 | err := wf("--- %s%s%s", diff.FromFile, fromDate, diff.Eol) 590 | if err != nil { 591 | return err 592 | } 593 | err = wf("+++ %s%s%s", diff.ToFile, toDate, diff.Eol) 594 | if err != nil { 595 | return err 596 | } 597 | } 598 | } 599 | first, last := g[0], g[len(g)-1] 600 | range1 := formatRangeUnified(first.I1, last.I2) 601 | range2 := formatRangeUnified(first.J1, last.J2) 602 | if err := wf("@@ -%s +%s @@%s", range1, range2, diff.Eol); err != nil { 603 | return err 604 | } 605 | for _, c := range g { 606 | i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2 607 | if c.Tag == 'e' { 608 | for _, line := range diff.A[i1:i2] { 609 | if err := ws(" " + line); err != nil { 610 | return err 611 | } 612 | } 613 | continue 614 | } 615 | if c.Tag == 'r' || c.Tag == 'd' { 616 | for _, line := range diff.A[i1:i2] { 617 | if err := ws("-" + line); err != nil { 618 | return err 619 | } 620 | } 621 | } 622 | if c.Tag == 'r' || c.Tag == 'i' { 623 | for _, line := range diff.B[j1:j2] { 624 | if err := ws("+" + line); err != nil { 625 | return err 626 | } 627 | } 628 | } 629 | } 630 | } 631 | return nil 632 | } 633 | 634 | // Like WriteUnifiedDiff but returns the diff a string. 635 | func GetUnifiedDiffString(diff UnifiedDiff) (string, error) { 636 | w := &bytes.Buffer{} 637 | err := WriteUnifiedDiff(w, diff) 638 | return string(w.Bytes()), err 639 | } 640 | 641 | // Convert range to the "ed" format. 642 | func formatRangeContext(start, stop int) string { 643 | // Per the diff spec at http://www.unix.org/single_unix_specification/ 644 | beginning := start + 1 // lines start numbering with one 645 | length := stop - start 646 | if length == 0 { 647 | beginning -= 1 // empty ranges begin at line just before the range 648 | } 649 | if length <= 1 { 650 | return fmt.Sprintf("%d", beginning) 651 | } 652 | return fmt.Sprintf("%d,%d", beginning, beginning+length-1) 653 | } 654 | 655 | type ContextDiff UnifiedDiff 656 | 657 | // Compare two sequences of lines; generate the delta as a context diff. 658 | // 659 | // Context diffs are a compact way of showing line changes and a few 660 | // lines of context. The number of context lines is set by diff.Context 661 | // which defaults to three. 662 | // 663 | // By default, the diff control lines (those with *** or ---) are 664 | // created with a trailing newline. 665 | // 666 | // For inputs that do not have trailing newlines, set the diff.Eol 667 | // argument to "" so that the output will be uniformly newline free. 668 | // 669 | // The context diff format normally has a header for filenames and 670 | // modification times. Any or all of these may be specified using 671 | // strings for diff.FromFile, diff.ToFile, diff.FromDate, diff.ToDate. 672 | // The modification times are normally expressed in the ISO 8601 format. 673 | // If not specified, the strings default to blanks. 674 | func WriteContextDiff(writer io.Writer, diff ContextDiff) error { 675 | buf := bufio.NewWriter(writer) 676 | defer buf.Flush() 677 | var diffErr error 678 | wf := func(format string, args ...interface{}) { 679 | _, err := buf.WriteString(fmt.Sprintf(format, args...)) 680 | if diffErr == nil && err != nil { 681 | diffErr = err 682 | } 683 | } 684 | ws := func(s string) { 685 | _, err := buf.WriteString(s) 686 | if diffErr == nil && err != nil { 687 | diffErr = err 688 | } 689 | } 690 | 691 | if len(diff.Eol) == 0 { 692 | diff.Eol = "\n" 693 | } 694 | 695 | prefix := map[byte]string{ 696 | 'i': "+ ", 697 | 'd': "- ", 698 | 'r': "! ", 699 | 'e': " ", 700 | } 701 | 702 | started := false 703 | m := NewMatcher(diff.A, diff.B) 704 | for _, g := range m.GetGroupedOpCodes(diff.Context) { 705 | if !started { 706 | started = true 707 | fromDate := "" 708 | if len(diff.FromDate) > 0 { 709 | fromDate = "\t" + diff.FromDate 710 | } 711 | toDate := "" 712 | if len(diff.ToDate) > 0 { 713 | toDate = "\t" + diff.ToDate 714 | } 715 | if diff.FromFile != "" || diff.ToFile != "" { 716 | wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol) 717 | wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol) 718 | } 719 | } 720 | 721 | first, last := g[0], g[len(g)-1] 722 | ws("***************" + diff.Eol) 723 | 724 | range1 := formatRangeContext(first.I1, last.I2) 725 | wf("*** %s ****%s", range1, diff.Eol) 726 | for _, c := range g { 727 | if c.Tag == 'r' || c.Tag == 'd' { 728 | for _, cc := range g { 729 | if cc.Tag == 'i' { 730 | continue 731 | } 732 | for _, line := range diff.A[cc.I1:cc.I2] { 733 | ws(prefix[cc.Tag] + line) 734 | } 735 | } 736 | break 737 | } 738 | } 739 | 740 | range2 := formatRangeContext(first.J1, last.J2) 741 | wf("--- %s ----%s", range2, diff.Eol) 742 | for _, c := range g { 743 | if c.Tag == 'r' || c.Tag == 'i' { 744 | for _, cc := range g { 745 | if cc.Tag == 'd' { 746 | continue 747 | } 748 | for _, line := range diff.B[cc.J1:cc.J2] { 749 | ws(prefix[cc.Tag] + line) 750 | } 751 | } 752 | break 753 | } 754 | } 755 | } 756 | return diffErr 757 | } 758 | 759 | // Like WriteContextDiff but returns the diff a string. 760 | func GetContextDiffString(diff ContextDiff) (string, error) { 761 | w := &bytes.Buffer{} 762 | err := WriteContextDiff(w, diff) 763 | return string(w.Bytes()), err 764 | } 765 | 766 | // Split a string on "\n" while preserving them. The output can be used 767 | // as input for UnifiedDiff and ContextDiff structures. 768 | func SplitLines(s string) []string { 769 | lines := strings.SplitAfter(s, "\n") 770 | lines[len(lines)-1] += "\n" 771 | return lines 772 | } 773 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2012-2018 Mat Ryer and Tyler Bunnell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/assertion_format.go: -------------------------------------------------------------------------------- 1 | /* 2 | * CODE GENERATED AUTOMATICALLY WITH github.com/stretchr/testify/_codegen 3 | * THIS FILE MUST NOT BE EDITED BY HAND 4 | */ 5 | 6 | package assert 7 | 8 | import ( 9 | http "net/http" 10 | url "net/url" 11 | time "time" 12 | ) 13 | 14 | // Conditionf uses a Comparison to assert a complex condition. 15 | func Conditionf(t TestingT, comp Comparison, msg string, args ...interface{}) bool { 16 | if h, ok := t.(tHelper); ok { 17 | h.Helper() 18 | } 19 | return Condition(t, comp, append([]interface{}{msg}, args...)...) 20 | } 21 | 22 | // Containsf asserts that the specified string, list(array, slice...) or map contains the 23 | // specified substring or element. 24 | // 25 | // assert.Containsf(t, "Hello World", "World", "error message %s", "formatted") 26 | // assert.Containsf(t, ["Hello", "World"], "World", "error message %s", "formatted") 27 | // assert.Containsf(t, {"Hello": "World"}, "Hello", "error message %s", "formatted") 28 | func Containsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool { 29 | if h, ok := t.(tHelper); ok { 30 | h.Helper() 31 | } 32 | return Contains(t, s, contains, append([]interface{}{msg}, args...)...) 33 | } 34 | 35 | // DirExistsf checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. 36 | func DirExistsf(t TestingT, path string, msg string, args ...interface{}) bool { 37 | if h, ok := t.(tHelper); ok { 38 | h.Helper() 39 | } 40 | return DirExists(t, path, append([]interface{}{msg}, args...)...) 41 | } 42 | 43 | // ElementsMatchf asserts that the specified listA(array, slice...) is equal to specified 44 | // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, 45 | // the number of appearances of each of them in both lists should match. 46 | // 47 | // assert.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") 48 | func ElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg string, args ...interface{}) bool { 49 | if h, ok := t.(tHelper); ok { 50 | h.Helper() 51 | } 52 | return ElementsMatch(t, listA, listB, append([]interface{}{msg}, args...)...) 53 | } 54 | 55 | // Emptyf asserts that the specified object is empty. I.e. nil, "", false, 0 or either 56 | // a slice or a channel with len == 0. 57 | // 58 | // assert.Emptyf(t, obj, "error message %s", "formatted") 59 | func Emptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool { 60 | if h, ok := t.(tHelper); ok { 61 | h.Helper() 62 | } 63 | return Empty(t, object, append([]interface{}{msg}, args...)...) 64 | } 65 | 66 | // Equalf asserts that two objects are equal. 67 | // 68 | // assert.Equalf(t, 123, 123, "error message %s", "formatted") 69 | // 70 | // Pointer variable equality is determined based on the equality of the 71 | // referenced values (as opposed to the memory addresses). Function equality 72 | // cannot be determined and will always fail. 73 | func Equalf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 74 | if h, ok := t.(tHelper); ok { 75 | h.Helper() 76 | } 77 | return Equal(t, expected, actual, append([]interface{}{msg}, args...)...) 78 | } 79 | 80 | // EqualErrorf asserts that a function returned an error (i.e. not `nil`) 81 | // and that it is equal to the provided error. 82 | // 83 | // actualObj, err := SomeFunction() 84 | // assert.EqualErrorf(t, err, expectedErrorString, "error message %s", "formatted") 85 | func EqualErrorf(t TestingT, theError error, errString string, msg string, args ...interface{}) bool { 86 | if h, ok := t.(tHelper); ok { 87 | h.Helper() 88 | } 89 | return EqualError(t, theError, errString, append([]interface{}{msg}, args...)...) 90 | } 91 | 92 | // EqualValuesf asserts that two objects are equal or convertable to the same types 93 | // and equal. 94 | // 95 | // assert.EqualValuesf(t, uint32(123, "error message %s", "formatted"), int32(123)) 96 | func EqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 97 | if h, ok := t.(tHelper); ok { 98 | h.Helper() 99 | } 100 | return EqualValues(t, expected, actual, append([]interface{}{msg}, args...)...) 101 | } 102 | 103 | // Errorf asserts that a function returned an error (i.e. not `nil`). 104 | // 105 | // actualObj, err := SomeFunction() 106 | // if assert.Errorf(t, err, "error message %s", "formatted") { 107 | // assert.Equal(t, expectedErrorf, err) 108 | // } 109 | func Errorf(t TestingT, err error, msg string, args ...interface{}) bool { 110 | if h, ok := t.(tHelper); ok { 111 | h.Helper() 112 | } 113 | return Error(t, err, append([]interface{}{msg}, args...)...) 114 | } 115 | 116 | // Exactlyf asserts that two objects are equal in value and type. 117 | // 118 | // assert.Exactlyf(t, int32(123, "error message %s", "formatted"), int64(123)) 119 | func Exactlyf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 120 | if h, ok := t.(tHelper); ok { 121 | h.Helper() 122 | } 123 | return Exactly(t, expected, actual, append([]interface{}{msg}, args...)...) 124 | } 125 | 126 | // Failf reports a failure through 127 | func Failf(t TestingT, failureMessage string, msg string, args ...interface{}) bool { 128 | if h, ok := t.(tHelper); ok { 129 | h.Helper() 130 | } 131 | return Fail(t, failureMessage, append([]interface{}{msg}, args...)...) 132 | } 133 | 134 | // FailNowf fails test 135 | func FailNowf(t TestingT, failureMessage string, msg string, args ...interface{}) bool { 136 | if h, ok := t.(tHelper); ok { 137 | h.Helper() 138 | } 139 | return FailNow(t, failureMessage, append([]interface{}{msg}, args...)...) 140 | } 141 | 142 | // Falsef asserts that the specified value is false. 143 | // 144 | // assert.Falsef(t, myBool, "error message %s", "formatted") 145 | func Falsef(t TestingT, value bool, msg string, args ...interface{}) bool { 146 | if h, ok := t.(tHelper); ok { 147 | h.Helper() 148 | } 149 | return False(t, value, append([]interface{}{msg}, args...)...) 150 | } 151 | 152 | // FileExistsf checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. 153 | func FileExistsf(t TestingT, path string, msg string, args ...interface{}) bool { 154 | if h, ok := t.(tHelper); ok { 155 | h.Helper() 156 | } 157 | return FileExists(t, path, append([]interface{}{msg}, args...)...) 158 | } 159 | 160 | // HTTPBodyContainsf asserts that a specified handler returns a 161 | // body that contains a string. 162 | // 163 | // assert.HTTPBodyContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") 164 | // 165 | // Returns whether the assertion was successful (true) or not (false). 166 | func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { 167 | if h, ok := t.(tHelper); ok { 168 | h.Helper() 169 | } 170 | return HTTPBodyContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...) 171 | } 172 | 173 | // HTTPBodyNotContainsf asserts that a specified handler returns a 174 | // body that does not contain a string. 175 | // 176 | // assert.HTTPBodyNotContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") 177 | // 178 | // Returns whether the assertion was successful (true) or not (false). 179 | func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { 180 | if h, ok := t.(tHelper); ok { 181 | h.Helper() 182 | } 183 | return HTTPBodyNotContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...) 184 | } 185 | 186 | // HTTPErrorf asserts that a specified handler returns an error status code. 187 | // 188 | // assert.HTTPErrorf(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} 189 | // 190 | // Returns whether the assertion was successful (true, "error message %s", "formatted") or not (false). 191 | func HTTPErrorf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { 192 | if h, ok := t.(tHelper); ok { 193 | h.Helper() 194 | } 195 | return HTTPError(t, handler, method, url, values, append([]interface{}{msg}, args...)...) 196 | } 197 | 198 | // HTTPRedirectf asserts that a specified handler returns a redirect status code. 199 | // 200 | // assert.HTTPRedirectf(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} 201 | // 202 | // Returns whether the assertion was successful (true, "error message %s", "formatted") or not (false). 203 | func HTTPRedirectf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { 204 | if h, ok := t.(tHelper); ok { 205 | h.Helper() 206 | } 207 | return HTTPRedirect(t, handler, method, url, values, append([]interface{}{msg}, args...)...) 208 | } 209 | 210 | // HTTPSuccessf asserts that a specified handler returns a success status code. 211 | // 212 | // assert.HTTPSuccessf(t, myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted") 213 | // 214 | // Returns whether the assertion was successful (true) or not (false). 215 | func HTTPSuccessf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { 216 | if h, ok := t.(tHelper); ok { 217 | h.Helper() 218 | } 219 | return HTTPSuccess(t, handler, method, url, values, append([]interface{}{msg}, args...)...) 220 | } 221 | 222 | // Implementsf asserts that an object is implemented by the specified interface. 223 | // 224 | // assert.Implementsf(t, (*MyInterface, "error message %s", "formatted")(nil), new(MyObject)) 225 | func Implementsf(t TestingT, interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { 226 | if h, ok := t.(tHelper); ok { 227 | h.Helper() 228 | } 229 | return Implements(t, interfaceObject, object, append([]interface{}{msg}, args...)...) 230 | } 231 | 232 | // InDeltaf asserts that the two numerals are within delta of each other. 233 | // 234 | // assert.InDeltaf(t, math.Pi, (22 / 7.0, "error message %s", "formatted"), 0.01) 235 | func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { 236 | if h, ok := t.(tHelper); ok { 237 | h.Helper() 238 | } 239 | return InDelta(t, expected, actual, delta, append([]interface{}{msg}, args...)...) 240 | } 241 | 242 | // InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. 243 | func InDeltaMapValuesf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { 244 | if h, ok := t.(tHelper); ok { 245 | h.Helper() 246 | } 247 | return InDeltaMapValues(t, expected, actual, delta, append([]interface{}{msg}, args...)...) 248 | } 249 | 250 | // InDeltaSlicef is the same as InDelta, except it compares two slices. 251 | func InDeltaSlicef(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { 252 | if h, ok := t.(tHelper); ok { 253 | h.Helper() 254 | } 255 | return InDeltaSlice(t, expected, actual, delta, append([]interface{}{msg}, args...)...) 256 | } 257 | 258 | // InEpsilonf asserts that expected and actual have a relative error less than epsilon 259 | func InEpsilonf(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { 260 | if h, ok := t.(tHelper); ok { 261 | h.Helper() 262 | } 263 | return InEpsilon(t, expected, actual, epsilon, append([]interface{}{msg}, args...)...) 264 | } 265 | 266 | // InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices. 267 | func InEpsilonSlicef(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { 268 | if h, ok := t.(tHelper); ok { 269 | h.Helper() 270 | } 271 | return InEpsilonSlice(t, expected, actual, epsilon, append([]interface{}{msg}, args...)...) 272 | } 273 | 274 | // IsTypef asserts that the specified objects are of the same type. 275 | func IsTypef(t TestingT, expectedType interface{}, object interface{}, msg string, args ...interface{}) bool { 276 | if h, ok := t.(tHelper); ok { 277 | h.Helper() 278 | } 279 | return IsType(t, expectedType, object, append([]interface{}{msg}, args...)...) 280 | } 281 | 282 | // JSONEqf asserts that two JSON strings are equivalent. 283 | // 284 | // assert.JSONEqf(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted") 285 | func JSONEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { 286 | if h, ok := t.(tHelper); ok { 287 | h.Helper() 288 | } 289 | return JSONEq(t, expected, actual, append([]interface{}{msg}, args...)...) 290 | } 291 | 292 | // Lenf asserts that the specified object has specific length. 293 | // Lenf also fails if the object has a type that len() not accept. 294 | // 295 | // assert.Lenf(t, mySlice, 3, "error message %s", "formatted") 296 | func Lenf(t TestingT, object interface{}, length int, msg string, args ...interface{}) bool { 297 | if h, ok := t.(tHelper); ok { 298 | h.Helper() 299 | } 300 | return Len(t, object, length, append([]interface{}{msg}, args...)...) 301 | } 302 | 303 | // Nilf asserts that the specified object is nil. 304 | // 305 | // assert.Nilf(t, err, "error message %s", "formatted") 306 | func Nilf(t TestingT, object interface{}, msg string, args ...interface{}) bool { 307 | if h, ok := t.(tHelper); ok { 308 | h.Helper() 309 | } 310 | return Nil(t, object, append([]interface{}{msg}, args...)...) 311 | } 312 | 313 | // NoErrorf asserts that a function returned no error (i.e. `nil`). 314 | // 315 | // actualObj, err := SomeFunction() 316 | // if assert.NoErrorf(t, err, "error message %s", "formatted") { 317 | // assert.Equal(t, expectedObj, actualObj) 318 | // } 319 | func NoErrorf(t TestingT, err error, msg string, args ...interface{}) bool { 320 | if h, ok := t.(tHelper); ok { 321 | h.Helper() 322 | } 323 | return NoError(t, err, append([]interface{}{msg}, args...)...) 324 | } 325 | 326 | // NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the 327 | // specified substring or element. 328 | // 329 | // assert.NotContainsf(t, "Hello World", "Earth", "error message %s", "formatted") 330 | // assert.NotContainsf(t, ["Hello", "World"], "Earth", "error message %s", "formatted") 331 | // assert.NotContainsf(t, {"Hello": "World"}, "Earth", "error message %s", "formatted") 332 | func NotContainsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool { 333 | if h, ok := t.(tHelper); ok { 334 | h.Helper() 335 | } 336 | return NotContains(t, s, contains, append([]interface{}{msg}, args...)...) 337 | } 338 | 339 | // NotEmptyf asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either 340 | // a slice or a channel with len == 0. 341 | // 342 | // if assert.NotEmptyf(t, obj, "error message %s", "formatted") { 343 | // assert.Equal(t, "two", obj[1]) 344 | // } 345 | func NotEmptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool { 346 | if h, ok := t.(tHelper); ok { 347 | h.Helper() 348 | } 349 | return NotEmpty(t, object, append([]interface{}{msg}, args...)...) 350 | } 351 | 352 | // NotEqualf asserts that the specified values are NOT equal. 353 | // 354 | // assert.NotEqualf(t, obj1, obj2, "error message %s", "formatted") 355 | // 356 | // Pointer variable equality is determined based on the equality of the 357 | // referenced values (as opposed to the memory addresses). 358 | func NotEqualf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 359 | if h, ok := t.(tHelper); ok { 360 | h.Helper() 361 | } 362 | return NotEqual(t, expected, actual, append([]interface{}{msg}, args...)...) 363 | } 364 | 365 | // NotNilf asserts that the specified object is not nil. 366 | // 367 | // assert.NotNilf(t, err, "error message %s", "formatted") 368 | func NotNilf(t TestingT, object interface{}, msg string, args ...interface{}) bool { 369 | if h, ok := t.(tHelper); ok { 370 | h.Helper() 371 | } 372 | return NotNil(t, object, append([]interface{}{msg}, args...)...) 373 | } 374 | 375 | // NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic. 376 | // 377 | // assert.NotPanicsf(t, func(){ RemainCalm() }, "error message %s", "formatted") 378 | func NotPanicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool { 379 | if h, ok := t.(tHelper); ok { 380 | h.Helper() 381 | } 382 | return NotPanics(t, f, append([]interface{}{msg}, args...)...) 383 | } 384 | 385 | // NotRegexpf asserts that a specified regexp does not match a string. 386 | // 387 | // assert.NotRegexpf(t, regexp.MustCompile("starts", "error message %s", "formatted"), "it's starting") 388 | // assert.NotRegexpf(t, "^start", "it's not starting", "error message %s", "formatted") 389 | func NotRegexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool { 390 | if h, ok := t.(tHelper); ok { 391 | h.Helper() 392 | } 393 | return NotRegexp(t, rx, str, append([]interface{}{msg}, args...)...) 394 | } 395 | 396 | // NotSubsetf asserts that the specified list(array, slice...) contains not all 397 | // elements given in the specified subset(array, slice...). 398 | // 399 | // assert.NotSubsetf(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]", "error message %s", "formatted") 400 | func NotSubsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool { 401 | if h, ok := t.(tHelper); ok { 402 | h.Helper() 403 | } 404 | return NotSubset(t, list, subset, append([]interface{}{msg}, args...)...) 405 | } 406 | 407 | // NotZerof asserts that i is not the zero value for its type. 408 | func NotZerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { 409 | if h, ok := t.(tHelper); ok { 410 | h.Helper() 411 | } 412 | return NotZero(t, i, append([]interface{}{msg}, args...)...) 413 | } 414 | 415 | // Panicsf asserts that the code inside the specified PanicTestFunc panics. 416 | // 417 | // assert.Panicsf(t, func(){ GoCrazy() }, "error message %s", "formatted") 418 | func Panicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool { 419 | if h, ok := t.(tHelper); ok { 420 | h.Helper() 421 | } 422 | return Panics(t, f, append([]interface{}{msg}, args...)...) 423 | } 424 | 425 | // PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that 426 | // the recovered panic value equals the expected panic value. 427 | // 428 | // assert.PanicsWithValuef(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted") 429 | func PanicsWithValuef(t TestingT, expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool { 430 | if h, ok := t.(tHelper); ok { 431 | h.Helper() 432 | } 433 | return PanicsWithValue(t, expected, f, append([]interface{}{msg}, args...)...) 434 | } 435 | 436 | // Regexpf asserts that a specified regexp matches a string. 437 | // 438 | // assert.Regexpf(t, regexp.MustCompile("start", "error message %s", "formatted"), "it's starting") 439 | // assert.Regexpf(t, "start...$", "it's not starting", "error message %s", "formatted") 440 | func Regexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool { 441 | if h, ok := t.(tHelper); ok { 442 | h.Helper() 443 | } 444 | return Regexp(t, rx, str, append([]interface{}{msg}, args...)...) 445 | } 446 | 447 | // Subsetf asserts that the specified list(array, slice...) contains all 448 | // elements given in the specified subset(array, slice...). 449 | // 450 | // assert.Subsetf(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]", "error message %s", "formatted") 451 | func Subsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool { 452 | if h, ok := t.(tHelper); ok { 453 | h.Helper() 454 | } 455 | return Subset(t, list, subset, append([]interface{}{msg}, args...)...) 456 | } 457 | 458 | // Truef asserts that the specified value is true. 459 | // 460 | // assert.Truef(t, myBool, "error message %s", "formatted") 461 | func Truef(t TestingT, value bool, msg string, args ...interface{}) bool { 462 | if h, ok := t.(tHelper); ok { 463 | h.Helper() 464 | } 465 | return True(t, value, append([]interface{}{msg}, args...)...) 466 | } 467 | 468 | // WithinDurationf asserts that the two times are within duration delta of each other. 469 | // 470 | // assert.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") 471 | func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...interface{}) bool { 472 | if h, ok := t.(tHelper); ok { 473 | h.Helper() 474 | } 475 | return WithinDuration(t, expected, actual, delta, append([]interface{}{msg}, args...)...) 476 | } 477 | 478 | // Zerof asserts that i is the zero value for its type. 479 | func Zerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { 480 | if h, ok := t.(tHelper); ok { 481 | h.Helper() 482 | } 483 | return Zero(t, i, append([]interface{}{msg}, args...)...) 484 | } 485 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl: -------------------------------------------------------------------------------- 1 | {{.CommentFormat}} 2 | func {{.DocInfo.Name}}f(t TestingT, {{.ParamsFormat}}) bool { 3 | if h, ok := t.(tHelper); ok { h.Helper() } 4 | return {{.DocInfo.Name}}(t, {{.ForwardedParamsFormat}}) 5 | } 6 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/assertion_forward.go: -------------------------------------------------------------------------------- 1 | /* 2 | * CODE GENERATED AUTOMATICALLY WITH github.com/stretchr/testify/_codegen 3 | * THIS FILE MUST NOT BE EDITED BY HAND 4 | */ 5 | 6 | package assert 7 | 8 | import ( 9 | http "net/http" 10 | url "net/url" 11 | time "time" 12 | ) 13 | 14 | // Condition uses a Comparison to assert a complex condition. 15 | func (a *Assertions) Condition(comp Comparison, msgAndArgs ...interface{}) bool { 16 | if h, ok := a.t.(tHelper); ok { 17 | h.Helper() 18 | } 19 | return Condition(a.t, comp, msgAndArgs...) 20 | } 21 | 22 | // Conditionf uses a Comparison to assert a complex condition. 23 | func (a *Assertions) Conditionf(comp Comparison, msg string, args ...interface{}) bool { 24 | if h, ok := a.t.(tHelper); ok { 25 | h.Helper() 26 | } 27 | return Conditionf(a.t, comp, msg, args...) 28 | } 29 | 30 | // Contains asserts that the specified string, list(array, slice...) or map contains the 31 | // specified substring or element. 32 | // 33 | // a.Contains("Hello World", "World") 34 | // a.Contains(["Hello", "World"], "World") 35 | // a.Contains({"Hello": "World"}, "Hello") 36 | func (a *Assertions) Contains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool { 37 | if h, ok := a.t.(tHelper); ok { 38 | h.Helper() 39 | } 40 | return Contains(a.t, s, contains, msgAndArgs...) 41 | } 42 | 43 | // Containsf asserts that the specified string, list(array, slice...) or map contains the 44 | // specified substring or element. 45 | // 46 | // a.Containsf("Hello World", "World", "error message %s", "formatted") 47 | // a.Containsf(["Hello", "World"], "World", "error message %s", "formatted") 48 | // a.Containsf({"Hello": "World"}, "Hello", "error message %s", "formatted") 49 | func (a *Assertions) Containsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool { 50 | if h, ok := a.t.(tHelper); ok { 51 | h.Helper() 52 | } 53 | return Containsf(a.t, s, contains, msg, args...) 54 | } 55 | 56 | // DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. 57 | func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) bool { 58 | if h, ok := a.t.(tHelper); ok { 59 | h.Helper() 60 | } 61 | return DirExists(a.t, path, msgAndArgs...) 62 | } 63 | 64 | // DirExistsf checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. 65 | func (a *Assertions) DirExistsf(path string, msg string, args ...interface{}) bool { 66 | if h, ok := a.t.(tHelper); ok { 67 | h.Helper() 68 | } 69 | return DirExistsf(a.t, path, msg, args...) 70 | } 71 | 72 | // ElementsMatch asserts that the specified listA(array, slice...) is equal to specified 73 | // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, 74 | // the number of appearances of each of them in both lists should match. 75 | // 76 | // a.ElementsMatch([1, 3, 2, 3], [1, 3, 3, 2]) 77 | func (a *Assertions) ElementsMatch(listA interface{}, listB interface{}, msgAndArgs ...interface{}) bool { 78 | if h, ok := a.t.(tHelper); ok { 79 | h.Helper() 80 | } 81 | return ElementsMatch(a.t, listA, listB, msgAndArgs...) 82 | } 83 | 84 | // ElementsMatchf asserts that the specified listA(array, slice...) is equal to specified 85 | // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, 86 | // the number of appearances of each of them in both lists should match. 87 | // 88 | // a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") 89 | func (a *Assertions) ElementsMatchf(listA interface{}, listB interface{}, msg string, args ...interface{}) bool { 90 | if h, ok := a.t.(tHelper); ok { 91 | h.Helper() 92 | } 93 | return ElementsMatchf(a.t, listA, listB, msg, args...) 94 | } 95 | 96 | // Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either 97 | // a slice or a channel with len == 0. 98 | // 99 | // a.Empty(obj) 100 | func (a *Assertions) Empty(object interface{}, msgAndArgs ...interface{}) bool { 101 | if h, ok := a.t.(tHelper); ok { 102 | h.Helper() 103 | } 104 | return Empty(a.t, object, msgAndArgs...) 105 | } 106 | 107 | // Emptyf asserts that the specified object is empty. I.e. nil, "", false, 0 or either 108 | // a slice or a channel with len == 0. 109 | // 110 | // a.Emptyf(obj, "error message %s", "formatted") 111 | func (a *Assertions) Emptyf(object interface{}, msg string, args ...interface{}) bool { 112 | if h, ok := a.t.(tHelper); ok { 113 | h.Helper() 114 | } 115 | return Emptyf(a.t, object, msg, args...) 116 | } 117 | 118 | // Equal asserts that two objects are equal. 119 | // 120 | // a.Equal(123, 123) 121 | // 122 | // Pointer variable equality is determined based on the equality of the 123 | // referenced values (as opposed to the memory addresses). Function equality 124 | // cannot be determined and will always fail. 125 | func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { 126 | if h, ok := a.t.(tHelper); ok { 127 | h.Helper() 128 | } 129 | return Equal(a.t, expected, actual, msgAndArgs...) 130 | } 131 | 132 | // EqualError asserts that a function returned an error (i.e. not `nil`) 133 | // and that it is equal to the provided error. 134 | // 135 | // actualObj, err := SomeFunction() 136 | // a.EqualError(err, expectedErrorString) 137 | func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...interface{}) bool { 138 | if h, ok := a.t.(tHelper); ok { 139 | h.Helper() 140 | } 141 | return EqualError(a.t, theError, errString, msgAndArgs...) 142 | } 143 | 144 | // EqualErrorf asserts that a function returned an error (i.e. not `nil`) 145 | // and that it is equal to the provided error. 146 | // 147 | // actualObj, err := SomeFunction() 148 | // a.EqualErrorf(err, expectedErrorString, "error message %s", "formatted") 149 | func (a *Assertions) EqualErrorf(theError error, errString string, msg string, args ...interface{}) bool { 150 | if h, ok := a.t.(tHelper); ok { 151 | h.Helper() 152 | } 153 | return EqualErrorf(a.t, theError, errString, msg, args...) 154 | } 155 | 156 | // EqualValues asserts that two objects are equal or convertable to the same types 157 | // and equal. 158 | // 159 | // a.EqualValues(uint32(123), int32(123)) 160 | func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { 161 | if h, ok := a.t.(tHelper); ok { 162 | h.Helper() 163 | } 164 | return EqualValues(a.t, expected, actual, msgAndArgs...) 165 | } 166 | 167 | // EqualValuesf asserts that two objects are equal or convertable to the same types 168 | // and equal. 169 | // 170 | // a.EqualValuesf(uint32(123, "error message %s", "formatted"), int32(123)) 171 | func (a *Assertions) EqualValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 172 | if h, ok := a.t.(tHelper); ok { 173 | h.Helper() 174 | } 175 | return EqualValuesf(a.t, expected, actual, msg, args...) 176 | } 177 | 178 | // Equalf asserts that two objects are equal. 179 | // 180 | // a.Equalf(123, 123, "error message %s", "formatted") 181 | // 182 | // Pointer variable equality is determined based on the equality of the 183 | // referenced values (as opposed to the memory addresses). Function equality 184 | // cannot be determined and will always fail. 185 | func (a *Assertions) Equalf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 186 | if h, ok := a.t.(tHelper); ok { 187 | h.Helper() 188 | } 189 | return Equalf(a.t, expected, actual, msg, args...) 190 | } 191 | 192 | // Error asserts that a function returned an error (i.e. not `nil`). 193 | // 194 | // actualObj, err := SomeFunction() 195 | // if a.Error(err) { 196 | // assert.Equal(t, expectedError, err) 197 | // } 198 | func (a *Assertions) Error(err error, msgAndArgs ...interface{}) bool { 199 | if h, ok := a.t.(tHelper); ok { 200 | h.Helper() 201 | } 202 | return Error(a.t, err, msgAndArgs...) 203 | } 204 | 205 | // Errorf asserts that a function returned an error (i.e. not `nil`). 206 | // 207 | // actualObj, err := SomeFunction() 208 | // if a.Errorf(err, "error message %s", "formatted") { 209 | // assert.Equal(t, expectedErrorf, err) 210 | // } 211 | func (a *Assertions) Errorf(err error, msg string, args ...interface{}) bool { 212 | if h, ok := a.t.(tHelper); ok { 213 | h.Helper() 214 | } 215 | return Errorf(a.t, err, msg, args...) 216 | } 217 | 218 | // Exactly asserts that two objects are equal in value and type. 219 | // 220 | // a.Exactly(int32(123), int64(123)) 221 | func (a *Assertions) Exactly(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { 222 | if h, ok := a.t.(tHelper); ok { 223 | h.Helper() 224 | } 225 | return Exactly(a.t, expected, actual, msgAndArgs...) 226 | } 227 | 228 | // Exactlyf asserts that two objects are equal in value and type. 229 | // 230 | // a.Exactlyf(int32(123, "error message %s", "formatted"), int64(123)) 231 | func (a *Assertions) Exactlyf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 232 | if h, ok := a.t.(tHelper); ok { 233 | h.Helper() 234 | } 235 | return Exactlyf(a.t, expected, actual, msg, args...) 236 | } 237 | 238 | // Fail reports a failure through 239 | func (a *Assertions) Fail(failureMessage string, msgAndArgs ...interface{}) bool { 240 | if h, ok := a.t.(tHelper); ok { 241 | h.Helper() 242 | } 243 | return Fail(a.t, failureMessage, msgAndArgs...) 244 | } 245 | 246 | // FailNow fails test 247 | func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...interface{}) bool { 248 | if h, ok := a.t.(tHelper); ok { 249 | h.Helper() 250 | } 251 | return FailNow(a.t, failureMessage, msgAndArgs...) 252 | } 253 | 254 | // FailNowf fails test 255 | func (a *Assertions) FailNowf(failureMessage string, msg string, args ...interface{}) bool { 256 | if h, ok := a.t.(tHelper); ok { 257 | h.Helper() 258 | } 259 | return FailNowf(a.t, failureMessage, msg, args...) 260 | } 261 | 262 | // Failf reports a failure through 263 | func (a *Assertions) Failf(failureMessage string, msg string, args ...interface{}) bool { 264 | if h, ok := a.t.(tHelper); ok { 265 | h.Helper() 266 | } 267 | return Failf(a.t, failureMessage, msg, args...) 268 | } 269 | 270 | // False asserts that the specified value is false. 271 | // 272 | // a.False(myBool) 273 | func (a *Assertions) False(value bool, msgAndArgs ...interface{}) bool { 274 | if h, ok := a.t.(tHelper); ok { 275 | h.Helper() 276 | } 277 | return False(a.t, value, msgAndArgs...) 278 | } 279 | 280 | // Falsef asserts that the specified value is false. 281 | // 282 | // a.Falsef(myBool, "error message %s", "formatted") 283 | func (a *Assertions) Falsef(value bool, msg string, args ...interface{}) bool { 284 | if h, ok := a.t.(tHelper); ok { 285 | h.Helper() 286 | } 287 | return Falsef(a.t, value, msg, args...) 288 | } 289 | 290 | // FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. 291 | func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) bool { 292 | if h, ok := a.t.(tHelper); ok { 293 | h.Helper() 294 | } 295 | return FileExists(a.t, path, msgAndArgs...) 296 | } 297 | 298 | // FileExistsf checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. 299 | func (a *Assertions) FileExistsf(path string, msg string, args ...interface{}) bool { 300 | if h, ok := a.t.(tHelper); ok { 301 | h.Helper() 302 | } 303 | return FileExistsf(a.t, path, msg, args...) 304 | } 305 | 306 | // HTTPBodyContains asserts that a specified handler returns a 307 | // body that contains a string. 308 | // 309 | // a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 310 | // 311 | // Returns whether the assertion was successful (true) or not (false). 312 | func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { 313 | if h, ok := a.t.(tHelper); ok { 314 | h.Helper() 315 | } 316 | return HTTPBodyContains(a.t, handler, method, url, values, str, msgAndArgs...) 317 | } 318 | 319 | // HTTPBodyContainsf asserts that a specified handler returns a 320 | // body that contains a string. 321 | // 322 | // a.HTTPBodyContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") 323 | // 324 | // Returns whether the assertion was successful (true) or not (false). 325 | func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { 326 | if h, ok := a.t.(tHelper); ok { 327 | h.Helper() 328 | } 329 | return HTTPBodyContainsf(a.t, handler, method, url, values, str, msg, args...) 330 | } 331 | 332 | // HTTPBodyNotContains asserts that a specified handler returns a 333 | // body that does not contain a string. 334 | // 335 | // a.HTTPBodyNotContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 336 | // 337 | // Returns whether the assertion was successful (true) or not (false). 338 | func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { 339 | if h, ok := a.t.(tHelper); ok { 340 | h.Helper() 341 | } 342 | return HTTPBodyNotContains(a.t, handler, method, url, values, str, msgAndArgs...) 343 | } 344 | 345 | // HTTPBodyNotContainsf asserts that a specified handler returns a 346 | // body that does not contain a string. 347 | // 348 | // a.HTTPBodyNotContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") 349 | // 350 | // Returns whether the assertion was successful (true) or not (false). 351 | func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { 352 | if h, ok := a.t.(tHelper); ok { 353 | h.Helper() 354 | } 355 | return HTTPBodyNotContainsf(a.t, handler, method, url, values, str, msg, args...) 356 | } 357 | 358 | // HTTPError asserts that a specified handler returns an error status code. 359 | // 360 | // a.HTTPError(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} 361 | // 362 | // Returns whether the assertion was successful (true) or not (false). 363 | func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { 364 | if h, ok := a.t.(tHelper); ok { 365 | h.Helper() 366 | } 367 | return HTTPError(a.t, handler, method, url, values, msgAndArgs...) 368 | } 369 | 370 | // HTTPErrorf asserts that a specified handler returns an error status code. 371 | // 372 | // a.HTTPErrorf(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} 373 | // 374 | // Returns whether the assertion was successful (true, "error message %s", "formatted") or not (false). 375 | func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { 376 | if h, ok := a.t.(tHelper); ok { 377 | h.Helper() 378 | } 379 | return HTTPErrorf(a.t, handler, method, url, values, msg, args...) 380 | } 381 | 382 | // HTTPRedirect asserts that a specified handler returns a redirect status code. 383 | // 384 | // a.HTTPRedirect(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} 385 | // 386 | // Returns whether the assertion was successful (true) or not (false). 387 | func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { 388 | if h, ok := a.t.(tHelper); ok { 389 | h.Helper() 390 | } 391 | return HTTPRedirect(a.t, handler, method, url, values, msgAndArgs...) 392 | } 393 | 394 | // HTTPRedirectf asserts that a specified handler returns a redirect status code. 395 | // 396 | // a.HTTPRedirectf(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} 397 | // 398 | // Returns whether the assertion was successful (true, "error message %s", "formatted") or not (false). 399 | func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { 400 | if h, ok := a.t.(tHelper); ok { 401 | h.Helper() 402 | } 403 | return HTTPRedirectf(a.t, handler, method, url, values, msg, args...) 404 | } 405 | 406 | // HTTPSuccess asserts that a specified handler returns a success status code. 407 | // 408 | // a.HTTPSuccess(myHandler, "POST", "http://www.google.com", nil) 409 | // 410 | // Returns whether the assertion was successful (true) or not (false). 411 | func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { 412 | if h, ok := a.t.(tHelper); ok { 413 | h.Helper() 414 | } 415 | return HTTPSuccess(a.t, handler, method, url, values, msgAndArgs...) 416 | } 417 | 418 | // HTTPSuccessf asserts that a specified handler returns a success status code. 419 | // 420 | // a.HTTPSuccessf(myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted") 421 | // 422 | // Returns whether the assertion was successful (true) or not (false). 423 | func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { 424 | if h, ok := a.t.(tHelper); ok { 425 | h.Helper() 426 | } 427 | return HTTPSuccessf(a.t, handler, method, url, values, msg, args...) 428 | } 429 | 430 | // Implements asserts that an object is implemented by the specified interface. 431 | // 432 | // a.Implements((*MyInterface)(nil), new(MyObject)) 433 | func (a *Assertions) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool { 434 | if h, ok := a.t.(tHelper); ok { 435 | h.Helper() 436 | } 437 | return Implements(a.t, interfaceObject, object, msgAndArgs...) 438 | } 439 | 440 | // Implementsf asserts that an object is implemented by the specified interface. 441 | // 442 | // a.Implementsf((*MyInterface, "error message %s", "formatted")(nil), new(MyObject)) 443 | func (a *Assertions) Implementsf(interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { 444 | if h, ok := a.t.(tHelper); ok { 445 | h.Helper() 446 | } 447 | return Implementsf(a.t, interfaceObject, object, msg, args...) 448 | } 449 | 450 | // InDelta asserts that the two numerals are within delta of each other. 451 | // 452 | // a.InDelta(math.Pi, (22 / 7.0), 0.01) 453 | func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { 454 | if h, ok := a.t.(tHelper); ok { 455 | h.Helper() 456 | } 457 | return InDelta(a.t, expected, actual, delta, msgAndArgs...) 458 | } 459 | 460 | // InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. 461 | func (a *Assertions) InDeltaMapValues(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { 462 | if h, ok := a.t.(tHelper); ok { 463 | h.Helper() 464 | } 465 | return InDeltaMapValues(a.t, expected, actual, delta, msgAndArgs...) 466 | } 467 | 468 | // InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. 469 | func (a *Assertions) InDeltaMapValuesf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { 470 | if h, ok := a.t.(tHelper); ok { 471 | h.Helper() 472 | } 473 | return InDeltaMapValuesf(a.t, expected, actual, delta, msg, args...) 474 | } 475 | 476 | // InDeltaSlice is the same as InDelta, except it compares two slices. 477 | func (a *Assertions) InDeltaSlice(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { 478 | if h, ok := a.t.(tHelper); ok { 479 | h.Helper() 480 | } 481 | return InDeltaSlice(a.t, expected, actual, delta, msgAndArgs...) 482 | } 483 | 484 | // InDeltaSlicef is the same as InDelta, except it compares two slices. 485 | func (a *Assertions) InDeltaSlicef(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { 486 | if h, ok := a.t.(tHelper); ok { 487 | h.Helper() 488 | } 489 | return InDeltaSlicef(a.t, expected, actual, delta, msg, args...) 490 | } 491 | 492 | // InDeltaf asserts that the two numerals are within delta of each other. 493 | // 494 | // a.InDeltaf(math.Pi, (22 / 7.0, "error message %s", "formatted"), 0.01) 495 | func (a *Assertions) InDeltaf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { 496 | if h, ok := a.t.(tHelper); ok { 497 | h.Helper() 498 | } 499 | return InDeltaf(a.t, expected, actual, delta, msg, args...) 500 | } 501 | 502 | // InEpsilon asserts that expected and actual have a relative error less than epsilon 503 | func (a *Assertions) InEpsilon(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { 504 | if h, ok := a.t.(tHelper); ok { 505 | h.Helper() 506 | } 507 | return InEpsilon(a.t, expected, actual, epsilon, msgAndArgs...) 508 | } 509 | 510 | // InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices. 511 | func (a *Assertions) InEpsilonSlice(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { 512 | if h, ok := a.t.(tHelper); ok { 513 | h.Helper() 514 | } 515 | return InEpsilonSlice(a.t, expected, actual, epsilon, msgAndArgs...) 516 | } 517 | 518 | // InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices. 519 | func (a *Assertions) InEpsilonSlicef(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { 520 | if h, ok := a.t.(tHelper); ok { 521 | h.Helper() 522 | } 523 | return InEpsilonSlicef(a.t, expected, actual, epsilon, msg, args...) 524 | } 525 | 526 | // InEpsilonf asserts that expected and actual have a relative error less than epsilon 527 | func (a *Assertions) InEpsilonf(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { 528 | if h, ok := a.t.(tHelper); ok { 529 | h.Helper() 530 | } 531 | return InEpsilonf(a.t, expected, actual, epsilon, msg, args...) 532 | } 533 | 534 | // IsType asserts that the specified objects are of the same type. 535 | func (a *Assertions) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool { 536 | if h, ok := a.t.(tHelper); ok { 537 | h.Helper() 538 | } 539 | return IsType(a.t, expectedType, object, msgAndArgs...) 540 | } 541 | 542 | // IsTypef asserts that the specified objects are of the same type. 543 | func (a *Assertions) IsTypef(expectedType interface{}, object interface{}, msg string, args ...interface{}) bool { 544 | if h, ok := a.t.(tHelper); ok { 545 | h.Helper() 546 | } 547 | return IsTypef(a.t, expectedType, object, msg, args...) 548 | } 549 | 550 | // JSONEq asserts that two JSON strings are equivalent. 551 | // 552 | // a.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) 553 | func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...interface{}) bool { 554 | if h, ok := a.t.(tHelper); ok { 555 | h.Helper() 556 | } 557 | return JSONEq(a.t, expected, actual, msgAndArgs...) 558 | } 559 | 560 | // JSONEqf asserts that two JSON strings are equivalent. 561 | // 562 | // a.JSONEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted") 563 | func (a *Assertions) JSONEqf(expected string, actual string, msg string, args ...interface{}) bool { 564 | if h, ok := a.t.(tHelper); ok { 565 | h.Helper() 566 | } 567 | return JSONEqf(a.t, expected, actual, msg, args...) 568 | } 569 | 570 | // Len asserts that the specified object has specific length. 571 | // Len also fails if the object has a type that len() not accept. 572 | // 573 | // a.Len(mySlice, 3) 574 | func (a *Assertions) Len(object interface{}, length int, msgAndArgs ...interface{}) bool { 575 | if h, ok := a.t.(tHelper); ok { 576 | h.Helper() 577 | } 578 | return Len(a.t, object, length, msgAndArgs...) 579 | } 580 | 581 | // Lenf asserts that the specified object has specific length. 582 | // Lenf also fails if the object has a type that len() not accept. 583 | // 584 | // a.Lenf(mySlice, 3, "error message %s", "formatted") 585 | func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...interface{}) bool { 586 | if h, ok := a.t.(tHelper); ok { 587 | h.Helper() 588 | } 589 | return Lenf(a.t, object, length, msg, args...) 590 | } 591 | 592 | // Nil asserts that the specified object is nil. 593 | // 594 | // a.Nil(err) 595 | func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) bool { 596 | if h, ok := a.t.(tHelper); ok { 597 | h.Helper() 598 | } 599 | return Nil(a.t, object, msgAndArgs...) 600 | } 601 | 602 | // Nilf asserts that the specified object is nil. 603 | // 604 | // a.Nilf(err, "error message %s", "formatted") 605 | func (a *Assertions) Nilf(object interface{}, msg string, args ...interface{}) bool { 606 | if h, ok := a.t.(tHelper); ok { 607 | h.Helper() 608 | } 609 | return Nilf(a.t, object, msg, args...) 610 | } 611 | 612 | // NoError asserts that a function returned no error (i.e. `nil`). 613 | // 614 | // actualObj, err := SomeFunction() 615 | // if a.NoError(err) { 616 | // assert.Equal(t, expectedObj, actualObj) 617 | // } 618 | func (a *Assertions) NoError(err error, msgAndArgs ...interface{}) bool { 619 | if h, ok := a.t.(tHelper); ok { 620 | h.Helper() 621 | } 622 | return NoError(a.t, err, msgAndArgs...) 623 | } 624 | 625 | // NoErrorf asserts that a function returned no error (i.e. `nil`). 626 | // 627 | // actualObj, err := SomeFunction() 628 | // if a.NoErrorf(err, "error message %s", "formatted") { 629 | // assert.Equal(t, expectedObj, actualObj) 630 | // } 631 | func (a *Assertions) NoErrorf(err error, msg string, args ...interface{}) bool { 632 | if h, ok := a.t.(tHelper); ok { 633 | h.Helper() 634 | } 635 | return NoErrorf(a.t, err, msg, args...) 636 | } 637 | 638 | // NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the 639 | // specified substring or element. 640 | // 641 | // a.NotContains("Hello World", "Earth") 642 | // a.NotContains(["Hello", "World"], "Earth") 643 | // a.NotContains({"Hello": "World"}, "Earth") 644 | func (a *Assertions) NotContains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool { 645 | if h, ok := a.t.(tHelper); ok { 646 | h.Helper() 647 | } 648 | return NotContains(a.t, s, contains, msgAndArgs...) 649 | } 650 | 651 | // NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the 652 | // specified substring or element. 653 | // 654 | // a.NotContainsf("Hello World", "Earth", "error message %s", "formatted") 655 | // a.NotContainsf(["Hello", "World"], "Earth", "error message %s", "formatted") 656 | // a.NotContainsf({"Hello": "World"}, "Earth", "error message %s", "formatted") 657 | func (a *Assertions) NotContainsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool { 658 | if h, ok := a.t.(tHelper); ok { 659 | h.Helper() 660 | } 661 | return NotContainsf(a.t, s, contains, msg, args...) 662 | } 663 | 664 | // NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either 665 | // a slice or a channel with len == 0. 666 | // 667 | // if a.NotEmpty(obj) { 668 | // assert.Equal(t, "two", obj[1]) 669 | // } 670 | func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) bool { 671 | if h, ok := a.t.(tHelper); ok { 672 | h.Helper() 673 | } 674 | return NotEmpty(a.t, object, msgAndArgs...) 675 | } 676 | 677 | // NotEmptyf asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either 678 | // a slice or a channel with len == 0. 679 | // 680 | // if a.NotEmptyf(obj, "error message %s", "formatted") { 681 | // assert.Equal(t, "two", obj[1]) 682 | // } 683 | func (a *Assertions) NotEmptyf(object interface{}, msg string, args ...interface{}) bool { 684 | if h, ok := a.t.(tHelper); ok { 685 | h.Helper() 686 | } 687 | return NotEmptyf(a.t, object, msg, args...) 688 | } 689 | 690 | // NotEqual asserts that the specified values are NOT equal. 691 | // 692 | // a.NotEqual(obj1, obj2) 693 | // 694 | // Pointer variable equality is determined based on the equality of the 695 | // referenced values (as opposed to the memory addresses). 696 | func (a *Assertions) NotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { 697 | if h, ok := a.t.(tHelper); ok { 698 | h.Helper() 699 | } 700 | return NotEqual(a.t, expected, actual, msgAndArgs...) 701 | } 702 | 703 | // NotEqualf asserts that the specified values are NOT equal. 704 | // 705 | // a.NotEqualf(obj1, obj2, "error message %s", "formatted") 706 | // 707 | // Pointer variable equality is determined based on the equality of the 708 | // referenced values (as opposed to the memory addresses). 709 | func (a *Assertions) NotEqualf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { 710 | if h, ok := a.t.(tHelper); ok { 711 | h.Helper() 712 | } 713 | return NotEqualf(a.t, expected, actual, msg, args...) 714 | } 715 | 716 | // NotNil asserts that the specified object is not nil. 717 | // 718 | // a.NotNil(err) 719 | func (a *Assertions) NotNil(object interface{}, msgAndArgs ...interface{}) bool { 720 | if h, ok := a.t.(tHelper); ok { 721 | h.Helper() 722 | } 723 | return NotNil(a.t, object, msgAndArgs...) 724 | } 725 | 726 | // NotNilf asserts that the specified object is not nil. 727 | // 728 | // a.NotNilf(err, "error message %s", "formatted") 729 | func (a *Assertions) NotNilf(object interface{}, msg string, args ...interface{}) bool { 730 | if h, ok := a.t.(tHelper); ok { 731 | h.Helper() 732 | } 733 | return NotNilf(a.t, object, msg, args...) 734 | } 735 | 736 | // NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. 737 | // 738 | // a.NotPanics(func(){ RemainCalm() }) 739 | func (a *Assertions) NotPanics(f PanicTestFunc, msgAndArgs ...interface{}) bool { 740 | if h, ok := a.t.(tHelper); ok { 741 | h.Helper() 742 | } 743 | return NotPanics(a.t, f, msgAndArgs...) 744 | } 745 | 746 | // NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic. 747 | // 748 | // a.NotPanicsf(func(){ RemainCalm() }, "error message %s", "formatted") 749 | func (a *Assertions) NotPanicsf(f PanicTestFunc, msg string, args ...interface{}) bool { 750 | if h, ok := a.t.(tHelper); ok { 751 | h.Helper() 752 | } 753 | return NotPanicsf(a.t, f, msg, args...) 754 | } 755 | 756 | // NotRegexp asserts that a specified regexp does not match a string. 757 | // 758 | // a.NotRegexp(regexp.MustCompile("starts"), "it's starting") 759 | // a.NotRegexp("^start", "it's not starting") 760 | func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { 761 | if h, ok := a.t.(tHelper); ok { 762 | h.Helper() 763 | } 764 | return NotRegexp(a.t, rx, str, msgAndArgs...) 765 | } 766 | 767 | // NotRegexpf asserts that a specified regexp does not match a string. 768 | // 769 | // a.NotRegexpf(regexp.MustCompile("starts", "error message %s", "formatted"), "it's starting") 770 | // a.NotRegexpf("^start", "it's not starting", "error message %s", "formatted") 771 | func (a *Assertions) NotRegexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool { 772 | if h, ok := a.t.(tHelper); ok { 773 | h.Helper() 774 | } 775 | return NotRegexpf(a.t, rx, str, msg, args...) 776 | } 777 | 778 | // NotSubset asserts that the specified list(array, slice...) contains not all 779 | // elements given in the specified subset(array, slice...). 780 | // 781 | // a.NotSubset([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]") 782 | func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool { 783 | if h, ok := a.t.(tHelper); ok { 784 | h.Helper() 785 | } 786 | return NotSubset(a.t, list, subset, msgAndArgs...) 787 | } 788 | 789 | // NotSubsetf asserts that the specified list(array, slice...) contains not all 790 | // elements given in the specified subset(array, slice...). 791 | // 792 | // a.NotSubsetf([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]", "error message %s", "formatted") 793 | func (a *Assertions) NotSubsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool { 794 | if h, ok := a.t.(tHelper); ok { 795 | h.Helper() 796 | } 797 | return NotSubsetf(a.t, list, subset, msg, args...) 798 | } 799 | 800 | // NotZero asserts that i is not the zero value for its type. 801 | func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) bool { 802 | if h, ok := a.t.(tHelper); ok { 803 | h.Helper() 804 | } 805 | return NotZero(a.t, i, msgAndArgs...) 806 | } 807 | 808 | // NotZerof asserts that i is not the zero value for its type. 809 | func (a *Assertions) NotZerof(i interface{}, msg string, args ...interface{}) bool { 810 | if h, ok := a.t.(tHelper); ok { 811 | h.Helper() 812 | } 813 | return NotZerof(a.t, i, msg, args...) 814 | } 815 | 816 | // Panics asserts that the code inside the specified PanicTestFunc panics. 817 | // 818 | // a.Panics(func(){ GoCrazy() }) 819 | func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs ...interface{}) bool { 820 | if h, ok := a.t.(tHelper); ok { 821 | h.Helper() 822 | } 823 | return Panics(a.t, f, msgAndArgs...) 824 | } 825 | 826 | // PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that 827 | // the recovered panic value equals the expected panic value. 828 | // 829 | // a.PanicsWithValue("crazy error", func(){ GoCrazy() }) 830 | func (a *Assertions) PanicsWithValue(expected interface{}, f PanicTestFunc, msgAndArgs ...interface{}) bool { 831 | if h, ok := a.t.(tHelper); ok { 832 | h.Helper() 833 | } 834 | return PanicsWithValue(a.t, expected, f, msgAndArgs...) 835 | } 836 | 837 | // PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that 838 | // the recovered panic value equals the expected panic value. 839 | // 840 | // a.PanicsWithValuef("crazy error", func(){ GoCrazy() }, "error message %s", "formatted") 841 | func (a *Assertions) PanicsWithValuef(expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool { 842 | if h, ok := a.t.(tHelper); ok { 843 | h.Helper() 844 | } 845 | return PanicsWithValuef(a.t, expected, f, msg, args...) 846 | } 847 | 848 | // Panicsf asserts that the code inside the specified PanicTestFunc panics. 849 | // 850 | // a.Panicsf(func(){ GoCrazy() }, "error message %s", "formatted") 851 | func (a *Assertions) Panicsf(f PanicTestFunc, msg string, args ...interface{}) bool { 852 | if h, ok := a.t.(tHelper); ok { 853 | h.Helper() 854 | } 855 | return Panicsf(a.t, f, msg, args...) 856 | } 857 | 858 | // Regexp asserts that a specified regexp matches a string. 859 | // 860 | // a.Regexp(regexp.MustCompile("start"), "it's starting") 861 | // a.Regexp("start...$", "it's not starting") 862 | func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { 863 | if h, ok := a.t.(tHelper); ok { 864 | h.Helper() 865 | } 866 | return Regexp(a.t, rx, str, msgAndArgs...) 867 | } 868 | 869 | // Regexpf asserts that a specified regexp matches a string. 870 | // 871 | // a.Regexpf(regexp.MustCompile("start", "error message %s", "formatted"), "it's starting") 872 | // a.Regexpf("start...$", "it's not starting", "error message %s", "formatted") 873 | func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool { 874 | if h, ok := a.t.(tHelper); ok { 875 | h.Helper() 876 | } 877 | return Regexpf(a.t, rx, str, msg, args...) 878 | } 879 | 880 | // Subset asserts that the specified list(array, slice...) contains all 881 | // elements given in the specified subset(array, slice...). 882 | // 883 | // a.Subset([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]") 884 | func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool { 885 | if h, ok := a.t.(tHelper); ok { 886 | h.Helper() 887 | } 888 | return Subset(a.t, list, subset, msgAndArgs...) 889 | } 890 | 891 | // Subsetf asserts that the specified list(array, slice...) contains all 892 | // elements given in the specified subset(array, slice...). 893 | // 894 | // a.Subsetf([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]", "error message %s", "formatted") 895 | func (a *Assertions) Subsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool { 896 | if h, ok := a.t.(tHelper); ok { 897 | h.Helper() 898 | } 899 | return Subsetf(a.t, list, subset, msg, args...) 900 | } 901 | 902 | // True asserts that the specified value is true. 903 | // 904 | // a.True(myBool) 905 | func (a *Assertions) True(value bool, msgAndArgs ...interface{}) bool { 906 | if h, ok := a.t.(tHelper); ok { 907 | h.Helper() 908 | } 909 | return True(a.t, value, msgAndArgs...) 910 | } 911 | 912 | // Truef asserts that the specified value is true. 913 | // 914 | // a.Truef(myBool, "error message %s", "formatted") 915 | func (a *Assertions) Truef(value bool, msg string, args ...interface{}) bool { 916 | if h, ok := a.t.(tHelper); ok { 917 | h.Helper() 918 | } 919 | return Truef(a.t, value, msg, args...) 920 | } 921 | 922 | // WithinDuration asserts that the two times are within duration delta of each other. 923 | // 924 | // a.WithinDuration(time.Now(), time.Now(), 10*time.Second) 925 | func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool { 926 | if h, ok := a.t.(tHelper); ok { 927 | h.Helper() 928 | } 929 | return WithinDuration(a.t, expected, actual, delta, msgAndArgs...) 930 | } 931 | 932 | // WithinDurationf asserts that the two times are within duration delta of each other. 933 | // 934 | // a.WithinDurationf(time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") 935 | func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, args ...interface{}) bool { 936 | if h, ok := a.t.(tHelper); ok { 937 | h.Helper() 938 | } 939 | return WithinDurationf(a.t, expected, actual, delta, msg, args...) 940 | } 941 | 942 | // Zero asserts that i is the zero value for its type. 943 | func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) bool { 944 | if h, ok := a.t.(tHelper); ok { 945 | h.Helper() 946 | } 947 | return Zero(a.t, i, msgAndArgs...) 948 | } 949 | 950 | // Zerof asserts that i is the zero value for its type. 951 | func (a *Assertions) Zerof(i interface{}, msg string, args ...interface{}) bool { 952 | if h, ok := a.t.(tHelper); ok { 953 | h.Helper() 954 | } 955 | return Zerof(a.t, i, msg, args...) 956 | } 957 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl: -------------------------------------------------------------------------------- 1 | {{.CommentWithoutT "a"}} 2 | func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) bool { 3 | if h, ok := a.t.(tHelper); ok { h.Helper() } 4 | return {{.DocInfo.Name}}(a.t, {{.ForwardedParams}}) 5 | } 6 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/doc.go: -------------------------------------------------------------------------------- 1 | // Package assert provides a set of comprehensive testing tools for use with the normal Go testing system. 2 | // 3 | // Example Usage 4 | // 5 | // The following is a complete example using assert in a standard test function: 6 | // import ( 7 | // "testing" 8 | // "github.com/stretchr/testify/assert" 9 | // ) 10 | // 11 | // func TestSomething(t *testing.T) { 12 | // 13 | // var a string = "Hello" 14 | // var b string = "Hello" 15 | // 16 | // assert.Equal(t, a, b, "The two words should be the same.") 17 | // 18 | // } 19 | // 20 | // if you assert many times, use the format below: 21 | // 22 | // import ( 23 | // "testing" 24 | // "github.com/stretchr/testify/assert" 25 | // ) 26 | // 27 | // func TestSomething(t *testing.T) { 28 | // assert := assert.New(t) 29 | // 30 | // var a string = "Hello" 31 | // var b string = "Hello" 32 | // 33 | // assert.Equal(a, b, "The two words should be the same.") 34 | // } 35 | // 36 | // Assertions 37 | // 38 | // Assertions allow you to easily write test code, and are global funcs in the `assert` package. 39 | // All assertion functions take, as the first argument, the `*testing.T` object provided by the 40 | // testing framework. This allows the assertion funcs to write the failings and other details to 41 | // the correct place. 42 | // 43 | // Every assertion function also takes an optional string message as the final argument, 44 | // allowing custom error messages to be appended to the message the assertion method outputs. 45 | package assert 46 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/errors.go: -------------------------------------------------------------------------------- 1 | package assert 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | // AnError is an error instance useful for testing. If the code does not care 8 | // about error specifics, and only needs to return the error for example, this 9 | // error should be used to make the test code more readable. 10 | var AnError = errors.New("assert.AnError general error for testing") 11 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/forward_assertions.go: -------------------------------------------------------------------------------- 1 | package assert 2 | 3 | // Assertions provides assertion methods around the 4 | // TestingT interface. 5 | type Assertions struct { 6 | t TestingT 7 | } 8 | 9 | // New makes a new Assertions object for the specified TestingT. 10 | func New(t TestingT) *Assertions { 11 | return &Assertions{ 12 | t: t, 13 | } 14 | } 15 | 16 | //go:generate go run ../_codegen/main.go -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs 17 | -------------------------------------------------------------------------------- /vendor/github.com/stretchr/testify/assert/http_assertions.go: -------------------------------------------------------------------------------- 1 | package assert 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "net/http/httptest" 7 | "net/url" 8 | "strings" 9 | ) 10 | 11 | // httpCode is a helper that returns HTTP code of the response. It returns -1 and 12 | // an error if building a new request fails. 13 | func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) { 14 | w := httptest.NewRecorder() 15 | req, err := http.NewRequest(method, url, nil) 16 | if err != nil { 17 | return -1, err 18 | } 19 | req.URL.RawQuery = values.Encode() 20 | handler(w, req) 21 | return w.Code, nil 22 | } 23 | 24 | // HTTPSuccess asserts that a specified handler returns a success status code. 25 | // 26 | // assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil) 27 | // 28 | // Returns whether the assertion was successful (true) or not (false). 29 | func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { 30 | if h, ok := t.(tHelper); ok { 31 | h.Helper() 32 | } 33 | code, err := httpCode(handler, method, url, values) 34 | if err != nil { 35 | Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) 36 | return false 37 | } 38 | 39 | isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent 40 | if !isSuccessCode { 41 | Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code)) 42 | } 43 | 44 | return isSuccessCode 45 | } 46 | 47 | // HTTPRedirect asserts that a specified handler returns a redirect status code. 48 | // 49 | // assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} 50 | // 51 | // Returns whether the assertion was successful (true) or not (false). 52 | func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { 53 | if h, ok := t.(tHelper); ok { 54 | h.Helper() 55 | } 56 | code, err := httpCode(handler, method, url, values) 57 | if err != nil { 58 | Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) 59 | return false 60 | } 61 | 62 | isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect 63 | if !isRedirectCode { 64 | Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code)) 65 | } 66 | 67 | return isRedirectCode 68 | } 69 | 70 | // HTTPError asserts that a specified handler returns an error status code. 71 | // 72 | // assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} 73 | // 74 | // Returns whether the assertion was successful (true) or not (false). 75 | func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { 76 | if h, ok := t.(tHelper); ok { 77 | h.Helper() 78 | } 79 | code, err := httpCode(handler, method, url, values) 80 | if err != nil { 81 | Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) 82 | return false 83 | } 84 | 85 | isErrorCode := code >= http.StatusBadRequest 86 | if !isErrorCode { 87 | Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code)) 88 | } 89 | 90 | return isErrorCode 91 | } 92 | 93 | // HTTPBody is a helper that returns HTTP body of the response. It returns 94 | // empty string if building a new request fails. 95 | func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string { 96 | w := httptest.NewRecorder() 97 | req, err := http.NewRequest(method, url+"?"+values.Encode(), nil) 98 | if err != nil { 99 | return "" 100 | } 101 | handler(w, req) 102 | return w.Body.String() 103 | } 104 | 105 | // HTTPBodyContains asserts that a specified handler returns a 106 | // body that contains a string. 107 | // 108 | // assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 109 | // 110 | // Returns whether the assertion was successful (true) or not (false). 111 | func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { 112 | if h, ok := t.(tHelper); ok { 113 | h.Helper() 114 | } 115 | body := HTTPBody(handler, method, url, values) 116 | 117 | contains := strings.Contains(body, fmt.Sprint(str)) 118 | if !contains { 119 | Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) 120 | } 121 | 122 | return contains 123 | } 124 | 125 | // HTTPBodyNotContains asserts that a specified handler returns a 126 | // body that does not contain a string. 127 | // 128 | // assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") 129 | // 130 | // Returns whether the assertion was successful (true) or not (false). 131 | func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { 132 | if h, ok := t.(tHelper); ok { 133 | h.Helper() 134 | } 135 | body := HTTPBody(handler, method, url, values) 136 | 137 | contains := strings.Contains(body, fmt.Sprint(str)) 138 | if contains { 139 | Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) 140 | } 141 | 142 | return !contains 143 | } 144 | -------------------------------------------------------------------------------- /vendor/modules.txt: -------------------------------------------------------------------------------- 1 | # github.com/davecgh/go-spew v1.1.1 2 | github.com/davecgh/go-spew/spew 3 | # github.com/pmezard/go-difflib v1.0.0 4 | github.com/pmezard/go-difflib/difflib 5 | # github.com/stretchr/testify v1.3.0 6 | github.com/stretchr/testify/assert 7 | --------------------------------------------------------------------------------