diff options
author | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
---|---|---|
committer | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
commit | 107c1cdb09c575aa2f61d97f48d8587eb6bada4c (patch) | |
tree | ca7d008643efc555c388baeaf1d986e0b6b3e28c /vendor/github.com/vmihailenco | |
parent | 844b5a68d8af4791755b8f0ad293cc99f5959183 (diff) | |
download | terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.gz terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.zst terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.zip |
Upgrade to 0.12
Diffstat (limited to 'vendor/github.com/vmihailenco')
24 files changed, 3856 insertions, 0 deletions
diff --git a/vendor/github.com/vmihailenco/msgpack/.travis.yml b/vendor/github.com/vmihailenco/msgpack/.travis.yml new file mode 100644 index 0000000..0c2f74e --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/.travis.yml | |||
@@ -0,0 +1,17 @@ | |||
1 | sudo: false | ||
2 | language: go | ||
3 | |||
4 | go: | ||
5 | - 1.7.x | ||
6 | - 1.8.x | ||
7 | - 1.9.x | ||
8 | - 1.10.x | ||
9 | - 1.11.x | ||
10 | - tip | ||
11 | |||
12 | matrix: | ||
13 | allow_failures: | ||
14 | - go: tip | ||
15 | |||
16 | install: | ||
17 | - go get gopkg.in/check.v1 | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/CHANGELOG.md b/vendor/github.com/vmihailenco/msgpack/CHANGELOG.md new file mode 100644 index 0000000..9a4f38a --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/CHANGELOG.md | |||
@@ -0,0 +1,24 @@ | |||
1 | ## 3.4 | ||
2 | |||
3 | - Encode, Decode, Marshal, and Unmarshal are changed to accept single argument. EncodeMulti and DecodeMulti are added as replacement. | ||
4 | - Added EncodeInt8/16/32/64 and EncodeUint8/16/32/64. | ||
5 | - Encoder changed to preserve type of numbers instead of chosing most compact encoding. The old behavior can be achieved with Encoder.UseCompactEncoding. | ||
6 | |||
7 | ## v3.3 | ||
8 | |||
9 | - `msgpack:",inline"` tag is restored to force inlining structs. | ||
10 | |||
11 | ## v3.2 | ||
12 | |||
13 | - Decoding extension types returns pointer to the value instead of the value. Fixes #153 | ||
14 | |||
15 | ## v3 | ||
16 | |||
17 | - gopkg.in is not supported any more. Update import path to github.com/vmihailenco/msgpack. | ||
18 | - Msgpack maps are decoded into map[string]interface{} by default. | ||
19 | - EncodeSliceLen is removed in favor of EncodeArrayLen. DecodeSliceLen is removed in favor of DecodeArrayLen. | ||
20 | - Embedded structs are automatically inlined where possible. | ||
21 | - Time is encoded using extension as described in https://github.com/msgpack/msgpack/pull/209. Old format is supported as well. | ||
22 | - EncodeInt8/16/32/64 is replaced with EncodeInt. EncodeUint8/16/32/64 is replaced with EncodeUint. There should be no performance differences. | ||
23 | - DecodeInterface can now return int8/16/32 and uint8/16/32. | ||
24 | - PeekCode returns codes.Code instead of byte. | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/LICENSE b/vendor/github.com/vmihailenco/msgpack/LICENSE new file mode 100644 index 0000000..b749d07 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/LICENSE | |||
@@ -0,0 +1,25 @@ | |||
1 | Copyright (c) 2013 The github.com/vmihailenco/msgpack Authors. | ||
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 | ||
11 | copyright notice, this list of conditions and the following disclaimer | ||
12 | in the documentation and/or other materials provided with the | ||
13 | distribution. | ||
14 | |||
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
16 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
19 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
21 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/Makefile b/vendor/github.com/vmihailenco/msgpack/Makefile new file mode 100644 index 0000000..b62ae6a --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/Makefile | |||
@@ -0,0 +1,5 @@ | |||
1 | all: | ||
2 | go test ./... | ||
3 | env GOOS=linux GOARCH=386 go test ./... | ||
4 | go test ./... -short -race | ||
5 | go vet | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/README.md b/vendor/github.com/vmihailenco/msgpack/README.md new file mode 100644 index 0000000..0c75ae1 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/README.md | |||
@@ -0,0 +1,69 @@ | |||
1 | # MessagePack encoding for Golang | ||
2 | |||
3 | [](https://travis-ci.org/vmihailenco/msgpack) | ||
4 | [](https://godoc.org/github.com/vmihailenco/msgpack) | ||
5 | |||
6 | Supports: | ||
7 | - Primitives, arrays, maps, structs, time.Time and interface{}. | ||
8 | - Appengine *datastore.Key and datastore.Cursor. | ||
9 | - [CustomEncoder](https://godoc.org/github.com/vmihailenco/msgpack#example-CustomEncoder)/CustomDecoder interfaces for custom encoding. | ||
10 | - [Extensions](https://godoc.org/github.com/vmihailenco/msgpack#example-RegisterExt) to encode type information. | ||
11 | - Renaming fields via `msgpack:"my_field_name"`. | ||
12 | - Omitting individual empty fields via `msgpack:",omitempty"` tag or all [empty fields in a struct](https://godoc.org/github.com/vmihailenco/msgpack#example-Marshal--OmitEmpty). | ||
13 | - [Map keys sorting](https://godoc.org/github.com/vmihailenco/msgpack#Encoder.SortMapKeys). | ||
14 | - Encoding/decoding all [structs as arrays](https://godoc.org/github.com/vmihailenco/msgpack#Encoder.StructAsArray) or [individual structs](https://godoc.org/github.com/vmihailenco/msgpack#example-Marshal--AsArray). | ||
15 | - [Encoder.UseJSONTag](https://godoc.org/github.com/vmihailenco/msgpack#Encoder.UseJSONTag) with [Decoder.UseJSONTag](https://godoc.org/github.com/vmihailenco/msgpack#Decoder.UseJSONTag) can turn msgpack into drop-in replacement for JSON. | ||
16 | - Simple but very fast and efficient [queries](https://godoc.org/github.com/vmihailenco/msgpack#example-Decoder-Query). | ||
17 | |||
18 | API docs: https://godoc.org/github.com/vmihailenco/msgpack. | ||
19 | Examples: https://godoc.org/github.com/vmihailenco/msgpack#pkg-examples. | ||
20 | |||
21 | ## Installation | ||
22 | |||
23 | Install: | ||
24 | |||
25 | ```shell | ||
26 | go get -u github.com/vmihailenco/msgpack | ||
27 | ``` | ||
28 | |||
29 | ## Quickstart | ||
30 | |||
31 | ```go | ||
32 | func ExampleMarshal() { | ||
33 | type Item struct { | ||
34 | Foo string | ||
35 | } | ||
36 | |||
37 | b, err := msgpack.Marshal(&Item{Foo: "bar"}) | ||
38 | if err != nil { | ||
39 | panic(err) | ||
40 | } | ||
41 | |||
42 | var item Item | ||
43 | err = msgpack.Unmarshal(b, &item) | ||
44 | if err != nil { | ||
45 | panic(err) | ||
46 | } | ||
47 | fmt.Println(item.Foo) | ||
48 | // Output: bar | ||
49 | } | ||
50 | ``` | ||
51 | |||
52 | ## Benchmark | ||
53 | |||
54 | ``` | ||
55 | BenchmarkStructVmihailencoMsgpack-4 200000 12814 ns/op 2128 B/op 26 allocs/op | ||
56 | BenchmarkStructUgorjiGoMsgpack-4 100000 17678 ns/op 3616 B/op 70 allocs/op | ||
57 | BenchmarkStructUgorjiGoCodec-4 100000 19053 ns/op 7346 B/op 23 allocs/op | ||
58 | BenchmarkStructJSON-4 20000 69438 ns/op 7864 B/op 26 allocs/op | ||
59 | BenchmarkStructGOB-4 10000 104331 ns/op 14664 B/op 278 allocs/op | ||
60 | ``` | ||
61 | |||
62 | ## Howto | ||
63 | |||
64 | Please go through [examples](https://godoc.org/github.com/vmihailenco/msgpack#pkg-examples) to get an idea how to use this package. | ||
65 | |||
66 | ## See also | ||
67 | |||
68 | - [Golang PostgreSQL ORM](https://github.com/go-pg/pg) | ||
69 | - [Golang message task queue](https://github.com/go-msgqueue/msgqueue) | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/appengine.go b/vendor/github.com/vmihailenco/msgpack/appengine.go new file mode 100644 index 0000000..e8e91e5 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/appengine.go | |||
@@ -0,0 +1,64 @@ | |||
1 | // +build appengine | ||
2 | |||
3 | package msgpack | ||
4 | |||
5 | import ( | ||
6 | "reflect" | ||
7 | |||
8 | ds "google.golang.org/appengine/datastore" | ||
9 | ) | ||
10 | |||
11 | func init() { | ||
12 | Register((*ds.Key)(nil), encodeDatastoreKeyValue, decodeDatastoreKeyValue) | ||
13 | Register((*ds.Cursor)(nil), encodeDatastoreCursorValue, decodeDatastoreCursorValue) | ||
14 | } | ||
15 | |||
16 | func EncodeDatastoreKey(e *Encoder, key *ds.Key) error { | ||
17 | if key == nil { | ||
18 | return e.EncodeNil() | ||
19 | } | ||
20 | return e.EncodeString(key.Encode()) | ||
21 | } | ||
22 | |||
23 | func encodeDatastoreKeyValue(e *Encoder, v reflect.Value) error { | ||
24 | key := v.Interface().(*ds.Key) | ||
25 | return EncodeDatastoreKey(e, key) | ||
26 | } | ||
27 | |||
28 | func DecodeDatastoreKey(d *Decoder) (*ds.Key, error) { | ||
29 | v, err := d.DecodeString() | ||
30 | if err != nil { | ||
31 | return nil, err | ||
32 | } | ||
33 | if v == "" { | ||
34 | return nil, nil | ||
35 | } | ||
36 | return ds.DecodeKey(v) | ||
37 | } | ||
38 | |||
39 | func decodeDatastoreKeyValue(d *Decoder, v reflect.Value) error { | ||
40 | key, err := DecodeDatastoreKey(d) | ||
41 | if err != nil { | ||
42 | return err | ||
43 | } | ||
44 | v.Set(reflect.ValueOf(key)) | ||
45 | return nil | ||
46 | } | ||
47 | |||
48 | func encodeDatastoreCursorValue(e *Encoder, v reflect.Value) error { | ||
49 | cursor := v.Interface().(ds.Cursor) | ||
50 | return e.Encode(cursor.String()) | ||
51 | } | ||
52 | |||
53 | func decodeDatastoreCursorValue(d *Decoder, v reflect.Value) error { | ||
54 | s, err := d.DecodeString() | ||
55 | if err != nil { | ||
56 | return err | ||
57 | } | ||
58 | cursor, err := ds.DecodeCursor(s) | ||
59 | if err != nil { | ||
60 | return err | ||
61 | } | ||
62 | v.Set(reflect.ValueOf(cursor)) | ||
63 | return nil | ||
64 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/codes/codes.go b/vendor/github.com/vmihailenco/msgpack/codes/codes.go new file mode 100644 index 0000000..906af37 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/codes/codes.go | |||
@@ -0,0 +1,86 @@ | |||
1 | package codes | ||
2 | |||
3 | type Code byte | ||
4 | |||
5 | var ( | ||
6 | PosFixedNumHigh Code = 0x7f | ||
7 | NegFixedNumLow Code = 0xe0 | ||
8 | |||
9 | Nil Code = 0xc0 | ||
10 | |||
11 | False Code = 0xc2 | ||
12 | True Code = 0xc3 | ||
13 | |||
14 | Float Code = 0xca | ||
15 | Double Code = 0xcb | ||
16 | |||
17 | Uint8 Code = 0xcc | ||
18 | Uint16 Code = 0xcd | ||
19 | Uint32 Code = 0xce | ||
20 | Uint64 Code = 0xcf | ||
21 | |||
22 | Int8 Code = 0xd0 | ||
23 | Int16 Code = 0xd1 | ||
24 | Int32 Code = 0xd2 | ||
25 | Int64 Code = 0xd3 | ||
26 | |||
27 | FixedStrLow Code = 0xa0 | ||
28 | FixedStrHigh Code = 0xbf | ||
29 | FixedStrMask Code = 0x1f | ||
30 | Str8 Code = 0xd9 | ||
31 | Str16 Code = 0xda | ||
32 | Str32 Code = 0xdb | ||
33 | |||
34 | Bin8 Code = 0xc4 | ||
35 | Bin16 Code = 0xc5 | ||
36 | Bin32 Code = 0xc6 | ||
37 | |||
38 | FixedArrayLow Code = 0x90 | ||
39 | FixedArrayHigh Code = 0x9f | ||
40 | FixedArrayMask Code = 0xf | ||
41 | Array16 Code = 0xdc | ||
42 | Array32 Code = 0xdd | ||
43 | |||
44 | FixedMapLow Code = 0x80 | ||
45 | FixedMapHigh Code = 0x8f | ||
46 | FixedMapMask Code = 0xf | ||
47 | Map16 Code = 0xde | ||
48 | Map32 Code = 0xdf | ||
49 | |||
50 | FixExt1 Code = 0xd4 | ||
51 | FixExt2 Code = 0xd5 | ||
52 | FixExt4 Code = 0xd6 | ||
53 | FixExt8 Code = 0xd7 | ||
54 | FixExt16 Code = 0xd8 | ||
55 | Ext8 Code = 0xc7 | ||
56 | Ext16 Code = 0xc8 | ||
57 | Ext32 Code = 0xc9 | ||
58 | ) | ||
59 | |||
60 | func IsFixedNum(c Code) bool { | ||
61 | return c <= PosFixedNumHigh || c >= NegFixedNumLow | ||
62 | } | ||
63 | |||
64 | func IsFixedMap(c Code) bool { | ||
65 | return c >= FixedMapLow && c <= FixedMapHigh | ||
66 | } | ||
67 | |||
68 | func IsFixedArray(c Code) bool { | ||
69 | return c >= FixedArrayLow && c <= FixedArrayHigh | ||
70 | } | ||
71 | |||
72 | func IsFixedString(c Code) bool { | ||
73 | return c >= FixedStrLow && c <= FixedStrHigh | ||
74 | } | ||
75 | |||
76 | func IsString(c Code) bool { | ||
77 | return IsFixedString(c) || c == Str8 || c == Str16 || c == Str32 | ||
78 | } | ||
79 | |||
80 | func IsFixedExt(c Code) bool { | ||
81 | return c >= FixExt1 && c <= FixExt16 | ||
82 | } | ||
83 | |||
84 | func IsExt(c Code) bool { | ||
85 | return IsFixedExt(c) || c == Ext8 || c == Ext16 || c == Ext32 | ||
86 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/decode.go b/vendor/github.com/vmihailenco/msgpack/decode.go new file mode 100644 index 0000000..7414100 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/decode.go | |||
@@ -0,0 +1,546 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "bufio" | ||
5 | "bytes" | ||
6 | "errors" | ||
7 | "fmt" | ||
8 | "io" | ||
9 | "reflect" | ||
10 | "time" | ||
11 | |||
12 | "github.com/vmihailenco/msgpack/codes" | ||
13 | ) | ||
14 | |||
15 | const bytesAllocLimit = 1024 * 1024 // 1mb | ||
16 | |||
17 | type bufReader interface { | ||
18 | io.Reader | ||
19 | io.ByteScanner | ||
20 | } | ||
21 | |||
22 | func newBufReader(r io.Reader) bufReader { | ||
23 | if br, ok := r.(bufReader); ok { | ||
24 | return br | ||
25 | } | ||
26 | return bufio.NewReader(r) | ||
27 | } | ||
28 | |||
29 | func makeBuffer() []byte { | ||
30 | return make([]byte, 0, 64) | ||
31 | } | ||
32 | |||
33 | // Unmarshal decodes the MessagePack-encoded data and stores the result | ||
34 | // in the value pointed to by v. | ||
35 | func Unmarshal(data []byte, v interface{}) error { | ||
36 | return NewDecoder(bytes.NewReader(data)).Decode(v) | ||
37 | } | ||
38 | |||
39 | type Decoder struct { | ||
40 | r io.Reader | ||
41 | s io.ByteScanner | ||
42 | buf []byte | ||
43 | |||
44 | extLen int | ||
45 | rec []byte // accumulates read data if not nil | ||
46 | |||
47 | useLoose bool | ||
48 | useJSONTag bool | ||
49 | |||
50 | decodeMapFunc func(*Decoder) (interface{}, error) | ||
51 | } | ||
52 | |||
53 | // NewDecoder returns a new decoder that reads from r. | ||
54 | // | ||
55 | // The decoder introduces its own buffering and may read data from r | ||
56 | // beyond the MessagePack values requested. Buffering can be disabled | ||
57 | // by passing a reader that implements io.ByteScanner interface. | ||
58 | func NewDecoder(r io.Reader) *Decoder { | ||
59 | d := &Decoder{ | ||
60 | buf: makeBuffer(), | ||
61 | } | ||
62 | d.resetReader(r) | ||
63 | return d | ||
64 | } | ||
65 | |||
66 | func (d *Decoder) SetDecodeMapFunc(fn func(*Decoder) (interface{}, error)) { | ||
67 | d.decodeMapFunc = fn | ||
68 | } | ||
69 | |||
70 | // UseDecodeInterfaceLoose causes decoder to use DecodeInterfaceLoose | ||
71 | // to decode msgpack value into Go interface{}. | ||
72 | func (d *Decoder) UseDecodeInterfaceLoose(flag bool) { | ||
73 | d.useLoose = flag | ||
74 | } | ||
75 | |||
76 | // UseJSONTag causes the Decoder to use json struct tag as fallback option | ||
77 | // if there is no msgpack tag. | ||
78 | func (d *Decoder) UseJSONTag(v bool) *Decoder { | ||
79 | d.useJSONTag = v | ||
80 | return d | ||
81 | } | ||
82 | |||
83 | func (d *Decoder) Reset(r io.Reader) error { | ||
84 | d.resetReader(r) | ||
85 | return nil | ||
86 | } | ||
87 | |||
88 | func (d *Decoder) resetReader(r io.Reader) { | ||
89 | reader := newBufReader(r) | ||
90 | d.r = reader | ||
91 | d.s = reader | ||
92 | } | ||
93 | |||
94 | func (d *Decoder) Decode(v interface{}) error { | ||
95 | var err error | ||
96 | switch v := v.(type) { | ||
97 | case *string: | ||
98 | if v != nil { | ||
99 | *v, err = d.DecodeString() | ||
100 | return err | ||
101 | } | ||
102 | case *[]byte: | ||
103 | if v != nil { | ||
104 | return d.decodeBytesPtr(v) | ||
105 | } | ||
106 | case *int: | ||
107 | if v != nil { | ||
108 | *v, err = d.DecodeInt() | ||
109 | return err | ||
110 | } | ||
111 | case *int8: | ||
112 | if v != nil { | ||
113 | *v, err = d.DecodeInt8() | ||
114 | return err | ||
115 | } | ||
116 | case *int16: | ||
117 | if v != nil { | ||
118 | *v, err = d.DecodeInt16() | ||
119 | return err | ||
120 | } | ||
121 | case *int32: | ||
122 | if v != nil { | ||
123 | *v, err = d.DecodeInt32() | ||
124 | return err | ||
125 | } | ||
126 | case *int64: | ||
127 | if v != nil { | ||
128 | *v, err = d.DecodeInt64() | ||
129 | return err | ||
130 | } | ||
131 | case *uint: | ||
132 | if v != nil { | ||
133 | *v, err = d.DecodeUint() | ||
134 | return err | ||
135 | } | ||
136 | case *uint8: | ||
137 | if v != nil { | ||
138 | *v, err = d.DecodeUint8() | ||
139 | return err | ||
140 | } | ||
141 | case *uint16: | ||
142 | if v != nil { | ||
143 | *v, err = d.DecodeUint16() | ||
144 | return err | ||
145 | } | ||
146 | case *uint32: | ||
147 | if v != nil { | ||
148 | *v, err = d.DecodeUint32() | ||
149 | return err | ||
150 | } | ||
151 | case *uint64: | ||
152 | if v != nil { | ||
153 | *v, err = d.DecodeUint64() | ||
154 | return err | ||
155 | } | ||
156 | case *bool: | ||
157 | if v != nil { | ||
158 | *v, err = d.DecodeBool() | ||
159 | return err | ||
160 | } | ||
161 | case *float32: | ||
162 | if v != nil { | ||
163 | *v, err = d.DecodeFloat32() | ||
164 | return err | ||
165 | } | ||
166 | case *float64: | ||
167 | if v != nil { | ||
168 | *v, err = d.DecodeFloat64() | ||
169 | return err | ||
170 | } | ||
171 | case *[]string: | ||
172 | return d.decodeStringSlicePtr(v) | ||
173 | case *map[string]string: | ||
174 | return d.decodeMapStringStringPtr(v) | ||
175 | case *map[string]interface{}: | ||
176 | return d.decodeMapStringInterfacePtr(v) | ||
177 | case *time.Duration: | ||
178 | if v != nil { | ||
179 | vv, err := d.DecodeInt64() | ||
180 | *v = time.Duration(vv) | ||
181 | return err | ||
182 | } | ||
183 | case *time.Time: | ||
184 | if v != nil { | ||
185 | *v, err = d.DecodeTime() | ||
186 | return err | ||
187 | } | ||
188 | } | ||
189 | |||
190 | vv := reflect.ValueOf(v) | ||
191 | if !vv.IsValid() { | ||
192 | return errors.New("msgpack: Decode(nil)") | ||
193 | } | ||
194 | if vv.Kind() != reflect.Ptr { | ||
195 | return fmt.Errorf("msgpack: Decode(nonsettable %T)", v) | ||
196 | } | ||
197 | vv = vv.Elem() | ||
198 | if !vv.IsValid() { | ||
199 | return fmt.Errorf("msgpack: Decode(nonsettable %T)", v) | ||
200 | } | ||
201 | return d.DecodeValue(vv) | ||
202 | } | ||
203 | |||
204 | func (d *Decoder) DecodeMulti(v ...interface{}) error { | ||
205 | for _, vv := range v { | ||
206 | if err := d.Decode(vv); err != nil { | ||
207 | return err | ||
208 | } | ||
209 | } | ||
210 | return nil | ||
211 | } | ||
212 | |||
213 | func (d *Decoder) decodeInterfaceCond() (interface{}, error) { | ||
214 | if d.useLoose { | ||
215 | return d.DecodeInterfaceLoose() | ||
216 | } | ||
217 | return d.DecodeInterface() | ||
218 | } | ||
219 | |||
220 | func (d *Decoder) DecodeValue(v reflect.Value) error { | ||
221 | decode := getDecoder(v.Type()) | ||
222 | return decode(d, v) | ||
223 | } | ||
224 | |||
225 | func (d *Decoder) DecodeNil() error { | ||
226 | c, err := d.readCode() | ||
227 | if err != nil { | ||
228 | return err | ||
229 | } | ||
230 | if c != codes.Nil { | ||
231 | return fmt.Errorf("msgpack: invalid code=%x decoding nil", c) | ||
232 | } | ||
233 | return nil | ||
234 | } | ||
235 | |||
236 | func (d *Decoder) decodeNilValue(v reflect.Value) error { | ||
237 | err := d.DecodeNil() | ||
238 | if v.IsNil() { | ||
239 | return err | ||
240 | } | ||
241 | if v.Kind() == reflect.Ptr { | ||
242 | v = v.Elem() | ||
243 | } | ||
244 | v.Set(reflect.Zero(v.Type())) | ||
245 | return err | ||
246 | } | ||
247 | |||
248 | func (d *Decoder) DecodeBool() (bool, error) { | ||
249 | c, err := d.readCode() | ||
250 | if err != nil { | ||
251 | return false, err | ||
252 | } | ||
253 | return d.bool(c) | ||
254 | } | ||
255 | |||
256 | func (d *Decoder) bool(c codes.Code) (bool, error) { | ||
257 | if c == codes.False { | ||
258 | return false, nil | ||
259 | } | ||
260 | if c == codes.True { | ||
261 | return true, nil | ||
262 | } | ||
263 | return false, fmt.Errorf("msgpack: invalid code=%x decoding bool", c) | ||
264 | } | ||
265 | |||
266 | // DecodeInterface decodes value into interface. It returns following types: | ||
267 | // - nil, | ||
268 | // - bool, | ||
269 | // - int8, int16, int32, int64, | ||
270 | // - uint8, uint16, uint32, uint64, | ||
271 | // - float32 and float64, | ||
272 | // - string, | ||
273 | // - []byte, | ||
274 | // - slices of any of the above, | ||
275 | // - maps of any of the above. | ||
276 | // | ||
277 | // DecodeInterface should be used only when you don't know the type of value | ||
278 | // you are decoding. For example, if you are decoding number it is better to use | ||
279 | // DecodeInt64 for negative numbers and DecodeUint64 for positive numbers. | ||
280 | func (d *Decoder) DecodeInterface() (interface{}, error) { | ||
281 | c, err := d.readCode() | ||
282 | if err != nil { | ||
283 | return nil, err | ||
284 | } | ||
285 | |||
286 | if codes.IsFixedNum(c) { | ||
287 | return int8(c), nil | ||
288 | } | ||
289 | if codes.IsFixedMap(c) { | ||
290 | err = d.s.UnreadByte() | ||
291 | if err != nil { | ||
292 | return nil, err | ||
293 | } | ||
294 | return d.DecodeMap() | ||
295 | } | ||
296 | if codes.IsFixedArray(c) { | ||
297 | return d.decodeSlice(c) | ||
298 | } | ||
299 | if codes.IsFixedString(c) { | ||
300 | return d.string(c) | ||
301 | } | ||
302 | |||
303 | switch c { | ||
304 | case codes.Nil: | ||
305 | return nil, nil | ||
306 | case codes.False, codes.True: | ||
307 | return d.bool(c) | ||
308 | case codes.Float: | ||
309 | return d.float32(c) | ||
310 | case codes.Double: | ||
311 | return d.float64(c) | ||
312 | case codes.Uint8: | ||
313 | return d.uint8() | ||
314 | case codes.Uint16: | ||
315 | return d.uint16() | ||
316 | case codes.Uint32: | ||
317 | return d.uint32() | ||
318 | case codes.Uint64: | ||
319 | return d.uint64() | ||
320 | case codes.Int8: | ||
321 | return d.int8() | ||
322 | case codes.Int16: | ||
323 | return d.int16() | ||
324 | case codes.Int32: | ||
325 | return d.int32() | ||
326 | case codes.Int64: | ||
327 | return d.int64() | ||
328 | case codes.Bin8, codes.Bin16, codes.Bin32: | ||
329 | return d.bytes(c, nil) | ||
330 | case codes.Str8, codes.Str16, codes.Str32: | ||
331 | return d.string(c) | ||
332 | case codes.Array16, codes.Array32: | ||
333 | return d.decodeSlice(c) | ||
334 | case codes.Map16, codes.Map32: | ||
335 | err = d.s.UnreadByte() | ||
336 | if err != nil { | ||
337 | return nil, err | ||
338 | } | ||
339 | return d.DecodeMap() | ||
340 | case codes.FixExt1, codes.FixExt2, codes.FixExt4, codes.FixExt8, codes.FixExt16, | ||
341 | codes.Ext8, codes.Ext16, codes.Ext32: | ||
342 | return d.extInterface(c) | ||
343 | } | ||
344 | |||
345 | return 0, fmt.Errorf("msgpack: unknown code %x decoding interface{}", c) | ||
346 | } | ||
347 | |||
348 | // DecodeInterfaceLoose is like DecodeInterface except that: | ||
349 | // - int8, int16, and int32 are converted to int64, | ||
350 | // - uint8, uint16, and uint32 are converted to uint64, | ||
351 | // - float32 is converted to float64. | ||
352 | func (d *Decoder) DecodeInterfaceLoose() (interface{}, error) { | ||
353 | c, err := d.readCode() | ||
354 | if err != nil { | ||
355 | return nil, err | ||
356 | } | ||
357 | |||
358 | if codes.IsFixedNum(c) { | ||
359 | return int64(c), nil | ||
360 | } | ||
361 | if codes.IsFixedMap(c) { | ||
362 | err = d.s.UnreadByte() | ||
363 | if err != nil { | ||
364 | return nil, err | ||
365 | } | ||
366 | return d.DecodeMap() | ||
367 | } | ||
368 | if codes.IsFixedArray(c) { | ||
369 | return d.decodeSlice(c) | ||
370 | } | ||
371 | if codes.IsFixedString(c) { | ||
372 | return d.string(c) | ||
373 | } | ||
374 | |||
375 | switch c { | ||
376 | case codes.Nil: | ||
377 | return nil, nil | ||
378 | case codes.False, codes.True: | ||
379 | return d.bool(c) | ||
380 | case codes.Float, codes.Double: | ||
381 | return d.float64(c) | ||
382 | case codes.Uint8, codes.Uint16, codes.Uint32, codes.Uint64: | ||
383 | return d.uint(c) | ||
384 | case codes.Int8, codes.Int16, codes.Int32, codes.Int64: | ||
385 | return d.int(c) | ||
386 | case codes.Bin8, codes.Bin16, codes.Bin32: | ||
387 | return d.bytes(c, nil) | ||
388 | case codes.Str8, codes.Str16, codes.Str32: | ||
389 | return d.string(c) | ||
390 | case codes.Array16, codes.Array32: | ||
391 | return d.decodeSlice(c) | ||
392 | case codes.Map16, codes.Map32: | ||
393 | err = d.s.UnreadByte() | ||
394 | if err != nil { | ||
395 | return nil, err | ||
396 | } | ||
397 | return d.DecodeMap() | ||
398 | case codes.FixExt1, codes.FixExt2, codes.FixExt4, codes.FixExt8, codes.FixExt16, | ||
399 | codes.Ext8, codes.Ext16, codes.Ext32: | ||
400 | return d.extInterface(c) | ||
401 | } | ||
402 | |||
403 | return 0, fmt.Errorf("msgpack: unknown code %x decoding interface{}", c) | ||
404 | } | ||
405 | |||
406 | // Skip skips next value. | ||
407 | func (d *Decoder) Skip() error { | ||
408 | c, err := d.readCode() | ||
409 | if err != nil { | ||
410 | return err | ||
411 | } | ||
412 | |||
413 | if codes.IsFixedNum(c) { | ||
414 | return nil | ||
415 | } else if codes.IsFixedMap(c) { | ||
416 | return d.skipMap(c) | ||
417 | } else if codes.IsFixedArray(c) { | ||
418 | return d.skipSlice(c) | ||
419 | } else if codes.IsFixedString(c) { | ||
420 | return d.skipBytes(c) | ||
421 | } | ||
422 | |||
423 | switch c { | ||
424 | case codes.Nil, codes.False, codes.True: | ||
425 | return nil | ||
426 | case codes.Uint8, codes.Int8: | ||
427 | return d.skipN(1) | ||
428 | case codes.Uint16, codes.Int16: | ||
429 | return d.skipN(2) | ||
430 | case codes.Uint32, codes.Int32, codes.Float: | ||
431 | return d.skipN(4) | ||
432 | case codes.Uint64, codes.Int64, codes.Double: | ||
433 | return d.skipN(8) | ||
434 | case codes.Bin8, codes.Bin16, codes.Bin32: | ||
435 | return d.skipBytes(c) | ||
436 | case codes.Str8, codes.Str16, codes.Str32: | ||
437 | return d.skipBytes(c) | ||
438 | case codes.Array16, codes.Array32: | ||
439 | return d.skipSlice(c) | ||
440 | case codes.Map16, codes.Map32: | ||
441 | return d.skipMap(c) | ||
442 | case codes.FixExt1, codes.FixExt2, codes.FixExt4, codes.FixExt8, codes.FixExt16, | ||
443 | codes.Ext8, codes.Ext16, codes.Ext32: | ||
444 | return d.skipExt(c) | ||
445 | } | ||
446 | |||
447 | return fmt.Errorf("msgpack: unknown code %x", c) | ||
448 | } | ||
449 | |||
450 | // PeekCode returns the next MessagePack code without advancing the reader. | ||
451 | // Subpackage msgpack/codes contains list of available codes. | ||
452 | func (d *Decoder) PeekCode() (codes.Code, error) { | ||
453 | c, err := d.s.ReadByte() | ||
454 | if err != nil { | ||
455 | return 0, err | ||
456 | } | ||
457 | return codes.Code(c), d.s.UnreadByte() | ||
458 | } | ||
459 | |||
460 | func (d *Decoder) hasNilCode() bool { | ||
461 | code, err := d.PeekCode() | ||
462 | return err == nil && code == codes.Nil | ||
463 | } | ||
464 | |||
465 | func (d *Decoder) readCode() (codes.Code, error) { | ||
466 | d.extLen = 0 | ||
467 | c, err := d.s.ReadByte() | ||
468 | if err != nil { | ||
469 | return 0, err | ||
470 | } | ||
471 | if d.rec != nil { | ||
472 | d.rec = append(d.rec, c) | ||
473 | } | ||
474 | return codes.Code(c), nil | ||
475 | } | ||
476 | |||
477 | func (d *Decoder) readFull(b []byte) error { | ||
478 | _, err := io.ReadFull(d.r, b) | ||
479 | if err != nil { | ||
480 | return err | ||
481 | } | ||
482 | if d.rec != nil { | ||
483 | d.rec = append(d.rec, b...) | ||
484 | } | ||
485 | return nil | ||
486 | } | ||
487 | |||
488 | func (d *Decoder) readN(n int) ([]byte, error) { | ||
489 | buf, err := readN(d.r, d.buf, n) | ||
490 | if err != nil { | ||
491 | return nil, err | ||
492 | } | ||
493 | d.buf = buf | ||
494 | if d.rec != nil { | ||
495 | d.rec = append(d.rec, buf...) | ||
496 | } | ||
497 | return buf, nil | ||
498 | } | ||
499 | |||
500 | func readN(r io.Reader, b []byte, n int) ([]byte, error) { | ||
501 | if b == nil { | ||
502 | if n == 0 { | ||
503 | return make([]byte, 0), nil | ||
504 | } | ||
505 | if n <= bytesAllocLimit { | ||
506 | b = make([]byte, n) | ||
507 | } else { | ||
508 | b = make([]byte, bytesAllocLimit) | ||
509 | } | ||
510 | } | ||
511 | |||
512 | if n <= cap(b) { | ||
513 | b = b[:n] | ||
514 | _, err := io.ReadFull(r, b) | ||
515 | return b, err | ||
516 | } | ||
517 | b = b[:cap(b)] | ||
518 | |||
519 | var pos int | ||
520 | for { | ||
521 | alloc := n - len(b) | ||
522 | if alloc > bytesAllocLimit { | ||
523 | alloc = bytesAllocLimit | ||
524 | } | ||
525 | b = append(b, make([]byte, alloc)...) | ||
526 | |||
527 | _, err := io.ReadFull(r, b[pos:]) | ||
528 | if err != nil { | ||
529 | return nil, err | ||
530 | } | ||
531 | |||
532 | if len(b) == n { | ||
533 | break | ||
534 | } | ||
535 | pos = len(b) | ||
536 | } | ||
537 | |||
538 | return b, nil | ||
539 | } | ||
540 | |||
541 | func min(a, b int) int { | ||
542 | if a <= b { | ||
543 | return a | ||
544 | } | ||
545 | return b | ||
546 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/decode_map.go b/vendor/github.com/vmihailenco/msgpack/decode_map.go new file mode 100644 index 0000000..b542a75 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/decode_map.go | |||
@@ -0,0 +1,338 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "errors" | ||
5 | "fmt" | ||
6 | "reflect" | ||
7 | |||
8 | "github.com/vmihailenco/msgpack/codes" | ||
9 | ) | ||
10 | |||
11 | const mapElemsAllocLimit = 1e4 | ||
12 | |||
13 | var mapStringStringPtrType = reflect.TypeOf((*map[string]string)(nil)) | ||
14 | var mapStringStringType = mapStringStringPtrType.Elem() | ||
15 | |||
16 | var mapStringInterfacePtrType = reflect.TypeOf((*map[string]interface{})(nil)) | ||
17 | var mapStringInterfaceType = mapStringInterfacePtrType.Elem() | ||
18 | |||
19 | var errInvalidCode = errors.New("invalid code") | ||
20 | |||
21 | func decodeMapValue(d *Decoder, v reflect.Value) error { | ||
22 | size, err := d.DecodeMapLen() | ||
23 | if err != nil { | ||
24 | return err | ||
25 | } | ||
26 | |||
27 | typ := v.Type() | ||
28 | if size == -1 { | ||
29 | v.Set(reflect.Zero(typ)) | ||
30 | return nil | ||
31 | } | ||
32 | |||
33 | if v.IsNil() { | ||
34 | v.Set(reflect.MakeMap(typ)) | ||
35 | } | ||
36 | if size == 0 { | ||
37 | return nil | ||
38 | } | ||
39 | |||
40 | return decodeMapValueSize(d, v, size) | ||
41 | } | ||
42 | |||
43 | func decodeMapValueSize(d *Decoder, v reflect.Value, size int) error { | ||
44 | typ := v.Type() | ||
45 | keyType := typ.Key() | ||
46 | valueType := typ.Elem() | ||
47 | |||
48 | for i := 0; i < size; i++ { | ||
49 | mk := reflect.New(keyType).Elem() | ||
50 | if err := d.DecodeValue(mk); err != nil { | ||
51 | return err | ||
52 | } | ||
53 | |||
54 | mv := reflect.New(valueType).Elem() | ||
55 | if err := d.DecodeValue(mv); err != nil { | ||
56 | return err | ||
57 | } | ||
58 | |||
59 | v.SetMapIndex(mk, mv) | ||
60 | } | ||
61 | |||
62 | return nil | ||
63 | } | ||
64 | |||
65 | // DecodeMapLen decodes map length. Length is -1 when map is nil. | ||
66 | func (d *Decoder) DecodeMapLen() (int, error) { | ||
67 | c, err := d.readCode() | ||
68 | if err != nil { | ||
69 | return 0, err | ||
70 | } | ||
71 | |||
72 | if codes.IsExt(c) { | ||
73 | if err = d.skipExtHeader(c); err != nil { | ||
74 | return 0, err | ||
75 | } | ||
76 | |||
77 | c, err = d.readCode() | ||
78 | if err != nil { | ||
79 | return 0, err | ||
80 | } | ||
81 | } | ||
82 | return d.mapLen(c) | ||
83 | } | ||
84 | |||
85 | func (d *Decoder) mapLen(c codes.Code) (int, error) { | ||
86 | size, err := d._mapLen(c) | ||
87 | err = expandInvalidCodeMapLenError(c, err) | ||
88 | return size, err | ||
89 | } | ||
90 | |||
91 | func (d *Decoder) _mapLen(c codes.Code) (int, error) { | ||
92 | if c == codes.Nil { | ||
93 | return -1, nil | ||
94 | } | ||
95 | if c >= codes.FixedMapLow && c <= codes.FixedMapHigh { | ||
96 | return int(c & codes.FixedMapMask), nil | ||
97 | } | ||
98 | if c == codes.Map16 { | ||
99 | size, err := d.uint16() | ||
100 | return int(size), err | ||
101 | } | ||
102 | if c == codes.Map32 { | ||
103 | size, err := d.uint32() | ||
104 | return int(size), err | ||
105 | } | ||
106 | return 0, errInvalidCode | ||
107 | } | ||
108 | |||
109 | func expandInvalidCodeMapLenError(c codes.Code, err error) error { | ||
110 | if err == errInvalidCode { | ||
111 | return fmt.Errorf("msgpack: invalid code=%x decoding map length", c) | ||
112 | } | ||
113 | return err | ||
114 | } | ||
115 | |||
116 | func decodeMapStringStringValue(d *Decoder, v reflect.Value) error { | ||
117 | mptr := v.Addr().Convert(mapStringStringPtrType).Interface().(*map[string]string) | ||
118 | return d.decodeMapStringStringPtr(mptr) | ||
119 | } | ||
120 | |||
121 | func (d *Decoder) decodeMapStringStringPtr(ptr *map[string]string) error { | ||
122 | size, err := d.DecodeMapLen() | ||
123 | if err != nil { | ||
124 | return err | ||
125 | } | ||
126 | if size == -1 { | ||
127 | *ptr = nil | ||
128 | return nil | ||
129 | } | ||
130 | |||
131 | m := *ptr | ||
132 | if m == nil { | ||
133 | *ptr = make(map[string]string, min(size, mapElemsAllocLimit)) | ||
134 | m = *ptr | ||
135 | } | ||
136 | |||
137 | for i := 0; i < size; i++ { | ||
138 | mk, err := d.DecodeString() | ||
139 | if err != nil { | ||
140 | return err | ||
141 | } | ||
142 | mv, err := d.DecodeString() | ||
143 | if err != nil { | ||
144 | return err | ||
145 | } | ||
146 | m[mk] = mv | ||
147 | } | ||
148 | |||
149 | return nil | ||
150 | } | ||
151 | |||
152 | func decodeMapStringInterfaceValue(d *Decoder, v reflect.Value) error { | ||
153 | ptr := v.Addr().Convert(mapStringInterfacePtrType).Interface().(*map[string]interface{}) | ||
154 | return d.decodeMapStringInterfacePtr(ptr) | ||
155 | } | ||
156 | |||
157 | func (d *Decoder) decodeMapStringInterfacePtr(ptr *map[string]interface{}) error { | ||
158 | n, err := d.DecodeMapLen() | ||
159 | if err != nil { | ||
160 | return err | ||
161 | } | ||
162 | if n == -1 { | ||
163 | *ptr = nil | ||
164 | return nil | ||
165 | } | ||
166 | |||
167 | m := *ptr | ||
168 | if m == nil { | ||
169 | *ptr = make(map[string]interface{}, min(n, mapElemsAllocLimit)) | ||
170 | m = *ptr | ||
171 | } | ||
172 | |||
173 | for i := 0; i < n; i++ { | ||
174 | mk, err := d.DecodeString() | ||
175 | if err != nil { | ||
176 | return err | ||
177 | } | ||
178 | mv, err := d.decodeInterfaceCond() | ||
179 | if err != nil { | ||
180 | return err | ||
181 | } | ||
182 | m[mk] = mv | ||
183 | } | ||
184 | |||
185 | return nil | ||
186 | } | ||
187 | |||
188 | func (d *Decoder) DecodeMap() (interface{}, error) { | ||
189 | if d.decodeMapFunc != nil { | ||
190 | return d.decodeMapFunc(d) | ||
191 | } | ||
192 | |||
193 | size, err := d.DecodeMapLen() | ||
194 | if err != nil { | ||
195 | return nil, err | ||
196 | } | ||
197 | if size == -1 { | ||
198 | return nil, nil | ||
199 | } | ||
200 | if size == 0 { | ||
201 | return make(map[string]interface{}), nil | ||
202 | } | ||
203 | |||
204 | code, err := d.PeekCode() | ||
205 | if err != nil { | ||
206 | return nil, err | ||
207 | } | ||
208 | |||
209 | if codes.IsString(code) { | ||
210 | return d.decodeMapStringInterfaceSize(size) | ||
211 | } | ||
212 | |||
213 | key, err := d.decodeInterfaceCond() | ||
214 | if err != nil { | ||
215 | return nil, err | ||
216 | } | ||
217 | |||
218 | value, err := d.decodeInterfaceCond() | ||
219 | if err != nil { | ||
220 | return nil, err | ||
221 | } | ||
222 | |||
223 | keyType := reflect.TypeOf(key) | ||
224 | valueType := reflect.TypeOf(value) | ||
225 | mapType := reflect.MapOf(keyType, valueType) | ||
226 | mapValue := reflect.MakeMap(mapType) | ||
227 | |||
228 | mapValue.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value)) | ||
229 | size-- | ||
230 | |||
231 | err = decodeMapValueSize(d, mapValue, size) | ||
232 | if err != nil { | ||
233 | return nil, err | ||
234 | } | ||
235 | |||
236 | return mapValue.Interface(), nil | ||
237 | } | ||
238 | |||
239 | func (d *Decoder) decodeMapStringInterfaceSize(size int) (map[string]interface{}, error) { | ||
240 | m := make(map[string]interface{}, min(size, mapElemsAllocLimit)) | ||
241 | for i := 0; i < size; i++ { | ||
242 | mk, err := d.DecodeString() | ||
243 | if err != nil { | ||
244 | return nil, err | ||
245 | } | ||
246 | mv, err := d.decodeInterfaceCond() | ||
247 | if err != nil { | ||
248 | return nil, err | ||
249 | } | ||
250 | m[mk] = mv | ||
251 | } | ||
252 | return m, nil | ||
253 | } | ||
254 | |||
255 | func (d *Decoder) skipMap(c codes.Code) error { | ||
256 | n, err := d.mapLen(c) | ||
257 | if err != nil { | ||
258 | return err | ||
259 | } | ||
260 | for i := 0; i < n; i++ { | ||
261 | if err := d.Skip(); err != nil { | ||
262 | return err | ||
263 | } | ||
264 | if err := d.Skip(); err != nil { | ||
265 | return err | ||
266 | } | ||
267 | } | ||
268 | return nil | ||
269 | } | ||
270 | |||
271 | func decodeStructValue(d *Decoder, v reflect.Value) error { | ||
272 | c, err := d.readCode() | ||
273 | if err != nil { | ||
274 | return err | ||
275 | } | ||
276 | |||
277 | var isArray bool | ||
278 | |||
279 | n, err := d._mapLen(c) | ||
280 | if err != nil { | ||
281 | var err2 error | ||
282 | n, err2 = d.arrayLen(c) | ||
283 | if err2 != nil { | ||
284 | return expandInvalidCodeMapLenError(c, err) | ||
285 | } | ||
286 | isArray = true | ||
287 | } | ||
288 | if n == -1 { | ||
289 | if err = mustSet(v); err != nil { | ||
290 | return err | ||
291 | } | ||
292 | v.Set(reflect.Zero(v.Type())) | ||
293 | return nil | ||
294 | } | ||
295 | |||
296 | var fields *fields | ||
297 | if d.useJSONTag { | ||
298 | fields = jsonStructs.Fields(v.Type()) | ||
299 | } else { | ||
300 | fields = structs.Fields(v.Type()) | ||
301 | } | ||
302 | |||
303 | if isArray { | ||
304 | for i, f := range fields.List { | ||
305 | if i >= n { | ||
306 | break | ||
307 | } | ||
308 | if err := f.DecodeValue(d, v); err != nil { | ||
309 | return err | ||
310 | } | ||
311 | } | ||
312 | // Skip extra values. | ||
313 | for i := len(fields.List); i < n; i++ { | ||
314 | if err := d.Skip(); err != nil { | ||
315 | return err | ||
316 | } | ||
317 | } | ||
318 | return nil | ||
319 | } | ||
320 | |||
321 | for i := 0; i < n; i++ { | ||
322 | name, err := d.DecodeString() | ||
323 | if err != nil { | ||
324 | return err | ||
325 | } | ||
326 | if f := fields.Table[name]; f != nil { | ||
327 | if err := f.DecodeValue(d, v); err != nil { | ||
328 | return err | ||
329 | } | ||
330 | } else { | ||
331 | if err := d.Skip(); err != nil { | ||
332 | return err | ||
333 | } | ||
334 | } | ||
335 | } | ||
336 | |||
337 | return nil | ||
338 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/decode_number.go b/vendor/github.com/vmihailenco/msgpack/decode_number.go new file mode 100644 index 0000000..15019cc --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/decode_number.go | |||
@@ -0,0 +1,307 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "math" | ||
6 | "reflect" | ||
7 | |||
8 | "github.com/vmihailenco/msgpack/codes" | ||
9 | ) | ||
10 | |||
11 | func (d *Decoder) skipN(n int) error { | ||
12 | _, err := d.readN(n) | ||
13 | return err | ||
14 | } | ||
15 | |||
16 | func (d *Decoder) uint8() (uint8, error) { | ||
17 | c, err := d.readCode() | ||
18 | if err != nil { | ||
19 | return 0, err | ||
20 | } | ||
21 | return uint8(c), nil | ||
22 | } | ||
23 | |||
24 | func (d *Decoder) int8() (int8, error) { | ||
25 | n, err := d.uint8() | ||
26 | return int8(n), err | ||
27 | } | ||
28 | |||
29 | func (d *Decoder) uint16() (uint16, error) { | ||
30 | b, err := d.readN(2) | ||
31 | if err != nil { | ||
32 | return 0, err | ||
33 | } | ||
34 | return (uint16(b[0]) << 8) | uint16(b[1]), nil | ||
35 | } | ||
36 | |||
37 | func (d *Decoder) int16() (int16, error) { | ||
38 | n, err := d.uint16() | ||
39 | return int16(n), err | ||
40 | } | ||
41 | |||
42 | func (d *Decoder) uint32() (uint32, error) { | ||
43 | b, err := d.readN(4) | ||
44 | if err != nil { | ||
45 | return 0, err | ||
46 | } | ||
47 | n := (uint32(b[0]) << 24) | | ||
48 | (uint32(b[1]) << 16) | | ||
49 | (uint32(b[2]) << 8) | | ||
50 | uint32(b[3]) | ||
51 | return n, nil | ||
52 | } | ||
53 | |||
54 | func (d *Decoder) int32() (int32, error) { | ||
55 | n, err := d.uint32() | ||
56 | return int32(n), err | ||
57 | } | ||
58 | |||
59 | func (d *Decoder) uint64() (uint64, error) { | ||
60 | b, err := d.readN(8) | ||
61 | if err != nil { | ||
62 | return 0, err | ||
63 | } | ||
64 | n := (uint64(b[0]) << 56) | | ||
65 | (uint64(b[1]) << 48) | | ||
66 | (uint64(b[2]) << 40) | | ||
67 | (uint64(b[3]) << 32) | | ||
68 | (uint64(b[4]) << 24) | | ||
69 | (uint64(b[5]) << 16) | | ||
70 | (uint64(b[6]) << 8) | | ||
71 | uint64(b[7]) | ||
72 | return n, nil | ||
73 | } | ||
74 | |||
75 | func (d *Decoder) int64() (int64, error) { | ||
76 | n, err := d.uint64() | ||
77 | return int64(n), err | ||
78 | } | ||
79 | |||
80 | // DecodeUint64 decodes msgpack int8/16/32/64 and uint8/16/32/64 | ||
81 | // into Go uint64. | ||
82 | func (d *Decoder) DecodeUint64() (uint64, error) { | ||
83 | c, err := d.readCode() | ||
84 | if err != nil { | ||
85 | return 0, err | ||
86 | } | ||
87 | return d.uint(c) | ||
88 | } | ||
89 | |||
90 | func (d *Decoder) uint(c codes.Code) (uint64, error) { | ||
91 | if c == codes.Nil { | ||
92 | return 0, nil | ||
93 | } | ||
94 | if codes.IsFixedNum(c) { | ||
95 | return uint64(int8(c)), nil | ||
96 | } | ||
97 | switch c { | ||
98 | case codes.Uint8: | ||
99 | n, err := d.uint8() | ||
100 | return uint64(n), err | ||
101 | case codes.Int8: | ||
102 | n, err := d.int8() | ||
103 | return uint64(n), err | ||
104 | case codes.Uint16: | ||
105 | n, err := d.uint16() | ||
106 | return uint64(n), err | ||
107 | case codes.Int16: | ||
108 | n, err := d.int16() | ||
109 | return uint64(n), err | ||
110 | case codes.Uint32: | ||
111 | n, err := d.uint32() | ||
112 | return uint64(n), err | ||
113 | case codes.Int32: | ||
114 | n, err := d.int32() | ||
115 | return uint64(n), err | ||
116 | case codes.Uint64, codes.Int64: | ||
117 | return d.uint64() | ||
118 | } | ||
119 | return 0, fmt.Errorf("msgpack: invalid code=%x decoding uint64", c) | ||
120 | } | ||
121 | |||
122 | // DecodeInt64 decodes msgpack int8/16/32/64 and uint8/16/32/64 | ||
123 | // into Go int64. | ||
124 | func (d *Decoder) DecodeInt64() (int64, error) { | ||
125 | c, err := d.readCode() | ||
126 | if err != nil { | ||
127 | return 0, err | ||
128 | } | ||
129 | return d.int(c) | ||
130 | } | ||
131 | |||
132 | func (d *Decoder) int(c codes.Code) (int64, error) { | ||
133 | if c == codes.Nil { | ||
134 | return 0, nil | ||
135 | } | ||
136 | if codes.IsFixedNum(c) { | ||
137 | return int64(int8(c)), nil | ||
138 | } | ||
139 | switch c { | ||
140 | case codes.Uint8: | ||
141 | n, err := d.uint8() | ||
142 | return int64(n), err | ||
143 | case codes.Int8: | ||
144 | n, err := d.uint8() | ||
145 | return int64(int8(n)), err | ||
146 | case codes.Uint16: | ||
147 | n, err := d.uint16() | ||
148 | return int64(n), err | ||
149 | case codes.Int16: | ||
150 | n, err := d.uint16() | ||
151 | return int64(int16(n)), err | ||
152 | case codes.Uint32: | ||
153 | n, err := d.uint32() | ||
154 | return int64(n), err | ||
155 | case codes.Int32: | ||
156 | n, err := d.uint32() | ||
157 | return int64(int32(n)), err | ||
158 | case codes.Uint64, codes.Int64: | ||
159 | n, err := d.uint64() | ||
160 | return int64(n), err | ||
161 | } | ||
162 | return 0, fmt.Errorf("msgpack: invalid code=%x decoding int64", c) | ||
163 | } | ||
164 | |||
165 | func (d *Decoder) DecodeFloat32() (float32, error) { | ||
166 | c, err := d.readCode() | ||
167 | if err != nil { | ||
168 | return 0, err | ||
169 | } | ||
170 | return d.float32(c) | ||
171 | } | ||
172 | |||
173 | func (d *Decoder) float32(c codes.Code) (float32, error) { | ||
174 | if c == codes.Float { | ||
175 | n, err := d.uint32() | ||
176 | if err != nil { | ||
177 | return 0, err | ||
178 | } | ||
179 | return math.Float32frombits(n), nil | ||
180 | } | ||
181 | |||
182 | n, err := d.int(c) | ||
183 | if err != nil { | ||
184 | return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c) | ||
185 | } | ||
186 | return float32(n), nil | ||
187 | } | ||
188 | |||
189 | // DecodeFloat64 decodes msgpack float32/64 into Go float64. | ||
190 | func (d *Decoder) DecodeFloat64() (float64, error) { | ||
191 | c, err := d.readCode() | ||
192 | if err != nil { | ||
193 | return 0, err | ||
194 | } | ||
195 | return d.float64(c) | ||
196 | } | ||
197 | |||
198 | func (d *Decoder) float64(c codes.Code) (float64, error) { | ||
199 | switch c { | ||
200 | case codes.Float: | ||
201 | n, err := d.float32(c) | ||
202 | if err != nil { | ||
203 | return 0, err | ||
204 | } | ||
205 | return float64(n), nil | ||
206 | case codes.Double: | ||
207 | n, err := d.uint64() | ||
208 | if err != nil { | ||
209 | return 0, err | ||
210 | } | ||
211 | return math.Float64frombits(n), nil | ||
212 | } | ||
213 | |||
214 | n, err := d.int(c) | ||
215 | if err != nil { | ||
216 | return 0, fmt.Errorf("msgpack: invalid code=%x decoding float32", c) | ||
217 | } | ||
218 | return float64(n), nil | ||
219 | } | ||
220 | |||
221 | func (d *Decoder) DecodeUint() (uint, error) { | ||
222 | n, err := d.DecodeUint64() | ||
223 | return uint(n), err | ||
224 | } | ||
225 | |||
226 | func (d *Decoder) DecodeUint8() (uint8, error) { | ||
227 | n, err := d.DecodeUint64() | ||
228 | return uint8(n), err | ||
229 | } | ||
230 | |||
231 | func (d *Decoder) DecodeUint16() (uint16, error) { | ||
232 | n, err := d.DecodeUint64() | ||
233 | return uint16(n), err | ||
234 | } | ||
235 | |||
236 | func (d *Decoder) DecodeUint32() (uint32, error) { | ||
237 | n, err := d.DecodeUint64() | ||
238 | return uint32(n), err | ||
239 | } | ||
240 | |||
241 | func (d *Decoder) DecodeInt() (int, error) { | ||
242 | n, err := d.DecodeInt64() | ||
243 | return int(n), err | ||
244 | } | ||
245 | |||
246 | func (d *Decoder) DecodeInt8() (int8, error) { | ||
247 | n, err := d.DecodeInt64() | ||
248 | return int8(n), err | ||
249 | } | ||
250 | |||
251 | func (d *Decoder) DecodeInt16() (int16, error) { | ||
252 | n, err := d.DecodeInt64() | ||
253 | return int16(n), err | ||
254 | } | ||
255 | |||
256 | func (d *Decoder) DecodeInt32() (int32, error) { | ||
257 | n, err := d.DecodeInt64() | ||
258 | return int32(n), err | ||
259 | } | ||
260 | |||
261 | func decodeFloat32Value(d *Decoder, v reflect.Value) error { | ||
262 | f, err := d.DecodeFloat32() | ||
263 | if err != nil { | ||
264 | return err | ||
265 | } | ||
266 | if err = mustSet(v); err != nil { | ||
267 | return err | ||
268 | } | ||
269 | v.SetFloat(float64(f)) | ||
270 | return nil | ||
271 | } | ||
272 | |||
273 | func decodeFloat64Value(d *Decoder, v reflect.Value) error { | ||
274 | f, err := d.DecodeFloat64() | ||
275 | if err != nil { | ||
276 | return err | ||
277 | } | ||
278 | if err = mustSet(v); err != nil { | ||
279 | return err | ||
280 | } | ||
281 | v.SetFloat(f) | ||
282 | return nil | ||
283 | } | ||
284 | |||
285 | func decodeInt64Value(d *Decoder, v reflect.Value) error { | ||
286 | n, err := d.DecodeInt64() | ||
287 | if err != nil { | ||
288 | return err | ||
289 | } | ||
290 | if err = mustSet(v); err != nil { | ||
291 | return err | ||
292 | } | ||
293 | v.SetInt(n) | ||
294 | return nil | ||
295 | } | ||
296 | |||
297 | func decodeUint64Value(d *Decoder, v reflect.Value) error { | ||
298 | n, err := d.DecodeUint64() | ||
299 | if err != nil { | ||
300 | return err | ||
301 | } | ||
302 | if err = mustSet(v); err != nil { | ||
303 | return err | ||
304 | } | ||
305 | v.SetUint(n) | ||
306 | return nil | ||
307 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/decode_query.go b/vendor/github.com/vmihailenco/msgpack/decode_query.go new file mode 100644 index 0000000..d680be8 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/decode_query.go | |||
@@ -0,0 +1,158 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "strconv" | ||
6 | "strings" | ||
7 | |||
8 | "github.com/vmihailenco/msgpack/codes" | ||
9 | ) | ||
10 | |||
11 | type queryResult struct { | ||
12 | query string | ||
13 | key string | ||
14 | hasAsterisk bool | ||
15 | |||
16 | values []interface{} | ||
17 | } | ||
18 | |||
19 | func (q *queryResult) nextKey() { | ||
20 | ind := strings.IndexByte(q.query, '.') | ||
21 | if ind == -1 { | ||
22 | q.key = q.query | ||
23 | q.query = "" | ||
24 | return | ||
25 | } | ||
26 | q.key = q.query[:ind] | ||
27 | q.query = q.query[ind+1:] | ||
28 | } | ||
29 | |||
30 | // Query extracts data specified by the query from the msgpack stream skipping | ||
31 | // any other data. Query consists of map keys and array indexes separated with dot, | ||
32 | // e.g. key1.0.key2. | ||
33 | func (d *Decoder) Query(query string) ([]interface{}, error) { | ||
34 | res := queryResult{ | ||
35 | query: query, | ||
36 | } | ||
37 | if err := d.query(&res); err != nil { | ||
38 | return nil, err | ||
39 | } | ||
40 | return res.values, nil | ||
41 | } | ||
42 | |||
43 | func (d *Decoder) query(q *queryResult) error { | ||
44 | q.nextKey() | ||
45 | if q.key == "" { | ||
46 | v, err := d.decodeInterfaceCond() | ||
47 | if err != nil { | ||
48 | return err | ||
49 | } | ||
50 | q.values = append(q.values, v) | ||
51 | return nil | ||
52 | } | ||
53 | |||
54 | code, err := d.PeekCode() | ||
55 | if err != nil { | ||
56 | return err | ||
57 | } | ||
58 | |||
59 | switch { | ||
60 | case code == codes.Map16 || code == codes.Map32 || codes.IsFixedMap(code): | ||
61 | err = d.queryMapKey(q) | ||
62 | case code == codes.Array16 || code == codes.Array32 || codes.IsFixedArray(code): | ||
63 | err = d.queryArrayIndex(q) | ||
64 | default: | ||
65 | err = fmt.Errorf("msgpack: unsupported code=%x decoding key=%q", code, q.key) | ||
66 | } | ||
67 | return err | ||
68 | } | ||
69 | |||
70 | func (d *Decoder) queryMapKey(q *queryResult) error { | ||
71 | n, err := d.DecodeMapLen() | ||
72 | if err != nil { | ||
73 | return err | ||
74 | } | ||
75 | if n == -1 { | ||
76 | return nil | ||
77 | } | ||
78 | |||
79 | for i := 0; i < n; i++ { | ||
80 | k, err := d.bytesNoCopy() | ||
81 | if err != nil { | ||
82 | return err | ||
83 | } | ||
84 | |||
85 | if string(k) == q.key { | ||
86 | if err := d.query(q); err != nil { | ||
87 | return err | ||
88 | } | ||
89 | if q.hasAsterisk { | ||
90 | return d.skipNext((n - i - 1) * 2) | ||
91 | } | ||
92 | return nil | ||
93 | } | ||
94 | |||
95 | if err := d.Skip(); err != nil { | ||
96 | return err | ||
97 | } | ||
98 | } | ||
99 | |||
100 | return nil | ||
101 | } | ||
102 | |||
103 | func (d *Decoder) queryArrayIndex(q *queryResult) error { | ||
104 | n, err := d.DecodeArrayLen() | ||
105 | if err != nil { | ||
106 | return err | ||
107 | } | ||
108 | if n == -1 { | ||
109 | return nil | ||
110 | } | ||
111 | |||
112 | if q.key == "*" { | ||
113 | q.hasAsterisk = true | ||
114 | |||
115 | query := q.query | ||
116 | for i := 0; i < n; i++ { | ||
117 | q.query = query | ||
118 | if err := d.query(q); err != nil { | ||
119 | return err | ||
120 | } | ||
121 | } | ||
122 | |||
123 | q.hasAsterisk = false | ||
124 | return nil | ||
125 | } | ||
126 | |||
127 | ind, err := strconv.Atoi(q.key) | ||
128 | if err != nil { | ||
129 | return err | ||
130 | } | ||
131 | |||
132 | for i := 0; i < n; i++ { | ||
133 | if i == ind { | ||
134 | if err := d.query(q); err != nil { | ||
135 | return err | ||
136 | } | ||
137 | if q.hasAsterisk { | ||
138 | return d.skipNext(n - i - 1) | ||
139 | } | ||
140 | return nil | ||
141 | } | ||
142 | |||
143 | if err := d.Skip(); err != nil { | ||
144 | return err | ||
145 | } | ||
146 | } | ||
147 | |||
148 | return nil | ||
149 | } | ||
150 | |||
151 | func (d *Decoder) skipNext(n int) error { | ||
152 | for i := 0; i < n; i++ { | ||
153 | if err := d.Skip(); err != nil { | ||
154 | return err | ||
155 | } | ||
156 | } | ||
157 | return nil | ||
158 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/decode_slice.go b/vendor/github.com/vmihailenco/msgpack/decode_slice.go new file mode 100644 index 0000000..778d80b --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/decode_slice.go | |||
@@ -0,0 +1,193 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "reflect" | ||
6 | |||
7 | "github.com/vmihailenco/msgpack/codes" | ||
8 | ) | ||
9 | |||
10 | const sliceElemsAllocLimit = 1e4 | ||
11 | |||
12 | var sliceStringPtrType = reflect.TypeOf((*[]string)(nil)) | ||
13 | |||
14 | // DecodeArrayLen decodes array length. Length is -1 when array is nil. | ||
15 | func (d *Decoder) DecodeArrayLen() (int, error) { | ||
16 | c, err := d.readCode() | ||
17 | if err != nil { | ||
18 | return 0, err | ||
19 | } | ||
20 | return d.arrayLen(c) | ||
21 | } | ||
22 | |||
23 | func (d *Decoder) arrayLen(c codes.Code) (int, error) { | ||
24 | if c == codes.Nil { | ||
25 | return -1, nil | ||
26 | } else if c >= codes.FixedArrayLow && c <= codes.FixedArrayHigh { | ||
27 | return int(c & codes.FixedArrayMask), nil | ||
28 | } | ||
29 | switch c { | ||
30 | case codes.Array16: | ||
31 | n, err := d.uint16() | ||
32 | return int(n), err | ||
33 | case codes.Array32: | ||
34 | n, err := d.uint32() | ||
35 | return int(n), err | ||
36 | } | ||
37 | return 0, fmt.Errorf("msgpack: invalid code=%x decoding array length", c) | ||
38 | } | ||
39 | |||
40 | func decodeStringSliceValue(d *Decoder, v reflect.Value) error { | ||
41 | ptr := v.Addr().Convert(sliceStringPtrType).Interface().(*[]string) | ||
42 | return d.decodeStringSlicePtr(ptr) | ||
43 | } | ||
44 | |||
45 | func (d *Decoder) decodeStringSlicePtr(ptr *[]string) error { | ||
46 | n, err := d.DecodeArrayLen() | ||
47 | if err != nil { | ||
48 | return err | ||
49 | } | ||
50 | if n == -1 { | ||
51 | return nil | ||
52 | } | ||
53 | |||
54 | ss := setStringsCap(*ptr, n) | ||
55 | for i := 0; i < n; i++ { | ||
56 | s, err := d.DecodeString() | ||
57 | if err != nil { | ||
58 | return err | ||
59 | } | ||
60 | ss = append(ss, s) | ||
61 | } | ||
62 | *ptr = ss | ||
63 | |||
64 | return nil | ||
65 | } | ||
66 | |||
67 | func setStringsCap(s []string, n int) []string { | ||
68 | if n > sliceElemsAllocLimit { | ||
69 | n = sliceElemsAllocLimit | ||
70 | } | ||
71 | |||
72 | if s == nil { | ||
73 | return make([]string, 0, n) | ||
74 | } | ||
75 | |||
76 | if cap(s) >= n { | ||
77 | return s[:0] | ||
78 | } | ||
79 | |||
80 | s = s[:cap(s)] | ||
81 | s = append(s, make([]string, n-len(s))...) | ||
82 | return s[:0] | ||
83 | } | ||
84 | |||
85 | func decodeSliceValue(d *Decoder, v reflect.Value) error { | ||
86 | n, err := d.DecodeArrayLen() | ||
87 | if err != nil { | ||
88 | return err | ||
89 | } | ||
90 | |||
91 | if n == -1 { | ||
92 | v.Set(reflect.Zero(v.Type())) | ||
93 | return nil | ||
94 | } | ||
95 | if n == 0 && v.IsNil() { | ||
96 | v.Set(reflect.MakeSlice(v.Type(), 0, 0)) | ||
97 | return nil | ||
98 | } | ||
99 | |||
100 | if v.Cap() >= n { | ||
101 | v.Set(v.Slice(0, n)) | ||
102 | } else if v.Len() < v.Cap() { | ||
103 | v.Set(v.Slice(0, v.Cap())) | ||
104 | } | ||
105 | |||
106 | for i := 0; i < n; i++ { | ||
107 | if i >= v.Len() { | ||
108 | v.Set(growSliceValue(v, n)) | ||
109 | } | ||
110 | sv := v.Index(i) | ||
111 | if err := d.DecodeValue(sv); err != nil { | ||
112 | return err | ||
113 | } | ||
114 | } | ||
115 | |||
116 | return nil | ||
117 | } | ||
118 | |||
119 | func growSliceValue(v reflect.Value, n int) reflect.Value { | ||
120 | diff := n - v.Len() | ||
121 | if diff > sliceElemsAllocLimit { | ||
122 | diff = sliceElemsAllocLimit | ||
123 | } | ||
124 | v = reflect.AppendSlice(v, reflect.MakeSlice(v.Type(), diff, diff)) | ||
125 | return v | ||
126 | } | ||
127 | |||
128 | func decodeArrayValue(d *Decoder, v reflect.Value) error { | ||
129 | n, err := d.DecodeArrayLen() | ||
130 | if err != nil { | ||
131 | return err | ||
132 | } | ||
133 | |||
134 | if n == -1 { | ||
135 | return nil | ||
136 | } | ||
137 | |||
138 | if n > v.Len() { | ||
139 | return fmt.Errorf("%s len is %d, but msgpack has %d elements", v.Type(), v.Len(), n) | ||
140 | } | ||
141 | for i := 0; i < n; i++ { | ||
142 | sv := v.Index(i) | ||
143 | if err := d.DecodeValue(sv); err != nil { | ||
144 | return err | ||
145 | } | ||
146 | } | ||
147 | |||
148 | return nil | ||
149 | } | ||
150 | |||
151 | func (d *Decoder) DecodeSlice() ([]interface{}, error) { | ||
152 | c, err := d.readCode() | ||
153 | if err != nil { | ||
154 | return nil, err | ||
155 | } | ||
156 | return d.decodeSlice(c) | ||
157 | } | ||
158 | |||
159 | func (d *Decoder) decodeSlice(c codes.Code) ([]interface{}, error) { | ||
160 | n, err := d.arrayLen(c) | ||
161 | if err != nil { | ||
162 | return nil, err | ||
163 | } | ||
164 | if n == -1 { | ||
165 | return nil, nil | ||
166 | } | ||
167 | |||
168 | s := make([]interface{}, 0, min(n, sliceElemsAllocLimit)) | ||
169 | for i := 0; i < n; i++ { | ||
170 | v, err := d.decodeInterfaceCond() | ||
171 | if err != nil { | ||
172 | return nil, err | ||
173 | } | ||
174 | s = append(s, v) | ||
175 | } | ||
176 | |||
177 | return s, nil | ||
178 | } | ||
179 | |||
180 | func (d *Decoder) skipSlice(c codes.Code) error { | ||
181 | n, err := d.arrayLen(c) | ||
182 | if err != nil { | ||
183 | return err | ||
184 | } | ||
185 | |||
186 | for i := 0; i < n; i++ { | ||
187 | if err := d.Skip(); err != nil { | ||
188 | return err | ||
189 | } | ||
190 | } | ||
191 | |||
192 | return nil | ||
193 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/decode_string.go b/vendor/github.com/vmihailenco/msgpack/decode_string.go new file mode 100644 index 0000000..5402022 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/decode_string.go | |||
@@ -0,0 +1,175 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "reflect" | ||
6 | |||
7 | "github.com/vmihailenco/msgpack/codes" | ||
8 | ) | ||
9 | |||
10 | func (d *Decoder) bytesLen(c codes.Code) (int, error) { | ||
11 | if c == codes.Nil { | ||
12 | return -1, nil | ||
13 | } else if codes.IsFixedString(c) { | ||
14 | return int(c & codes.FixedStrMask), nil | ||
15 | } | ||
16 | switch c { | ||
17 | case codes.Str8, codes.Bin8: | ||
18 | n, err := d.uint8() | ||
19 | return int(n), err | ||
20 | case codes.Str16, codes.Bin16: | ||
21 | n, err := d.uint16() | ||
22 | return int(n), err | ||
23 | case codes.Str32, codes.Bin32: | ||
24 | n, err := d.uint32() | ||
25 | return int(n), err | ||
26 | } | ||
27 | return 0, fmt.Errorf("msgpack: invalid code=%x decoding bytes length", c) | ||
28 | } | ||
29 | |||
30 | func (d *Decoder) DecodeString() (string, error) { | ||
31 | c, err := d.readCode() | ||
32 | if err != nil { | ||
33 | return "", err | ||
34 | } | ||
35 | return d.string(c) | ||
36 | } | ||
37 | |||
38 | func (d *Decoder) string(c codes.Code) (string, error) { | ||
39 | n, err := d.bytesLen(c) | ||
40 | if err != nil { | ||
41 | return "", err | ||
42 | } | ||
43 | if n == -1 { | ||
44 | return "", nil | ||
45 | } | ||
46 | b, err := d.readN(n) | ||
47 | return string(b), err | ||
48 | } | ||
49 | |||
50 | func decodeStringValue(d *Decoder, v reflect.Value) error { | ||
51 | s, err := d.DecodeString() | ||
52 | if err != nil { | ||
53 | return err | ||
54 | } | ||
55 | if err = mustSet(v); err != nil { | ||
56 | return err | ||
57 | } | ||
58 | v.SetString(s) | ||
59 | return nil | ||
60 | } | ||
61 | |||
62 | func (d *Decoder) DecodeBytesLen() (int, error) { | ||
63 | c, err := d.readCode() | ||
64 | if err != nil { | ||
65 | return 0, err | ||
66 | } | ||
67 | return d.bytesLen(c) | ||
68 | } | ||
69 | |||
70 | func (d *Decoder) DecodeBytes() ([]byte, error) { | ||
71 | c, err := d.readCode() | ||
72 | if err != nil { | ||
73 | return nil, err | ||
74 | } | ||
75 | return d.bytes(c, nil) | ||
76 | } | ||
77 | |||
78 | func (d *Decoder) bytes(c codes.Code, b []byte) ([]byte, error) { | ||
79 | n, err := d.bytesLen(c) | ||
80 | if err != nil { | ||
81 | return nil, err | ||
82 | } | ||
83 | if n == -1 { | ||
84 | return nil, nil | ||
85 | } | ||
86 | return readN(d.r, b, n) | ||
87 | } | ||
88 | |||
89 | func (d *Decoder) bytesNoCopy() ([]byte, error) { | ||
90 | c, err := d.readCode() | ||
91 | if err != nil { | ||
92 | return nil, err | ||
93 | } | ||
94 | n, err := d.bytesLen(c) | ||
95 | if err != nil { | ||
96 | return nil, err | ||
97 | } | ||
98 | if n == -1 { | ||
99 | return nil, nil | ||
100 | } | ||
101 | return d.readN(n) | ||
102 | } | ||
103 | |||
104 | func (d *Decoder) decodeBytesPtr(ptr *[]byte) error { | ||
105 | c, err := d.readCode() | ||
106 | if err != nil { | ||
107 | return err | ||
108 | } | ||
109 | return d.bytesPtr(c, ptr) | ||
110 | } | ||
111 | |||
112 | func (d *Decoder) bytesPtr(c codes.Code, ptr *[]byte) error { | ||
113 | n, err := d.bytesLen(c) | ||
114 | if err != nil { | ||
115 | return err | ||
116 | } | ||
117 | if n == -1 { | ||
118 | *ptr = nil | ||
119 | return nil | ||
120 | } | ||
121 | |||
122 | *ptr, err = readN(d.r, *ptr, n) | ||
123 | return err | ||
124 | } | ||
125 | |||
126 | func (d *Decoder) skipBytes(c codes.Code) error { | ||
127 | n, err := d.bytesLen(c) | ||
128 | if err != nil { | ||
129 | return err | ||
130 | } | ||
131 | if n == -1 { | ||
132 | return nil | ||
133 | } | ||
134 | return d.skipN(n) | ||
135 | } | ||
136 | |||
137 | func decodeBytesValue(d *Decoder, v reflect.Value) error { | ||
138 | c, err := d.readCode() | ||
139 | if err != nil { | ||
140 | return err | ||
141 | } | ||
142 | |||
143 | b, err := d.bytes(c, v.Bytes()) | ||
144 | if err != nil { | ||
145 | return err | ||
146 | } | ||
147 | |||
148 | if err = mustSet(v); err != nil { | ||
149 | return err | ||
150 | } | ||
151 | v.SetBytes(b) | ||
152 | |||
153 | return nil | ||
154 | } | ||
155 | |||
156 | func decodeByteArrayValue(d *Decoder, v reflect.Value) error { | ||
157 | c, err := d.readCode() | ||
158 | if err != nil { | ||
159 | return err | ||
160 | } | ||
161 | |||
162 | n, err := d.bytesLen(c) | ||
163 | if err != nil { | ||
164 | return err | ||
165 | } | ||
166 | if n == -1 { | ||
167 | return nil | ||
168 | } | ||
169 | if n > v.Len() { | ||
170 | return fmt.Errorf("%s len is %d, but msgpack has %d elements", v.Type(), v.Len(), n) | ||
171 | } | ||
172 | |||
173 | b := v.Slice(0, n).Bytes() | ||
174 | return d.readFull(b) | ||
175 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/decode_value.go b/vendor/github.com/vmihailenco/msgpack/decode_value.go new file mode 100644 index 0000000..7b858b5 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/decode_value.go | |||
@@ -0,0 +1,234 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "errors" | ||
5 | "fmt" | ||
6 | "reflect" | ||
7 | ) | ||
8 | |||
9 | var interfaceType = reflect.TypeOf((*interface{})(nil)).Elem() | ||
10 | var stringType = reflect.TypeOf((*string)(nil)).Elem() | ||
11 | |||
12 | var valueDecoders []decoderFunc | ||
13 | |||
14 | func init() { | ||
15 | valueDecoders = []decoderFunc{ | ||
16 | reflect.Bool: decodeBoolValue, | ||
17 | reflect.Int: decodeInt64Value, | ||
18 | reflect.Int8: decodeInt64Value, | ||
19 | reflect.Int16: decodeInt64Value, | ||
20 | reflect.Int32: decodeInt64Value, | ||
21 | reflect.Int64: decodeInt64Value, | ||
22 | reflect.Uint: decodeUint64Value, | ||
23 | reflect.Uint8: decodeUint64Value, | ||
24 | reflect.Uint16: decodeUint64Value, | ||
25 | reflect.Uint32: decodeUint64Value, | ||
26 | reflect.Uint64: decodeUint64Value, | ||
27 | reflect.Float32: decodeFloat32Value, | ||
28 | reflect.Float64: decodeFloat64Value, | ||
29 | reflect.Complex64: decodeUnsupportedValue, | ||
30 | reflect.Complex128: decodeUnsupportedValue, | ||
31 | reflect.Array: decodeArrayValue, | ||
32 | reflect.Chan: decodeUnsupportedValue, | ||
33 | reflect.Func: decodeUnsupportedValue, | ||
34 | reflect.Interface: decodeInterfaceValue, | ||
35 | reflect.Map: decodeMapValue, | ||
36 | reflect.Ptr: decodeUnsupportedValue, | ||
37 | reflect.Slice: decodeSliceValue, | ||
38 | reflect.String: decodeStringValue, | ||
39 | reflect.Struct: decodeStructValue, | ||
40 | reflect.UnsafePointer: decodeUnsupportedValue, | ||
41 | } | ||
42 | } | ||
43 | |||
44 | func mustSet(v reflect.Value) error { | ||
45 | if !v.CanSet() { | ||
46 | return fmt.Errorf("msgpack: Decode(nonsettable %s)", v.Type()) | ||
47 | } | ||
48 | return nil | ||
49 | } | ||
50 | |||
51 | func getDecoder(typ reflect.Type) decoderFunc { | ||
52 | kind := typ.Kind() | ||
53 | |||
54 | decoder, ok := typDecMap[typ] | ||
55 | if ok { | ||
56 | return decoder | ||
57 | } | ||
58 | |||
59 | if typ.Implements(customDecoderType) { | ||
60 | return decodeCustomValue | ||
61 | } | ||
62 | if typ.Implements(unmarshalerType) { | ||
63 | return unmarshalValue | ||
64 | } | ||
65 | |||
66 | // Addressable struct field value. | ||
67 | if kind != reflect.Ptr { | ||
68 | ptr := reflect.PtrTo(typ) | ||
69 | if ptr.Implements(customDecoderType) { | ||
70 | return decodeCustomValueAddr | ||
71 | } | ||
72 | if ptr.Implements(unmarshalerType) { | ||
73 | return unmarshalValueAddr | ||
74 | } | ||
75 | } | ||
76 | |||
77 | switch kind { | ||
78 | case reflect.Ptr: | ||
79 | return ptrDecoderFunc(typ) | ||
80 | case reflect.Slice: | ||
81 | elem := typ.Elem() | ||
82 | switch elem.Kind() { | ||
83 | case reflect.Uint8: | ||
84 | return decodeBytesValue | ||
85 | } | ||
86 | switch elem { | ||
87 | case stringType: | ||
88 | return decodeStringSliceValue | ||
89 | } | ||
90 | case reflect.Array: | ||
91 | if typ.Elem().Kind() == reflect.Uint8 { | ||
92 | return decodeByteArrayValue | ||
93 | } | ||
94 | case reflect.Map: | ||
95 | if typ.Key() == stringType { | ||
96 | switch typ.Elem() { | ||
97 | case stringType: | ||
98 | return decodeMapStringStringValue | ||
99 | case interfaceType: | ||
100 | return decodeMapStringInterfaceValue | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | return valueDecoders[kind] | ||
105 | } | ||
106 | |||
107 | func ptrDecoderFunc(typ reflect.Type) decoderFunc { | ||
108 | decoder := getDecoder(typ.Elem()) | ||
109 | return func(d *Decoder, v reflect.Value) error { | ||
110 | if d.hasNilCode() { | ||
111 | if err := mustSet(v); err != nil { | ||
112 | return err | ||
113 | } | ||
114 | if !v.IsNil() { | ||
115 | v.Set(reflect.Zero(v.Type())) | ||
116 | } | ||
117 | return d.DecodeNil() | ||
118 | } | ||
119 | if v.IsNil() { | ||
120 | if err := mustSet(v); err != nil { | ||
121 | return err | ||
122 | } | ||
123 | v.Set(reflect.New(v.Type().Elem())) | ||
124 | } | ||
125 | return decoder(d, v.Elem()) | ||
126 | } | ||
127 | } | ||
128 | |||
129 | func decodeCustomValueAddr(d *Decoder, v reflect.Value) error { | ||
130 | if !v.CanAddr() { | ||
131 | return fmt.Errorf("msgpack: Decode(nonaddressable %T)", v.Interface()) | ||
132 | } | ||
133 | return decodeCustomValue(d, v.Addr()) | ||
134 | } | ||
135 | |||
136 | func decodeCustomValue(d *Decoder, v reflect.Value) error { | ||
137 | if d.hasNilCode() { | ||
138 | return d.decodeNilValue(v) | ||
139 | } | ||
140 | |||
141 | if v.IsNil() { | ||
142 | v.Set(reflect.New(v.Type().Elem())) | ||
143 | } | ||
144 | |||
145 | decoder := v.Interface().(CustomDecoder) | ||
146 | return decoder.DecodeMsgpack(d) | ||
147 | } | ||
148 | |||
149 | func unmarshalValueAddr(d *Decoder, v reflect.Value) error { | ||
150 | if !v.CanAddr() { | ||
151 | return fmt.Errorf("msgpack: Decode(nonaddressable %T)", v.Interface()) | ||
152 | } | ||
153 | return unmarshalValue(d, v.Addr()) | ||
154 | } | ||
155 | |||
156 | func unmarshalValue(d *Decoder, v reflect.Value) error { | ||
157 | if d.hasNilCode() { | ||
158 | return d.decodeNilValue(v) | ||
159 | } | ||
160 | |||
161 | if v.IsNil() { | ||
162 | v.Set(reflect.New(v.Type().Elem())) | ||
163 | } | ||
164 | |||
165 | if d.extLen != 0 { | ||
166 | b, err := d.readN(d.extLen) | ||
167 | if err != nil { | ||
168 | return err | ||
169 | } | ||
170 | d.rec = b | ||
171 | } else { | ||
172 | d.rec = makeBuffer() | ||
173 | if err := d.Skip(); err != nil { | ||
174 | return err | ||
175 | } | ||
176 | } | ||
177 | |||
178 | unmarshaler := v.Interface().(Unmarshaler) | ||
179 | err := unmarshaler.UnmarshalMsgpack(d.rec) | ||
180 | d.rec = nil | ||
181 | return err | ||
182 | } | ||
183 | |||
184 | func decodeBoolValue(d *Decoder, v reflect.Value) error { | ||
185 | flag, err := d.DecodeBool() | ||
186 | if err != nil { | ||
187 | return err | ||
188 | } | ||
189 | if err = mustSet(v); err != nil { | ||
190 | return err | ||
191 | } | ||
192 | v.SetBool(flag) | ||
193 | return nil | ||
194 | } | ||
195 | |||
196 | func decodeInterfaceValue(d *Decoder, v reflect.Value) error { | ||
197 | if v.IsNil() { | ||
198 | return d.interfaceValue(v) | ||
199 | } | ||
200 | |||
201 | elem := v.Elem() | ||
202 | if !elem.CanAddr() { | ||
203 | if d.hasNilCode() { | ||
204 | v.Set(reflect.Zero(v.Type())) | ||
205 | return d.DecodeNil() | ||
206 | } | ||
207 | } | ||
208 | |||
209 | return d.DecodeValue(elem) | ||
210 | } | ||
211 | |||
212 | func (d *Decoder) interfaceValue(v reflect.Value) error { | ||
213 | vv, err := d.decodeInterfaceCond() | ||
214 | if err != nil { | ||
215 | return err | ||
216 | } | ||
217 | |||
218 | if vv != nil { | ||
219 | if v.Type() == errorType { | ||
220 | if vv, ok := vv.(string); ok { | ||
221 | v.Set(reflect.ValueOf(errors.New(vv))) | ||
222 | return nil | ||
223 | } | ||
224 | } | ||
225 | |||
226 | v.Set(reflect.ValueOf(vv)) | ||
227 | } | ||
228 | |||
229 | return nil | ||
230 | } | ||
231 | |||
232 | func decodeUnsupportedValue(d *Decoder, v reflect.Value) error { | ||
233 | return fmt.Errorf("msgpack: Decode(unsupported %s)", v.Type()) | ||
234 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/encode.go b/vendor/github.com/vmihailenco/msgpack/encode.go new file mode 100644 index 0000000..c2bb23c --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/encode.go | |||
@@ -0,0 +1,170 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "io" | ||
6 | "reflect" | ||
7 | "time" | ||
8 | |||
9 | "github.com/vmihailenco/msgpack/codes" | ||
10 | ) | ||
11 | |||
12 | type writer interface { | ||
13 | io.Writer | ||
14 | WriteByte(byte) error | ||
15 | WriteString(string) (int, error) | ||
16 | } | ||
17 | |||
18 | type byteWriter struct { | ||
19 | io.Writer | ||
20 | |||
21 | buf []byte | ||
22 | bootstrap [64]byte | ||
23 | } | ||
24 | |||
25 | func newByteWriter(w io.Writer) *byteWriter { | ||
26 | bw := &byteWriter{ | ||
27 | Writer: w, | ||
28 | } | ||
29 | bw.buf = bw.bootstrap[:] | ||
30 | return bw | ||
31 | } | ||
32 | |||
33 | func (w *byteWriter) WriteByte(c byte) error { | ||
34 | w.buf = w.buf[:1] | ||
35 | w.buf[0] = c | ||
36 | _, err := w.Write(w.buf) | ||
37 | return err | ||
38 | } | ||
39 | |||
40 | func (w *byteWriter) WriteString(s string) (int, error) { | ||
41 | w.buf = append(w.buf[:0], s...) | ||
42 | return w.Write(w.buf) | ||
43 | } | ||
44 | |||
45 | // Marshal returns the MessagePack encoding of v. | ||
46 | func Marshal(v interface{}) ([]byte, error) { | ||
47 | var buf bytes.Buffer | ||
48 | err := NewEncoder(&buf).Encode(v) | ||
49 | return buf.Bytes(), err | ||
50 | } | ||
51 | |||
52 | type Encoder struct { | ||
53 | w writer | ||
54 | buf []byte | ||
55 | |||
56 | sortMapKeys bool | ||
57 | structAsArray bool | ||
58 | useJSONTag bool | ||
59 | useCompact bool | ||
60 | } | ||
61 | |||
62 | // NewEncoder returns a new encoder that writes to w. | ||
63 | func NewEncoder(w io.Writer) *Encoder { | ||
64 | bw, ok := w.(writer) | ||
65 | if !ok { | ||
66 | bw = newByteWriter(w) | ||
67 | } | ||
68 | return &Encoder{ | ||
69 | w: bw, | ||
70 | buf: make([]byte, 9), | ||
71 | } | ||
72 | } | ||
73 | |||
74 | // SortMapKeys causes the Encoder to encode map keys in increasing order. | ||
75 | // Supported map types are: | ||
76 | // - map[string]string | ||
77 | // - map[string]interface{} | ||
78 | func (e *Encoder) SortMapKeys(flag bool) *Encoder { | ||
79 | e.sortMapKeys = flag | ||
80 | return e | ||
81 | } | ||
82 | |||
83 | // StructAsArray causes the Encoder to encode Go structs as MessagePack arrays. | ||
84 | func (e *Encoder) StructAsArray(flag bool) *Encoder { | ||
85 | e.structAsArray = flag | ||
86 | return e | ||
87 | } | ||
88 | |||
89 | // UseJSONTag causes the Encoder to use json struct tag as fallback option | ||
90 | // if there is no msgpack tag. | ||
91 | func (e *Encoder) UseJSONTag(flag bool) *Encoder { | ||
92 | e.useJSONTag = flag | ||
93 | return e | ||
94 | } | ||
95 | |||
96 | // UseCompactEncoding causes the Encoder to chose the most compact encoding. | ||
97 | // For example, it allows to encode Go int64 as msgpack int8 saving 7 bytes. | ||
98 | func (e *Encoder) UseCompactEncoding(flag bool) *Encoder { | ||
99 | e.useCompact = flag | ||
100 | return e | ||
101 | } | ||
102 | |||
103 | func (e *Encoder) Encode(v interface{}) error { | ||
104 | switch v := v.(type) { | ||
105 | case nil: | ||
106 | return e.EncodeNil() | ||
107 | case string: | ||
108 | return e.EncodeString(v) | ||
109 | case []byte: | ||
110 | return e.EncodeBytes(v) | ||
111 | case int: | ||
112 | return e.encodeInt64Cond(int64(v)) | ||
113 | case int64: | ||
114 | return e.encodeInt64Cond(v) | ||
115 | case uint: | ||
116 | return e.encodeUint64Cond(uint64(v)) | ||
117 | case uint64: | ||
118 | return e.encodeUint64Cond(v) | ||
119 | case bool: | ||
120 | return e.EncodeBool(v) | ||
121 | case float32: | ||
122 | return e.EncodeFloat32(v) | ||
123 | case float64: | ||
124 | return e.EncodeFloat64(v) | ||
125 | case time.Duration: | ||
126 | return e.encodeInt64Cond(int64(v)) | ||
127 | case time.Time: | ||
128 | return e.EncodeTime(v) | ||
129 | } | ||
130 | return e.EncodeValue(reflect.ValueOf(v)) | ||
131 | } | ||
132 | |||
133 | func (e *Encoder) EncodeMulti(v ...interface{}) error { | ||
134 | for _, vv := range v { | ||
135 | if err := e.Encode(vv); err != nil { | ||
136 | return err | ||
137 | } | ||
138 | } | ||
139 | return nil | ||
140 | } | ||
141 | |||
142 | func (e *Encoder) EncodeValue(v reflect.Value) error { | ||
143 | fn := getEncoder(v.Type()) | ||
144 | return fn(e, v) | ||
145 | } | ||
146 | |||
147 | func (e *Encoder) EncodeNil() error { | ||
148 | return e.writeCode(codes.Nil) | ||
149 | } | ||
150 | |||
151 | func (e *Encoder) EncodeBool(value bool) error { | ||
152 | if value { | ||
153 | return e.writeCode(codes.True) | ||
154 | } | ||
155 | return e.writeCode(codes.False) | ||
156 | } | ||
157 | |||
158 | func (e *Encoder) writeCode(c codes.Code) error { | ||
159 | return e.w.WriteByte(byte(c)) | ||
160 | } | ||
161 | |||
162 | func (e *Encoder) write(b []byte) error { | ||
163 | _, err := e.w.Write(b) | ||
164 | return err | ||
165 | } | ||
166 | |||
167 | func (e *Encoder) writeString(s string) error { | ||
168 | _, err := e.w.WriteString(s) | ||
169 | return err | ||
170 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/encode_map.go b/vendor/github.com/vmihailenco/msgpack/encode_map.go new file mode 100644 index 0000000..a87c407 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/encode_map.go | |||
@@ -0,0 +1,172 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "reflect" | ||
5 | "sort" | ||
6 | |||
7 | "github.com/vmihailenco/msgpack/codes" | ||
8 | ) | ||
9 | |||
10 | func encodeMapValue(e *Encoder, v reflect.Value) error { | ||
11 | if v.IsNil() { | ||
12 | return e.EncodeNil() | ||
13 | } | ||
14 | |||
15 | if err := e.EncodeMapLen(v.Len()); err != nil { | ||
16 | return err | ||
17 | } | ||
18 | |||
19 | for _, key := range v.MapKeys() { | ||
20 | if err := e.EncodeValue(key); err != nil { | ||
21 | return err | ||
22 | } | ||
23 | if err := e.EncodeValue(v.MapIndex(key)); err != nil { | ||
24 | return err | ||
25 | } | ||
26 | } | ||
27 | |||
28 | return nil | ||
29 | } | ||
30 | |||
31 | func encodeMapStringStringValue(e *Encoder, v reflect.Value) error { | ||
32 | if v.IsNil() { | ||
33 | return e.EncodeNil() | ||
34 | } | ||
35 | |||
36 | if err := e.EncodeMapLen(v.Len()); err != nil { | ||
37 | return err | ||
38 | } | ||
39 | |||
40 | m := v.Convert(mapStringStringType).Interface().(map[string]string) | ||
41 | if e.sortMapKeys { | ||
42 | return e.encodeSortedMapStringString(m) | ||
43 | } | ||
44 | |||
45 | for mk, mv := range m { | ||
46 | if err := e.EncodeString(mk); err != nil { | ||
47 | return err | ||
48 | } | ||
49 | if err := e.EncodeString(mv); err != nil { | ||
50 | return err | ||
51 | } | ||
52 | } | ||
53 | |||
54 | return nil | ||
55 | } | ||
56 | |||
57 | func encodeMapStringInterfaceValue(e *Encoder, v reflect.Value) error { | ||
58 | if v.IsNil() { | ||
59 | return e.EncodeNil() | ||
60 | } | ||
61 | |||
62 | if err := e.EncodeMapLen(v.Len()); err != nil { | ||
63 | return err | ||
64 | } | ||
65 | |||
66 | m := v.Convert(mapStringInterfaceType).Interface().(map[string]interface{}) | ||
67 | if e.sortMapKeys { | ||
68 | return e.encodeSortedMapStringInterface(m) | ||
69 | } | ||
70 | |||
71 | for mk, mv := range m { | ||
72 | if err := e.EncodeString(mk); err != nil { | ||
73 | return err | ||
74 | } | ||
75 | if err := e.Encode(mv); err != nil { | ||
76 | return err | ||
77 | } | ||
78 | } | ||
79 | |||
80 | return nil | ||
81 | } | ||
82 | |||
83 | func (e *Encoder) encodeSortedMapStringString(m map[string]string) error { | ||
84 | keys := make([]string, 0, len(m)) | ||
85 | for k, _ := range m { | ||
86 | keys = append(keys, k) | ||
87 | } | ||
88 | sort.Strings(keys) | ||
89 | |||
90 | for _, k := range keys { | ||
91 | err := e.EncodeString(k) | ||
92 | if err != nil { | ||
93 | return err | ||
94 | } | ||
95 | if err = e.EncodeString(m[k]); err != nil { | ||
96 | return err | ||
97 | } | ||
98 | } | ||
99 | |||
100 | return nil | ||
101 | } | ||
102 | |||
103 | func (e *Encoder) encodeSortedMapStringInterface(m map[string]interface{}) error { | ||
104 | keys := make([]string, 0, len(m)) | ||
105 | for k, _ := range m { | ||
106 | keys = append(keys, k) | ||
107 | } | ||
108 | sort.Strings(keys) | ||
109 | |||
110 | for _, k := range keys { | ||
111 | err := e.EncodeString(k) | ||
112 | if err != nil { | ||
113 | return err | ||
114 | } | ||
115 | if err = e.Encode(m[k]); err != nil { | ||
116 | return err | ||
117 | } | ||
118 | } | ||
119 | |||
120 | return nil | ||
121 | } | ||
122 | |||
123 | func (e *Encoder) EncodeMapLen(l int) error { | ||
124 | if l < 16 { | ||
125 | return e.writeCode(codes.FixedMapLow | codes.Code(l)) | ||
126 | } | ||
127 | if l < 65536 { | ||
128 | return e.write2(codes.Map16, uint16(l)) | ||
129 | } | ||
130 | return e.write4(codes.Map32, uint32(l)) | ||
131 | } | ||
132 | |||
133 | func encodeStructValue(e *Encoder, strct reflect.Value) error { | ||
134 | var structFields *fields | ||
135 | if e.useJSONTag { | ||
136 | structFields = jsonStructs.Fields(strct.Type()) | ||
137 | } else { | ||
138 | structFields = structs.Fields(strct.Type()) | ||
139 | } | ||
140 | |||
141 | if e.structAsArray || structFields.AsArray { | ||
142 | return encodeStructValueAsArray(e, strct, structFields.List) | ||
143 | } | ||
144 | fields := structFields.OmitEmpty(strct) | ||
145 | |||
146 | if err := e.EncodeMapLen(len(fields)); err != nil { | ||
147 | return err | ||
148 | } | ||
149 | |||
150 | for _, f := range fields { | ||
151 | if err := e.EncodeString(f.name); err != nil { | ||
152 | return err | ||
153 | } | ||
154 | if err := f.EncodeValue(e, strct); err != nil { | ||
155 | return err | ||
156 | } | ||
157 | } | ||
158 | |||
159 | return nil | ||
160 | } | ||
161 | |||
162 | func encodeStructValueAsArray(e *Encoder, strct reflect.Value, fields []*field) error { | ||
163 | if err := e.EncodeArrayLen(len(fields)); err != nil { | ||
164 | return err | ||
165 | } | ||
166 | for _, f := range fields { | ||
167 | if err := f.EncodeValue(e, strct); err != nil { | ||
168 | return err | ||
169 | } | ||
170 | } | ||
171 | return nil | ||
172 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/encode_number.go b/vendor/github.com/vmihailenco/msgpack/encode_number.go new file mode 100644 index 0000000..dd7db6f --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/encode_number.go | |||
@@ -0,0 +1,230 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "math" | ||
5 | "reflect" | ||
6 | |||
7 | "github.com/vmihailenco/msgpack/codes" | ||
8 | ) | ||
9 | |||
10 | // EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number. | ||
11 | func (e *Encoder) EncodeUint8(n uint8) error { | ||
12 | return e.write1(codes.Uint8, n) | ||
13 | } | ||
14 | |||
15 | func (e *Encoder) encodeUint8Cond(n uint8) error { | ||
16 | if e.useCompact { | ||
17 | return e.EncodeUint(uint64(n)) | ||
18 | } | ||
19 | return e.EncodeUint8(n) | ||
20 | } | ||
21 | |||
22 | // EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number. | ||
23 | func (e *Encoder) EncodeUint16(n uint16) error { | ||
24 | return e.write2(codes.Uint16, n) | ||
25 | } | ||
26 | |||
27 | func (e *Encoder) encodeUint16Cond(n uint16) error { | ||
28 | if e.useCompact { | ||
29 | return e.EncodeUint(uint64(n)) | ||
30 | } | ||
31 | return e.EncodeUint16(n) | ||
32 | } | ||
33 | |||
34 | // EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number. | ||
35 | func (e *Encoder) EncodeUint32(n uint32) error { | ||
36 | return e.write4(codes.Uint32, n) | ||
37 | } | ||
38 | |||
39 | func (e *Encoder) encodeUint32Cond(n uint32) error { | ||
40 | if e.useCompact { | ||
41 | return e.EncodeUint(uint64(n)) | ||
42 | } | ||
43 | return e.EncodeUint32(n) | ||
44 | } | ||
45 | |||
46 | // EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number. | ||
47 | func (e *Encoder) EncodeUint64(n uint64) error { | ||
48 | return e.write8(codes.Uint64, n) | ||
49 | } | ||
50 | |||
51 | func (e *Encoder) encodeUint64Cond(n uint64) error { | ||
52 | if e.useCompact { | ||
53 | return e.EncodeUint(n) | ||
54 | } | ||
55 | return e.EncodeUint64(n) | ||
56 | } | ||
57 | |||
58 | // EncodeInt8 encodes an int8 in 2 bytes preserving type of the number. | ||
59 | func (e *Encoder) EncodeInt8(n int8) error { | ||
60 | return e.write1(codes.Int8, uint8(n)) | ||
61 | } | ||
62 | |||
63 | func (e *Encoder) encodeInt8Cond(n int8) error { | ||
64 | if e.useCompact { | ||
65 | return e.EncodeInt(int64(n)) | ||
66 | } | ||
67 | return e.EncodeInt8(n) | ||
68 | } | ||
69 | |||
70 | // EncodeInt16 encodes an int16 in 3 bytes preserving type of the number. | ||
71 | func (e *Encoder) EncodeInt16(n int16) error { | ||
72 | return e.write2(codes.Int16, uint16(n)) | ||
73 | } | ||
74 | |||
75 | func (e *Encoder) encodeInt16Cond(n int16) error { | ||
76 | if e.useCompact { | ||
77 | return e.EncodeInt(int64(n)) | ||
78 | } | ||
79 | return e.EncodeInt16(n) | ||
80 | } | ||
81 | |||
82 | // EncodeInt32 encodes an int32 in 5 bytes preserving type of the number. | ||
83 | func (e *Encoder) EncodeInt32(n int32) error { | ||
84 | return e.write4(codes.Int32, uint32(n)) | ||
85 | } | ||
86 | |||
87 | func (e *Encoder) encodeInt32Cond(n int32) error { | ||
88 | if e.useCompact { | ||
89 | return e.EncodeInt(int64(n)) | ||
90 | } | ||
91 | return e.EncodeInt32(n) | ||
92 | } | ||
93 | |||
94 | // EncodeInt64 encodes an int64 in 9 bytes preserving type of the number. | ||
95 | func (e *Encoder) EncodeInt64(n int64) error { | ||
96 | return e.write8(codes.Int64, uint64(n)) | ||
97 | } | ||
98 | |||
99 | func (e *Encoder) encodeInt64Cond(n int64) error { | ||
100 | if e.useCompact { | ||
101 | return e.EncodeInt(n) | ||
102 | } | ||
103 | return e.EncodeInt64(n) | ||
104 | } | ||
105 | |||
106 | // EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes. | ||
107 | // Type of the number is lost during encoding. | ||
108 | func (e *Encoder) EncodeUint(n uint64) error { | ||
109 | if n <= math.MaxInt8 { | ||
110 | return e.w.WriteByte(byte(n)) | ||
111 | } | ||
112 | if n <= math.MaxUint8 { | ||
113 | return e.EncodeUint8(uint8(n)) | ||
114 | } | ||
115 | if n <= math.MaxUint16 { | ||
116 | return e.EncodeUint16(uint16(n)) | ||
117 | } | ||
118 | if n <= math.MaxUint32 { | ||
119 | return e.EncodeUint32(uint32(n)) | ||
120 | } | ||
121 | return e.EncodeUint64(uint64(n)) | ||
122 | } | ||
123 | |||
124 | // EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes. | ||
125 | // Type of number is lost during encoding. | ||
126 | func (e *Encoder) EncodeInt(n int64) error { | ||
127 | if n >= 0 { | ||
128 | return e.EncodeUint(uint64(n)) | ||
129 | } | ||
130 | if n >= int64(int8(codes.NegFixedNumLow)) { | ||
131 | return e.w.WriteByte(byte(n)) | ||
132 | } | ||
133 | if n >= math.MinInt8 { | ||
134 | return e.EncodeInt8(int8(n)) | ||
135 | } | ||
136 | if n >= math.MinInt16 { | ||
137 | return e.EncodeInt16(int16(n)) | ||
138 | } | ||
139 | if n >= math.MinInt32 { | ||
140 | return e.EncodeInt32(int32(n)) | ||
141 | } | ||
142 | return e.EncodeInt64(int64(n)) | ||
143 | } | ||
144 | |||
145 | func (e *Encoder) EncodeFloat32(n float32) error { | ||
146 | return e.write4(codes.Float, math.Float32bits(n)) | ||
147 | } | ||
148 | |||
149 | func (e *Encoder) EncodeFloat64(n float64) error { | ||
150 | return e.write8(codes.Double, math.Float64bits(n)) | ||
151 | } | ||
152 | |||
153 | func (e *Encoder) write1(code codes.Code, n uint8) error { | ||
154 | e.buf = e.buf[:2] | ||
155 | e.buf[0] = byte(code) | ||
156 | e.buf[1] = byte(n) | ||
157 | return e.write(e.buf) | ||
158 | } | ||
159 | |||
160 | func (e *Encoder) write2(code codes.Code, n uint16) error { | ||
161 | e.buf = e.buf[:3] | ||
162 | e.buf[0] = byte(code) | ||
163 | e.buf[1] = byte(n >> 8) | ||
164 | e.buf[2] = byte(n) | ||
165 | return e.write(e.buf) | ||
166 | } | ||
167 | |||
168 | func (e *Encoder) write4(code codes.Code, n uint32) error { | ||
169 | e.buf = e.buf[:5] | ||
170 | e.buf[0] = byte(code) | ||
171 | e.buf[1] = byte(n >> 24) | ||
172 | e.buf[2] = byte(n >> 16) | ||
173 | e.buf[3] = byte(n >> 8) | ||
174 | e.buf[4] = byte(n) | ||
175 | return e.write(e.buf) | ||
176 | } | ||
177 | |||
178 | func (e *Encoder) write8(code codes.Code, n uint64) error { | ||
179 | e.buf = e.buf[:9] | ||
180 | e.buf[0] = byte(code) | ||
181 | e.buf[1] = byte(n >> 56) | ||
182 | e.buf[2] = byte(n >> 48) | ||
183 | e.buf[3] = byte(n >> 40) | ||
184 | e.buf[4] = byte(n >> 32) | ||
185 | e.buf[5] = byte(n >> 24) | ||
186 | e.buf[6] = byte(n >> 16) | ||
187 | e.buf[7] = byte(n >> 8) | ||
188 | e.buf[8] = byte(n) | ||
189 | return e.write(e.buf) | ||
190 | } | ||
191 | |||
192 | func encodeUint8CondValue(e *Encoder, v reflect.Value) error { | ||
193 | return e.encodeUint8Cond(uint8(v.Uint())) | ||
194 | } | ||
195 | |||
196 | func encodeUint16CondValue(e *Encoder, v reflect.Value) error { | ||
197 | return e.encodeUint16Cond(uint16(v.Uint())) | ||
198 | } | ||
199 | |||
200 | func encodeUint32CondValue(e *Encoder, v reflect.Value) error { | ||
201 | return e.encodeUint32Cond(uint32(v.Uint())) | ||
202 | } | ||
203 | |||
204 | func encodeUint64CondValue(e *Encoder, v reflect.Value) error { | ||
205 | return e.encodeUint64Cond(v.Uint()) | ||
206 | } | ||
207 | |||
208 | func encodeInt8CondValue(e *Encoder, v reflect.Value) error { | ||
209 | return e.encodeInt8Cond(int8(v.Int())) | ||
210 | } | ||
211 | |||
212 | func encodeInt16CondValue(e *Encoder, v reflect.Value) error { | ||
213 | return e.encodeInt16Cond(int16(v.Int())) | ||
214 | } | ||
215 | |||
216 | func encodeInt32CondValue(e *Encoder, v reflect.Value) error { | ||
217 | return e.encodeInt32Cond(int32(v.Int())) | ||
218 | } | ||
219 | |||
220 | func encodeInt64CondValue(e *Encoder, v reflect.Value) error { | ||
221 | return e.encodeInt64Cond(v.Int()) | ||
222 | } | ||
223 | |||
224 | func encodeFloat32Value(e *Encoder, v reflect.Value) error { | ||
225 | return e.EncodeFloat32(float32(v.Float())) | ||
226 | } | ||
227 | |||
228 | func encodeFloat64Value(e *Encoder, v reflect.Value) error { | ||
229 | return e.EncodeFloat64(v.Float()) | ||
230 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/encode_slice.go b/vendor/github.com/vmihailenco/msgpack/encode_slice.go new file mode 100644 index 0000000..5ddbd63 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/encode_slice.go | |||
@@ -0,0 +1,124 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "reflect" | ||
5 | |||
6 | "github.com/vmihailenco/msgpack/codes" | ||
7 | ) | ||
8 | |||
9 | func encodeStringValue(e *Encoder, v reflect.Value) error { | ||
10 | return e.EncodeString(v.String()) | ||
11 | } | ||
12 | |||
13 | func encodeByteSliceValue(e *Encoder, v reflect.Value) error { | ||
14 | return e.EncodeBytes(v.Bytes()) | ||
15 | } | ||
16 | |||
17 | func encodeByteArrayValue(e *Encoder, v reflect.Value) error { | ||
18 | if err := e.EncodeBytesLen(v.Len()); err != nil { | ||
19 | return err | ||
20 | } | ||
21 | |||
22 | if v.CanAddr() { | ||
23 | b := v.Slice(0, v.Len()).Bytes() | ||
24 | return e.write(b) | ||
25 | } | ||
26 | |||
27 | e.buf = grow(e.buf, v.Len()) | ||
28 | reflect.Copy(reflect.ValueOf(e.buf), v) | ||
29 | return e.write(e.buf) | ||
30 | } | ||
31 | |||
32 | func grow(b []byte, n int) []byte { | ||
33 | if cap(b) >= n { | ||
34 | return b[:n] | ||
35 | } | ||
36 | b = b[:cap(b)] | ||
37 | b = append(b, make([]byte, n-len(b))...) | ||
38 | return b | ||
39 | } | ||
40 | |||
41 | func (e *Encoder) EncodeBytesLen(l int) error { | ||
42 | if l < 256 { | ||
43 | return e.write1(codes.Bin8, uint8(l)) | ||
44 | } | ||
45 | if l < 65536 { | ||
46 | return e.write2(codes.Bin16, uint16(l)) | ||
47 | } | ||
48 | return e.write4(codes.Bin32, uint32(l)) | ||
49 | } | ||
50 | |||
51 | func (e *Encoder) encodeStrLen(l int) error { | ||
52 | if l < 32 { | ||
53 | return e.writeCode(codes.FixedStrLow | codes.Code(l)) | ||
54 | } | ||
55 | if l < 256 { | ||
56 | return e.write1(codes.Str8, uint8(l)) | ||
57 | } | ||
58 | if l < 65536 { | ||
59 | return e.write2(codes.Str16, uint16(l)) | ||
60 | } | ||
61 | return e.write4(codes.Str32, uint32(l)) | ||
62 | } | ||
63 | |||
64 | func (e *Encoder) EncodeString(v string) error { | ||
65 | if err := e.encodeStrLen(len(v)); err != nil { | ||
66 | return err | ||
67 | } | ||
68 | return e.writeString(v) | ||
69 | } | ||
70 | |||
71 | func (e *Encoder) EncodeBytes(v []byte) error { | ||
72 | if v == nil { | ||
73 | return e.EncodeNil() | ||
74 | } | ||
75 | if err := e.EncodeBytesLen(len(v)); err != nil { | ||
76 | return err | ||
77 | } | ||
78 | return e.write(v) | ||
79 | } | ||
80 | |||
81 | func (e *Encoder) EncodeArrayLen(l int) error { | ||
82 | if l < 16 { | ||
83 | return e.writeCode(codes.FixedArrayLow | codes.Code(l)) | ||
84 | } | ||
85 | if l < 65536 { | ||
86 | return e.write2(codes.Array16, uint16(l)) | ||
87 | } | ||
88 | return e.write4(codes.Array32, uint32(l)) | ||
89 | } | ||
90 | |||
91 | func (e *Encoder) encodeStringSlice(s []string) error { | ||
92 | if s == nil { | ||
93 | return e.EncodeNil() | ||
94 | } | ||
95 | if err := e.EncodeArrayLen(len(s)); err != nil { | ||
96 | return err | ||
97 | } | ||
98 | for _, v := range s { | ||
99 | if err := e.EncodeString(v); err != nil { | ||
100 | return err | ||
101 | } | ||
102 | } | ||
103 | return nil | ||
104 | } | ||
105 | |||
106 | func encodeSliceValue(e *Encoder, v reflect.Value) error { | ||
107 | if v.IsNil() { | ||
108 | return e.EncodeNil() | ||
109 | } | ||
110 | return encodeArrayValue(e, v) | ||
111 | } | ||
112 | |||
113 | func encodeArrayValue(e *Encoder, v reflect.Value) error { | ||
114 | l := v.Len() | ||
115 | if err := e.EncodeArrayLen(l); err != nil { | ||
116 | return err | ||
117 | } | ||
118 | for i := 0; i < l; i++ { | ||
119 | if err := e.EncodeValue(v.Index(i)); err != nil { | ||
120 | return err | ||
121 | } | ||
122 | } | ||
123 | return nil | ||
124 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/encode_value.go b/vendor/github.com/vmihailenco/msgpack/encode_value.go new file mode 100644 index 0000000..b46ab02 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/encode_value.go | |||
@@ -0,0 +1,167 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "reflect" | ||
6 | ) | ||
7 | |||
8 | var valueEncoders []encoderFunc | ||
9 | |||
10 | func init() { | ||
11 | valueEncoders = []encoderFunc{ | ||
12 | reflect.Bool: encodeBoolValue, | ||
13 | reflect.Int: encodeInt64CondValue, | ||
14 | reflect.Int8: encodeInt8CondValue, | ||
15 | reflect.Int16: encodeInt16CondValue, | ||
16 | reflect.Int32: encodeInt32CondValue, | ||
17 | reflect.Int64: encodeInt64CondValue, | ||
18 | reflect.Uint: encodeUint64CondValue, | ||
19 | reflect.Uint8: encodeUint8CondValue, | ||
20 | reflect.Uint16: encodeUint16CondValue, | ||
21 | reflect.Uint32: encodeUint32CondValue, | ||
22 | reflect.Uint64: encodeUint64CondValue, | ||
23 | reflect.Float32: encodeFloat32Value, | ||
24 | reflect.Float64: encodeFloat64Value, | ||
25 | reflect.Complex64: encodeUnsupportedValue, | ||
26 | reflect.Complex128: encodeUnsupportedValue, | ||
27 | reflect.Array: encodeArrayValue, | ||
28 | reflect.Chan: encodeUnsupportedValue, | ||
29 | reflect.Func: encodeUnsupportedValue, | ||
30 | reflect.Interface: encodeInterfaceValue, | ||
31 | reflect.Map: encodeMapValue, | ||
32 | reflect.Ptr: encodeUnsupportedValue, | ||
33 | reflect.Slice: encodeSliceValue, | ||
34 | reflect.String: encodeStringValue, | ||
35 | reflect.Struct: encodeStructValue, | ||
36 | reflect.UnsafePointer: encodeUnsupportedValue, | ||
37 | } | ||
38 | } | ||
39 | |||
40 | func getEncoder(typ reflect.Type) encoderFunc { | ||
41 | if encoder, ok := typEncMap[typ]; ok { | ||
42 | return encoder | ||
43 | } | ||
44 | |||
45 | if typ.Implements(customEncoderType) { | ||
46 | return encodeCustomValue | ||
47 | } | ||
48 | if typ.Implements(marshalerType) { | ||
49 | return marshalValue | ||
50 | } | ||
51 | |||
52 | kind := typ.Kind() | ||
53 | |||
54 | // Addressable struct field value. | ||
55 | if kind != reflect.Ptr { | ||
56 | ptr := reflect.PtrTo(typ) | ||
57 | if ptr.Implements(customEncoderType) { | ||
58 | return encodeCustomValuePtr | ||
59 | } | ||
60 | if ptr.Implements(marshalerType) { | ||
61 | return marshalValuePtr | ||
62 | } | ||
63 | } | ||
64 | |||
65 | if typ == errorType { | ||
66 | return encodeErrorValue | ||
67 | } | ||
68 | |||
69 | switch kind { | ||
70 | case reflect.Ptr: | ||
71 | return ptrEncoderFunc(typ) | ||
72 | case reflect.Slice: | ||
73 | if typ.Elem().Kind() == reflect.Uint8 { | ||
74 | return encodeByteSliceValue | ||
75 | } | ||
76 | case reflect.Array: | ||
77 | if typ.Elem().Kind() == reflect.Uint8 { | ||
78 | return encodeByteArrayValue | ||
79 | } | ||
80 | case reflect.Map: | ||
81 | if typ.Key() == stringType { | ||
82 | switch typ.Elem() { | ||
83 | case stringType: | ||
84 | return encodeMapStringStringValue | ||
85 | case interfaceType: | ||
86 | return encodeMapStringInterfaceValue | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | return valueEncoders[kind] | ||
91 | } | ||
92 | |||
93 | func ptrEncoderFunc(typ reflect.Type) encoderFunc { | ||
94 | encoder := getEncoder(typ.Elem()) | ||
95 | return func(e *Encoder, v reflect.Value) error { | ||
96 | if v.IsNil() { | ||
97 | return e.EncodeNil() | ||
98 | } | ||
99 | return encoder(e, v.Elem()) | ||
100 | } | ||
101 | } | ||
102 | |||
103 | func encodeCustomValuePtr(e *Encoder, v reflect.Value) error { | ||
104 | if !v.CanAddr() { | ||
105 | return fmt.Errorf("msgpack: Encode(non-addressable %T)", v.Interface()) | ||
106 | } | ||
107 | encoder := v.Addr().Interface().(CustomEncoder) | ||
108 | return encoder.EncodeMsgpack(e) | ||
109 | } | ||
110 | |||
111 | func encodeCustomValue(e *Encoder, v reflect.Value) error { | ||
112 | switch v.Kind() { | ||
113 | case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: | ||
114 | if v.IsNil() { | ||
115 | return e.EncodeNil() | ||
116 | } | ||
117 | } | ||
118 | |||
119 | encoder := v.Interface().(CustomEncoder) | ||
120 | return encoder.EncodeMsgpack(e) | ||
121 | } | ||
122 | |||
123 | func marshalValuePtr(e *Encoder, v reflect.Value) error { | ||
124 | if !v.CanAddr() { | ||
125 | return fmt.Errorf("msgpack: Encode(non-addressable %T)", v.Interface()) | ||
126 | } | ||
127 | return marshalValue(e, v.Addr()) | ||
128 | } | ||
129 | |||
130 | func marshalValue(e *Encoder, v reflect.Value) error { | ||
131 | switch v.Kind() { | ||
132 | case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: | ||
133 | if v.IsNil() { | ||
134 | return e.EncodeNil() | ||
135 | } | ||
136 | } | ||
137 | |||
138 | marshaler := v.Interface().(Marshaler) | ||
139 | b, err := marshaler.MarshalMsgpack() | ||
140 | if err != nil { | ||
141 | return err | ||
142 | } | ||
143 | _, err = e.w.Write(b) | ||
144 | return err | ||
145 | } | ||
146 | |||
147 | func encodeBoolValue(e *Encoder, v reflect.Value) error { | ||
148 | return e.EncodeBool(v.Bool()) | ||
149 | } | ||
150 | |||
151 | func encodeInterfaceValue(e *Encoder, v reflect.Value) error { | ||
152 | if v.IsNil() { | ||
153 | return e.EncodeNil() | ||
154 | } | ||
155 | return e.EncodeValue(v.Elem()) | ||
156 | } | ||
157 | |||
158 | func encodeErrorValue(e *Encoder, v reflect.Value) error { | ||
159 | if v.IsNil() { | ||
160 | return e.EncodeNil() | ||
161 | } | ||
162 | return e.EncodeString(v.Interface().(error).Error()) | ||
163 | } | ||
164 | |||
165 | func encodeUnsupportedValue(e *Encoder, v reflect.Value) error { | ||
166 | return fmt.Errorf("msgpack: Encode(unsupported %s)", v.Type()) | ||
167 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/ext.go b/vendor/github.com/vmihailenco/msgpack/ext.go new file mode 100644 index 0000000..7970be8 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/ext.go | |||
@@ -0,0 +1,238 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "fmt" | ||
6 | "reflect" | ||
7 | "sync" | ||
8 | |||
9 | "github.com/vmihailenco/msgpack/codes" | ||
10 | ) | ||
11 | |||
12 | var extTypes = make(map[int8]reflect.Type) | ||
13 | |||
14 | var bufferPool = &sync.Pool{ | ||
15 | New: func() interface{} { | ||
16 | return new(bytes.Buffer) | ||
17 | }, | ||
18 | } | ||
19 | |||
20 | // RegisterExt records a type, identified by a value for that type, | ||
21 | // under the provided id. That id will identify the concrete type of a value | ||
22 | // sent or received as an interface variable. Only types that will be | ||
23 | // transferred as implementations of interface values need to be registered. | ||
24 | // Expecting to be used only during initialization, it panics if the mapping | ||
25 | // between types and ids is not a bijection. | ||
26 | func RegisterExt(id int8, value interface{}) { | ||
27 | typ := reflect.TypeOf(value) | ||
28 | if typ.Kind() == reflect.Ptr { | ||
29 | typ = typ.Elem() | ||
30 | } | ||
31 | ptr := reflect.PtrTo(typ) | ||
32 | |||
33 | if _, ok := extTypes[id]; ok { | ||
34 | panic(fmt.Errorf("msgpack: ext with id=%d is already registered", id)) | ||
35 | } | ||
36 | |||
37 | registerExt(id, ptr, getEncoder(ptr), getDecoder(ptr)) | ||
38 | registerExt(id, typ, getEncoder(typ), getDecoder(typ)) | ||
39 | } | ||
40 | |||
41 | func registerExt(id int8, typ reflect.Type, enc encoderFunc, dec decoderFunc) { | ||
42 | if dec != nil { | ||
43 | extTypes[id] = typ | ||
44 | } | ||
45 | if enc != nil { | ||
46 | typEncMap[typ] = makeExtEncoder(id, enc) | ||
47 | } | ||
48 | if dec != nil { | ||
49 | typDecMap[typ] = makeExtDecoder(id, dec) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | func (e *Encoder) EncodeExtHeader(typeId int8, length int) error { | ||
54 | if err := e.encodeExtLen(length); err != nil { | ||
55 | return err | ||
56 | } | ||
57 | if err := e.w.WriteByte(byte(typeId)); err != nil { | ||
58 | return err | ||
59 | } | ||
60 | return nil | ||
61 | } | ||
62 | |||
63 | func makeExtEncoder(typeId int8, enc encoderFunc) encoderFunc { | ||
64 | return func(e *Encoder, v reflect.Value) error { | ||
65 | buf := bufferPool.Get().(*bytes.Buffer) | ||
66 | defer bufferPool.Put(buf) | ||
67 | buf.Reset() | ||
68 | |||
69 | oldw := e.w | ||
70 | e.w = buf | ||
71 | err := enc(e, v) | ||
72 | e.w = oldw | ||
73 | |||
74 | if err != nil { | ||
75 | return err | ||
76 | } | ||
77 | |||
78 | err = e.EncodeExtHeader(typeId, buf.Len()) | ||
79 | if err != nil { | ||
80 | return err | ||
81 | } | ||
82 | return e.write(buf.Bytes()) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | func makeExtDecoder(typeId int8, dec decoderFunc) decoderFunc { | ||
87 | return func(d *Decoder, v reflect.Value) error { | ||
88 | c, err := d.PeekCode() | ||
89 | if err != nil { | ||
90 | return err | ||
91 | } | ||
92 | |||
93 | if !codes.IsExt(c) { | ||
94 | return dec(d, v) | ||
95 | } | ||
96 | |||
97 | id, extLen, err := d.DecodeExtHeader() | ||
98 | if err != nil { | ||
99 | return err | ||
100 | } | ||
101 | |||
102 | if id != typeId { | ||
103 | return fmt.Errorf("msgpack: got ext type=%d, wanted %d", int8(c), typeId) | ||
104 | } | ||
105 | |||
106 | d.extLen = extLen | ||
107 | return dec(d, v) | ||
108 | } | ||
109 | } | ||
110 | |||
111 | func (e *Encoder) encodeExtLen(l int) error { | ||
112 | switch l { | ||
113 | case 1: | ||
114 | return e.writeCode(codes.FixExt1) | ||
115 | case 2: | ||
116 | return e.writeCode(codes.FixExt2) | ||
117 | case 4: | ||
118 | return e.writeCode(codes.FixExt4) | ||
119 | case 8: | ||
120 | return e.writeCode(codes.FixExt8) | ||
121 | case 16: | ||
122 | return e.writeCode(codes.FixExt16) | ||
123 | } | ||
124 | if l < 256 { | ||
125 | return e.write1(codes.Ext8, uint8(l)) | ||
126 | } | ||
127 | if l < 65536 { | ||
128 | return e.write2(codes.Ext16, uint16(l)) | ||
129 | } | ||
130 | return e.write4(codes.Ext32, uint32(l)) | ||
131 | } | ||
132 | |||
133 | func (d *Decoder) parseExtLen(c codes.Code) (int, error) { | ||
134 | switch c { | ||
135 | case codes.FixExt1: | ||
136 | return 1, nil | ||
137 | case codes.FixExt2: | ||
138 | return 2, nil | ||
139 | case codes.FixExt4: | ||
140 | return 4, nil | ||
141 | case codes.FixExt8: | ||
142 | return 8, nil | ||
143 | case codes.FixExt16: | ||
144 | return 16, nil | ||
145 | case codes.Ext8: | ||
146 | n, err := d.uint8() | ||
147 | return int(n), err | ||
148 | case codes.Ext16: | ||
149 | n, err := d.uint16() | ||
150 | return int(n), err | ||
151 | case codes.Ext32: | ||
152 | n, err := d.uint32() | ||
153 | return int(n), err | ||
154 | default: | ||
155 | return 0, fmt.Errorf("msgpack: invalid code=%x decoding ext length", c) | ||
156 | } | ||
157 | } | ||
158 | |||
159 | func (d *Decoder) decodeExtHeader(c codes.Code) (int8, int, error) { | ||
160 | length, err := d.parseExtLen(c) | ||
161 | if err != nil { | ||
162 | return 0, 0, err | ||
163 | } | ||
164 | |||
165 | typeId, err := d.readCode() | ||
166 | if err != nil { | ||
167 | return 0, 0, err | ||
168 | } | ||
169 | |||
170 | return int8(typeId), length, nil | ||
171 | } | ||
172 | |||
173 | func (d *Decoder) DecodeExtHeader() (typeId int8, length int, err error) { | ||
174 | c, err := d.readCode() | ||
175 | if err != nil { | ||
176 | return | ||
177 | } | ||
178 | return d.decodeExtHeader(c) | ||
179 | } | ||
180 | |||
181 | func (d *Decoder) extInterface(c codes.Code) (interface{}, error) { | ||
182 | extId, extLen, err := d.decodeExtHeader(c) | ||
183 | if err != nil { | ||
184 | return nil, err | ||
185 | } | ||
186 | |||
187 | typ, ok := extTypes[extId] | ||
188 | if !ok { | ||
189 | return nil, fmt.Errorf("msgpack: unregistered ext id=%d", extId) | ||
190 | } | ||
191 | |||
192 | v := reflect.New(typ) | ||
193 | |||
194 | d.extLen = extLen | ||
195 | err = d.DecodeValue(v.Elem()) | ||
196 | d.extLen = 0 | ||
197 | if err != nil { | ||
198 | return nil, err | ||
199 | } | ||
200 | |||
201 | return v.Interface(), nil | ||
202 | } | ||
203 | |||
204 | func (d *Decoder) skipExt(c codes.Code) error { | ||
205 | n, err := d.parseExtLen(c) | ||
206 | if err != nil { | ||
207 | return err | ||
208 | } | ||
209 | return d.skipN(n + 1) | ||
210 | } | ||
211 | |||
212 | func (d *Decoder) skipExtHeader(c codes.Code) error { | ||
213 | // Read ext type. | ||
214 | _, err := d.readCode() | ||
215 | if err != nil { | ||
216 | return err | ||
217 | } | ||
218 | // Read ext body len. | ||
219 | for i := 0; i < extHeaderLen(c); i++ { | ||
220 | _, err := d.readCode() | ||
221 | if err != nil { | ||
222 | return err | ||
223 | } | ||
224 | } | ||
225 | return nil | ||
226 | } | ||
227 | |||
228 | func extHeaderLen(c codes.Code) int { | ||
229 | switch c { | ||
230 | case codes.Ext8: | ||
231 | return 1 | ||
232 | case codes.Ext16: | ||
233 | return 2 | ||
234 | case codes.Ext32: | ||
235 | return 4 | ||
236 | } | ||
237 | return 0 | ||
238 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/msgpack.go b/vendor/github.com/vmihailenco/msgpack/msgpack.go new file mode 100644 index 0000000..220b43c --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/msgpack.go | |||
@@ -0,0 +1,17 @@ | |||
1 | package msgpack | ||
2 | |||
3 | type Marshaler interface { | ||
4 | MarshalMsgpack() ([]byte, error) | ||
5 | } | ||
6 | |||
7 | type Unmarshaler interface { | ||
8 | UnmarshalMsgpack([]byte) error | ||
9 | } | ||
10 | |||
11 | type CustomEncoder interface { | ||
12 | EncodeMsgpack(*Encoder) error | ||
13 | } | ||
14 | |||
15 | type CustomDecoder interface { | ||
16 | DecodeMsgpack(*Decoder) error | ||
17 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/tag.go b/vendor/github.com/vmihailenco/msgpack/tag.go new file mode 100644 index 0000000..48e6f94 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/tag.go | |||
@@ -0,0 +1,42 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "strings" | ||
5 | ) | ||
6 | |||
7 | type tagOptions string | ||
8 | |||
9 | func (o tagOptions) Get(name string) (string, bool) { | ||
10 | s := string(o) | ||
11 | for len(s) > 0 { | ||
12 | var next string | ||
13 | idx := strings.IndexByte(s, ',') | ||
14 | if idx >= 0 { | ||
15 | s, next = s[:idx], s[idx+1:] | ||
16 | } | ||
17 | if strings.HasPrefix(s, name) { | ||
18 | return s[len(name):], true | ||
19 | } | ||
20 | s = next | ||
21 | } | ||
22 | return "", false | ||
23 | } | ||
24 | |||
25 | func (o tagOptions) Contains(name string) bool { | ||
26 | _, ok := o.Get(name) | ||
27 | return ok | ||
28 | } | ||
29 | |||
30 | func parseTag(tag string) (string, tagOptions) { | ||
31 | if idx := strings.IndexByte(tag, ','); idx != -1 { | ||
32 | name := tag[:idx] | ||
33 | if strings.IndexByte(name, ':') == -1 { | ||
34 | return name, tagOptions(tag[idx+1:]) | ||
35 | } | ||
36 | } | ||
37 | |||
38 | if strings.IndexByte(tag, ':') == -1 { | ||
39 | return tag, "" | ||
40 | } | ||
41 | return "", tagOptions(tag) | ||
42 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/time.go b/vendor/github.com/vmihailenco/msgpack/time.go new file mode 100644 index 0000000..f740969 --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/time.go | |||
@@ -0,0 +1,145 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "encoding/binary" | ||
5 | "fmt" | ||
6 | "reflect" | ||
7 | "time" | ||
8 | |||
9 | "github.com/vmihailenco/msgpack/codes" | ||
10 | ) | ||
11 | |||
12 | var timeExtId int8 = -1 | ||
13 | |||
14 | func init() { | ||
15 | timeType := reflect.TypeOf((*time.Time)(nil)).Elem() | ||
16 | registerExt(timeExtId, timeType, encodeTimeValue, decodeTimeValue) | ||
17 | } | ||
18 | |||
19 | func (e *Encoder) EncodeTime(tm time.Time) error { | ||
20 | b := e.encodeTime(tm) | ||
21 | if err := e.encodeExtLen(len(b)); err != nil { | ||
22 | return err | ||
23 | } | ||
24 | if err := e.w.WriteByte(byte(timeExtId)); err != nil { | ||
25 | return err | ||
26 | } | ||
27 | return e.write(b) | ||
28 | } | ||
29 | |||
30 | func (e *Encoder) encodeTime(tm time.Time) []byte { | ||
31 | secs := uint64(tm.Unix()) | ||
32 | if secs>>34 == 0 { | ||
33 | data := uint64(tm.Nanosecond())<<34 | secs | ||
34 | if data&0xffffffff00000000 == 0 { | ||
35 | b := make([]byte, 4) | ||
36 | binary.BigEndian.PutUint32(b, uint32(data)) | ||
37 | return b | ||
38 | } else { | ||
39 | b := make([]byte, 8) | ||
40 | binary.BigEndian.PutUint64(b, data) | ||
41 | return b | ||
42 | } | ||
43 | } | ||
44 | |||
45 | b := make([]byte, 12) | ||
46 | binary.BigEndian.PutUint32(b, uint32(tm.Nanosecond())) | ||
47 | binary.BigEndian.PutUint64(b[4:], uint64(secs)) | ||
48 | return b | ||
49 | } | ||
50 | |||
51 | func (d *Decoder) DecodeTime() (time.Time, error) { | ||
52 | tm, err := d.decodeTime() | ||
53 | if err != nil { | ||
54 | return tm, err | ||
55 | } | ||
56 | |||
57 | if tm.IsZero() { | ||
58 | // Assume that zero time does not have timezone information. | ||
59 | return tm.UTC(), nil | ||
60 | } | ||
61 | return tm, nil | ||
62 | } | ||
63 | |||
64 | func (d *Decoder) decodeTime() (time.Time, error) { | ||
65 | extLen := d.extLen | ||
66 | d.extLen = 0 | ||
67 | if extLen == 0 { | ||
68 | c, err := d.readCode() | ||
69 | if err != nil { | ||
70 | return time.Time{}, err | ||
71 | } | ||
72 | |||
73 | // Legacy format. | ||
74 | if c == codes.FixedArrayLow|2 { | ||
75 | sec, err := d.DecodeInt64() | ||
76 | if err != nil { | ||
77 | return time.Time{}, err | ||
78 | } | ||
79 | |||
80 | nsec, err := d.DecodeInt64() | ||
81 | if err != nil { | ||
82 | return time.Time{}, err | ||
83 | } | ||
84 | |||
85 | return time.Unix(sec, nsec), nil | ||
86 | } | ||
87 | |||
88 | if codes.IsString(c) { | ||
89 | s, err := d.string(c) | ||
90 | if err != nil { | ||
91 | return time.Time{}, err | ||
92 | } | ||
93 | return time.Parse(time.RFC3339Nano, s) | ||
94 | } | ||
95 | |||
96 | extLen, err = d.parseExtLen(c) | ||
97 | if err != nil { | ||
98 | return time.Time{}, err | ||
99 | } | ||
100 | |||
101 | // Skip ext id. | ||
102 | _, err = d.s.ReadByte() | ||
103 | if err != nil { | ||
104 | return time.Time{}, nil | ||
105 | } | ||
106 | } | ||
107 | |||
108 | b, err := d.readN(extLen) | ||
109 | if err != nil { | ||
110 | return time.Time{}, err | ||
111 | } | ||
112 | |||
113 | switch len(b) { | ||
114 | case 4: | ||
115 | sec := binary.BigEndian.Uint32(b) | ||
116 | return time.Unix(int64(sec), 0), nil | ||
117 | case 8: | ||
118 | sec := binary.BigEndian.Uint64(b) | ||
119 | nsec := int64(sec >> 34) | ||
120 | sec &= 0x00000003ffffffff | ||
121 | return time.Unix(int64(sec), nsec), nil | ||
122 | case 12: | ||
123 | nsec := binary.BigEndian.Uint32(b) | ||
124 | sec := binary.BigEndian.Uint64(b[4:]) | ||
125 | return time.Unix(int64(sec), int64(nsec)), nil | ||
126 | default: | ||
127 | err = fmt.Errorf("msgpack: invalid ext len=%d decoding time", extLen) | ||
128 | return time.Time{}, err | ||
129 | } | ||
130 | } | ||
131 | |||
132 | func encodeTimeValue(e *Encoder, v reflect.Value) error { | ||
133 | tm := v.Interface().(time.Time) | ||
134 | b := e.encodeTime(tm) | ||
135 | return e.write(b) | ||
136 | } | ||
137 | |||
138 | func decodeTimeValue(d *Decoder, v reflect.Value) error { | ||
139 | tm, err := d.DecodeTime() | ||
140 | if err != nil { | ||
141 | return err | ||
142 | } | ||
143 | v.Set(reflect.ValueOf(tm)) | ||
144 | return nil | ||
145 | } | ||
diff --git a/vendor/github.com/vmihailenco/msgpack/types.go b/vendor/github.com/vmihailenco/msgpack/types.go new file mode 100644 index 0000000..6a1bf7f --- /dev/null +++ b/vendor/github.com/vmihailenco/msgpack/types.go | |||
@@ -0,0 +1,310 @@ | |||
1 | package msgpack | ||
2 | |||
3 | import ( | ||
4 | "reflect" | ||
5 | "sync" | ||
6 | ) | ||
7 | |||
8 | var errorType = reflect.TypeOf((*error)(nil)).Elem() | ||
9 | |||
10 | var customEncoderType = reflect.TypeOf((*CustomEncoder)(nil)).Elem() | ||
11 | var customDecoderType = reflect.TypeOf((*CustomDecoder)(nil)).Elem() | ||
12 | |||
13 | var marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() | ||
14 | var unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() | ||
15 | |||
16 | type encoderFunc func(*Encoder, reflect.Value) error | ||
17 | type decoderFunc func(*Decoder, reflect.Value) error | ||
18 | |||
19 | var typEncMap = make(map[reflect.Type]encoderFunc) | ||
20 | var typDecMap = make(map[reflect.Type]decoderFunc) | ||
21 | |||
22 | // Register registers encoder and decoder functions for a value. | ||
23 | // This is low level API and in most cases you should prefer implementing | ||
24 | // Marshaler/CustomEncoder and Unmarshaler/CustomDecoder interfaces. | ||
25 | func Register(value interface{}, enc encoderFunc, dec decoderFunc) { | ||
26 | typ := reflect.TypeOf(value) | ||
27 | if enc != nil { | ||
28 | typEncMap[typ] = enc | ||
29 | } | ||
30 | if dec != nil { | ||
31 | typDecMap[typ] = dec | ||
32 | } | ||
33 | } | ||
34 | |||
35 | //------------------------------------------------------------------------------ | ||
36 | |||
37 | var structs = newStructCache(false) | ||
38 | var jsonStructs = newStructCache(true) | ||
39 | |||
40 | type structCache struct { | ||
41 | mu sync.RWMutex | ||
42 | m map[reflect.Type]*fields | ||
43 | |||
44 | useJSONTag bool | ||
45 | } | ||
46 | |||
47 | func newStructCache(useJSONTag bool) *structCache { | ||
48 | return &structCache{ | ||
49 | m: make(map[reflect.Type]*fields), | ||
50 | |||
51 | useJSONTag: useJSONTag, | ||
52 | } | ||
53 | } | ||
54 | |||
55 | func (m *structCache) Fields(typ reflect.Type) *fields { | ||
56 | m.mu.RLock() | ||
57 | fs, ok := m.m[typ] | ||
58 | m.mu.RUnlock() | ||
59 | if ok { | ||
60 | return fs | ||
61 | } | ||
62 | |||
63 | m.mu.Lock() | ||
64 | fs, ok = m.m[typ] | ||
65 | if !ok { | ||
66 | fs = getFields(typ, m.useJSONTag) | ||
67 | m.m[typ] = fs | ||
68 | } | ||
69 | m.mu.Unlock() | ||
70 | |||
71 | return fs | ||
72 | } | ||
73 | |||
74 | //------------------------------------------------------------------------------ | ||
75 | |||
76 | type field struct { | ||
77 | name string | ||
78 | index []int | ||
79 | omitEmpty bool | ||
80 | encoder encoderFunc | ||
81 | decoder decoderFunc | ||
82 | } | ||
83 | |||
84 | func (f *field) value(v reflect.Value) reflect.Value { | ||
85 | return fieldByIndex(v, f.index) | ||
86 | } | ||
87 | |||
88 | func (f *field) Omit(strct reflect.Value) bool { | ||
89 | return f.omitEmpty && isEmptyValue(f.value(strct)) | ||
90 | } | ||
91 | |||
92 | func (f *field) EncodeValue(e *Encoder, strct reflect.Value) error { | ||
93 | return f.encoder(e, f.value(strct)) | ||
94 | } | ||
95 | |||
96 | func (f *field) DecodeValue(d *Decoder, strct reflect.Value) error { | ||
97 | return f.decoder(d, f.value(strct)) | ||
98 | } | ||
99 | |||
100 | //------------------------------------------------------------------------------ | ||
101 | |||
102 | type fields struct { | ||
103 | Table map[string]*field | ||
104 | List []*field | ||
105 | AsArray bool | ||
106 | |||
107 | hasOmitEmpty bool | ||
108 | } | ||
109 | |||
110 | func newFields(numField int) *fields { | ||
111 | return &fields{ | ||
112 | Table: make(map[string]*field, numField), | ||
113 | List: make([]*field, 0, numField), | ||
114 | } | ||
115 | } | ||
116 | |||
117 | func (fs *fields) Add(field *field) { | ||
118 | fs.Table[field.name] = field | ||
119 | fs.List = append(fs.List, field) | ||
120 | if field.omitEmpty { | ||
121 | fs.hasOmitEmpty = true | ||
122 | } | ||
123 | } | ||
124 | |||
125 | func (fs *fields) OmitEmpty(strct reflect.Value) []*field { | ||
126 | if !fs.hasOmitEmpty { | ||
127 | return fs.List | ||
128 | } | ||
129 | |||
130 | fields := make([]*field, 0, len(fs.List)) | ||
131 | for _, f := range fs.List { | ||
132 | if !f.Omit(strct) { | ||
133 | fields = append(fields, f) | ||
134 | } | ||
135 | } | ||
136 | return fields | ||
137 | } | ||
138 | |||
139 | func getFields(typ reflect.Type, useJSONTag bool) *fields { | ||
140 | numField := typ.NumField() | ||
141 | fs := newFields(numField) | ||
142 | |||
143 | var omitEmpty bool | ||
144 | for i := 0; i < numField; i++ { | ||
145 | f := typ.Field(i) | ||
146 | |||
147 | tag := f.Tag.Get("msgpack") | ||
148 | if useJSONTag && tag == "" { | ||
149 | tag = f.Tag.Get("json") | ||
150 | } | ||
151 | |||
152 | name, opt := parseTag(tag) | ||
153 | if name == "-" { | ||
154 | continue | ||
155 | } | ||
156 | |||
157 | if f.Name == "_msgpack" { | ||
158 | if opt.Contains("asArray") { | ||
159 | fs.AsArray = true | ||
160 | } | ||
161 | if opt.Contains("omitempty") { | ||
162 | omitEmpty = true | ||
163 | } | ||
164 | } | ||
165 | |||
166 | if f.PkgPath != "" && !f.Anonymous { | ||
167 | continue | ||
168 | } | ||
169 | |||
170 | field := &field{ | ||
171 | name: name, | ||
172 | index: f.Index, | ||
173 | omitEmpty: omitEmpty || opt.Contains("omitempty"), | ||
174 | encoder: getEncoder(f.Type), | ||
175 | decoder: getDecoder(f.Type), | ||
176 | } | ||
177 | |||
178 | if field.name == "" { | ||
179 | field.name = f.Name | ||
180 | } | ||
181 | |||
182 | if f.Anonymous && !opt.Contains("noinline") { | ||
183 | inline := opt.Contains("inline") | ||
184 | if inline { | ||
185 | inlineFields(fs, f.Type, field, useJSONTag) | ||
186 | } else { | ||
187 | inline = autoinlineFields(fs, f.Type, field, useJSONTag) | ||
188 | } | ||
189 | if inline { | ||
190 | fs.Table[field.name] = field | ||
191 | continue | ||
192 | } | ||
193 | } | ||
194 | |||
195 | fs.Add(field) | ||
196 | } | ||
197 | return fs | ||
198 | } | ||
199 | |||
200 | var encodeStructValuePtr uintptr | ||
201 | var decodeStructValuePtr uintptr | ||
202 | |||
203 | func init() { | ||
204 | encodeStructValuePtr = reflect.ValueOf(encodeStructValue).Pointer() | ||
205 | decodeStructValuePtr = reflect.ValueOf(decodeStructValue).Pointer() | ||
206 | } | ||
207 | |||
208 | func inlineFields(fs *fields, typ reflect.Type, f *field, useJSONTag bool) { | ||
209 | inlinedFields := getFields(typ, useJSONTag).List | ||
210 | for _, field := range inlinedFields { | ||
211 | if _, ok := fs.Table[field.name]; ok { | ||
212 | // Don't inline shadowed fields. | ||
213 | continue | ||
214 | } | ||
215 | field.index = append(f.index, field.index...) | ||
216 | fs.Add(field) | ||
217 | } | ||
218 | } | ||
219 | |||
220 | func autoinlineFields(fs *fields, typ reflect.Type, f *field, useJSONTag bool) bool { | ||
221 | var encoder encoderFunc | ||
222 | var decoder decoderFunc | ||
223 | |||
224 | if typ.Kind() == reflect.Struct { | ||
225 | encoder = f.encoder | ||
226 | decoder = f.decoder | ||
227 | } else { | ||
228 | for typ.Kind() == reflect.Ptr { | ||
229 | typ = typ.Elem() | ||
230 | encoder = getEncoder(typ) | ||
231 | decoder = getDecoder(typ) | ||
232 | } | ||
233 | if typ.Kind() != reflect.Struct { | ||
234 | return false | ||
235 | } | ||
236 | } | ||
237 | |||
238 | if reflect.ValueOf(encoder).Pointer() != encodeStructValuePtr { | ||
239 | return false | ||
240 | } | ||
241 | if reflect.ValueOf(decoder).Pointer() != decodeStructValuePtr { | ||
242 | return false | ||
243 | } | ||
244 | |||
245 | inlinedFields := getFields(typ, useJSONTag).List | ||
246 | for _, field := range inlinedFields { | ||
247 | if _, ok := fs.Table[field.name]; ok { | ||
248 | // Don't auto inline if there are shadowed fields. | ||
249 | return false | ||
250 | } | ||
251 | } | ||
252 | |||
253 | for _, field := range inlinedFields { | ||
254 | field.index = append(f.index, field.index...) | ||
255 | fs.Add(field) | ||
256 | } | ||
257 | return true | ||
258 | } | ||
259 | |||
260 | func isEmptyValue(v reflect.Value) bool { | ||
261 | switch v.Kind() { | ||
262 | case reflect.Array, reflect.Map, reflect.Slice, reflect.String: | ||
263 | return v.Len() == 0 | ||
264 | case reflect.Bool: | ||
265 | return !v.Bool() | ||
266 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
267 | return v.Int() == 0 | ||
268 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
269 | return v.Uint() == 0 | ||
270 | case reflect.Float32, reflect.Float64: | ||
271 | return v.Float() == 0 | ||
272 | case reflect.Interface, reflect.Ptr: | ||
273 | return v.IsNil() | ||
274 | } | ||
275 | return false | ||
276 | } | ||
277 | |||
278 | func fieldByIndex(v reflect.Value, index []int) reflect.Value { | ||
279 | if len(index) == 1 { | ||
280 | return v.Field(index[0]) | ||
281 | } | ||
282 | for i, x := range index { | ||
283 | if i > 0 { | ||
284 | var ok bool | ||
285 | v, ok = indirectNew(v) | ||
286 | if !ok { | ||
287 | return v | ||
288 | } | ||
289 | } | ||
290 | v = v.Field(x) | ||
291 | } | ||
292 | return v | ||
293 | } | ||
294 | |||
295 | func indirectNew(v reflect.Value) (reflect.Value, bool) { | ||
296 | if v.Kind() == reflect.Ptr { | ||
297 | if v.IsNil() { | ||
298 | if !v.CanSet() { | ||
299 | return v, false | ||
300 | } | ||
301 | elemType := v.Type().Elem() | ||
302 | if elemType.Kind() != reflect.Struct { | ||
303 | return v, false | ||
304 | } | ||
305 | v.Set(reflect.New(elemType)) | ||
306 | } | ||
307 | v = v.Elem() | ||
308 | } | ||
309 | return v, true | ||
310 | } | ||