Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 8 | "reflect" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "google.golang.org/protobuf/internal/encoding/wire" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/proto" |
| 13 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 14 | ) |
| 15 | |
| 16 | type errInvalidUTF8 struct{} |
| 17 | |
| 18 | func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" } |
| 19 | func (errInvalidUTF8) InvalidUTF8() bool { return true } |
| 20 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 21 | func makeOneofFieldCoder(si structInfo, fd pref.FieldDescriptor) pointerCoderFuncs { |
| 22 | ot := si.oneofWrappersByNumber[fd.Number()] |
| 23 | funcs := fieldCoder(fd, ot.Field(0).Type) |
| 24 | fs := si.oneofsByName[fd.ContainingOneof().Name()] |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 25 | ft := fs.Type |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 26 | wiretag := wire.EncodeTag(fd.Number(), wireTypes[fd.Kind()]) |
| 27 | tagsize := wire.SizeVarint(wiretag) |
| 28 | getInfo := func(p pointer) (pointer, bool) { |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 29 | v := p.AsValueOf(ft).Elem() |
| 30 | if v.IsNil() { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 31 | return pointer{}, false |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 32 | } |
| 33 | v = v.Elem() // interface -> *struct |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 34 | if v.Elem().Type() != ot { |
| 35 | return pointer{}, false |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 36 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 37 | return pointerOfValue(v).Apply(zeroOffset), true |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 38 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 39 | pcf := pointerCoderFuncs{ |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 40 | size: func(p pointer, _ int, opts marshalOptions) int { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 41 | v, ok := getInfo(p) |
| 42 | if !ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 43 | return 0 |
| 44 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 45 | return funcs.size(v, tagsize, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 46 | }, |
| 47 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 48 | v, ok := getInfo(p) |
| 49 | if !ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 50 | return b, nil |
| 51 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 52 | return funcs.marshal(b, v, wiretag, opts) |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 53 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 54 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
Joe Tsai | 6c28674 | 2019-07-11 23:15:05 -0700 | [diff] [blame^] | 55 | var vw reflect.Value // pointer to wrapper type |
| 56 | vi := p.AsValueOf(ft).Elem() // oneof field value of interface kind |
| 57 | if !vi.IsNil() && !vi.Elem().IsNil() && vi.Elem().Elem().Type() == ot { |
| 58 | vw = vi.Elem() |
| 59 | } else { |
| 60 | vw = reflect.New(ot) |
| 61 | } |
| 62 | n, err := funcs.unmarshal(b, pointerOfValue(vw).Apply(zeroOffset), wtyp, opts) |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 63 | if err != nil { |
| 64 | return 0, err |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 65 | } |
Joe Tsai | 6c28674 | 2019-07-11 23:15:05 -0700 | [diff] [blame^] | 66 | vi.Set(vw) |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 67 | return n, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 68 | }, |
| 69 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 70 | if funcs.isInit != nil { |
| 71 | pcf.isInit = func(p pointer) error { |
| 72 | v, ok := getInfo(p) |
| 73 | if !ok { |
| 74 | return nil |
| 75 | } |
| 76 | return funcs.isInit(v) |
| 77 | } |
| 78 | } |
| 79 | return pcf |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func makeMessageFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 83 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 84 | return pointerCoderFuncs{ |
| 85 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 86 | return sizeMessageInfo(p, fi, tagsize, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 87 | }, |
| 88 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 89 | return appendMessageInfo(b, p, wiretag, fi, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 90 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 91 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 92 | return consumeMessageInfo(b, p, fi, wtyp, opts) |
| 93 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 94 | isInit: func(p pointer) error { |
| 95 | return fi.isInitializedPointer(p.Elem()) |
| 96 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 97 | } |
| 98 | } else { |
| 99 | return pointerCoderFuncs{ |
| 100 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 101 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 102 | return sizeMessage(m, tagsize, opts) |
| 103 | }, |
| 104 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 105 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 106 | return appendMessage(b, m, wiretag, opts) |
| 107 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 108 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 109 | mp := p.AsValueOf(ft).Elem() |
| 110 | if mp.IsNil() { |
| 111 | mp.Set(reflect.New(ft.Elem())) |
| 112 | } |
| 113 | return consumeMessage(b, asMessage(mp), wtyp, opts) |
| 114 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 115 | isInit: func(p pointer) error { |
| 116 | m := asMessage(p.AsValueOf(ft).Elem()) |
| 117 | return proto.IsInitialized(m) |
| 118 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 123 | func sizeMessageInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 124 | return wire.SizeBytes(mi.sizePointer(p.Elem(), opts)) + tagsize |
| 125 | } |
| 126 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 127 | func appendMessageInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 128 | b = wire.AppendVarint(b, wiretag) |
| 129 | b = wire.AppendVarint(b, uint64(mi.sizePointer(p.Elem(), opts))) |
| 130 | return mi.marshalAppendPointer(b, p.Elem(), opts) |
| 131 | } |
| 132 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 133 | func consumeMessageInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 134 | if wtyp != wire.BytesType { |
| 135 | return 0, errUnknown |
| 136 | } |
| 137 | v, n := wire.ConsumeBytes(b) |
| 138 | if n < 0 { |
| 139 | return 0, wire.ParseError(n) |
| 140 | } |
| 141 | if p.Elem().IsNil() { |
| 142 | p.SetPointer(pointerOfValue(reflect.New(mi.GoType.Elem()))) |
| 143 | } |
| 144 | if _, err := mi.unmarshalPointer(v, p.Elem(), 0, opts); err != nil { |
| 145 | return 0, err |
| 146 | } |
| 147 | return n, nil |
| 148 | } |
| 149 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 150 | func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int { |
| 151 | return wire.SizeBytes(proto.Size(m)) + tagsize |
| 152 | } |
| 153 | |
| 154 | func appendMessage(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 155 | b = wire.AppendVarint(b, wiretag) |
| 156 | b = wire.AppendVarint(b, uint64(proto.Size(m))) |
| 157 | return opts.Options().MarshalAppend(b, m) |
| 158 | } |
| 159 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 160 | func consumeMessage(b []byte, m proto.Message, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 161 | if wtyp != wire.BytesType { |
| 162 | return 0, errUnknown |
| 163 | } |
| 164 | v, n := wire.ConsumeBytes(b) |
| 165 | if n < 0 { |
| 166 | return 0, wire.ParseError(n) |
| 167 | } |
| 168 | if err := opts.Options().Unmarshal(v, m); err != nil { |
| 169 | return 0, err |
| 170 | } |
| 171 | return n, nil |
| 172 | } |
| 173 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 174 | func sizeMessageIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 175 | m := Export{}.MessageOf(ival).Interface() |
| 176 | return sizeMessage(m, tagsize, opts) |
| 177 | } |
| 178 | |
| 179 | func appendMessageIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 180 | m := Export{}.MessageOf(ival).Interface() |
| 181 | return appendMessage(b, m, wiretag, opts) |
| 182 | } |
| 183 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 184 | func consumeMessageIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 185 | m := Export{}.MessageOf(ival).Interface() |
| 186 | n, err := consumeMessage(b, m, wtyp, opts) |
| 187 | return ival, n, err |
| 188 | } |
| 189 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 190 | func isInitMessageIface(ival interface{}) error { |
| 191 | m := Export{}.MessageOf(ival).Interface() |
| 192 | return proto.IsInitialized(m) |
| 193 | } |
| 194 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 195 | var coderMessageIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 196 | size: sizeMessageIface, |
| 197 | marshal: appendMessageIface, |
| 198 | unmarshal: consumeMessageIface, |
| 199 | isInit: isInitMessageIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 203 | num := fd.Number() |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 204 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 205 | return pointerCoderFuncs{ |
| 206 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 207 | return sizeGroupType(p, fi, tagsize, opts) |
| 208 | }, |
| 209 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 210 | return appendGroupType(b, p, wiretag, fi, opts) |
| 211 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 212 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 213 | return consumeGroupType(b, p, fi, num, wtyp, opts) |
| 214 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 215 | isInit: func(p pointer) error { |
| 216 | return fi.isInitializedPointer(p.Elem()) |
| 217 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 218 | } |
| 219 | } else { |
| 220 | return pointerCoderFuncs{ |
| 221 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 222 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 223 | return sizeGroup(m, tagsize, opts) |
| 224 | }, |
| 225 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 226 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 227 | return appendGroup(b, m, wiretag, opts) |
| 228 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 229 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 230 | mp := p.AsValueOf(ft).Elem() |
| 231 | if mp.IsNil() { |
| 232 | mp.Set(reflect.New(ft.Elem())) |
| 233 | } |
| 234 | return consumeGroup(b, asMessage(mp), num, wtyp, opts) |
| 235 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 236 | isInit: func(p pointer) error { |
| 237 | m := asMessage(p.AsValueOf(ft).Elem()) |
| 238 | return proto.IsInitialized(m) |
| 239 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 244 | func sizeGroupType(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 245 | return 2*tagsize + mi.sizePointer(p.Elem(), opts) |
| 246 | } |
| 247 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 248 | func appendGroupType(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 249 | b = wire.AppendVarint(b, wiretag) // start group |
| 250 | b, err := mi.marshalAppendPointer(b, p.Elem(), opts) |
| 251 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 252 | return b, err |
| 253 | } |
| 254 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 255 | func consumeGroupType(b []byte, p pointer, mi *MessageInfo, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 256 | if wtyp != wire.StartGroupType { |
| 257 | return 0, errUnknown |
| 258 | } |
| 259 | if p.Elem().IsNil() { |
| 260 | p.SetPointer(pointerOfValue(reflect.New(mi.GoType.Elem()))) |
| 261 | } |
| 262 | return mi.unmarshalPointer(b, p.Elem(), num, opts) |
| 263 | } |
| 264 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 265 | func sizeGroup(m proto.Message, tagsize int, _ marshalOptions) int { |
| 266 | return 2*tagsize + proto.Size(m) |
| 267 | } |
| 268 | |
| 269 | func appendGroup(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 270 | b = wire.AppendVarint(b, wiretag) // start group |
| 271 | b, err := opts.Options().MarshalAppend(b, m) |
| 272 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 273 | return b, err |
| 274 | } |
| 275 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 276 | func consumeGroup(b []byte, m proto.Message, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 277 | if wtyp != wire.StartGroupType { |
| 278 | return 0, errUnknown |
| 279 | } |
| 280 | b, n := wire.ConsumeGroup(num, b) |
| 281 | if n < 0 { |
| 282 | return 0, wire.ParseError(n) |
| 283 | } |
| 284 | return n, opts.Options().Unmarshal(b, m) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 287 | func makeGroupValueCoder(fd pref.FieldDescriptor, ft reflect.Type) ifaceCoderFuncs { |
| 288 | return ifaceCoderFuncs{ |
| 289 | size: func(ival interface{}, tagsize int, opts marshalOptions) int { |
| 290 | m := Export{}.MessageOf(ival).Interface() |
| 291 | return sizeGroup(m, tagsize, opts) |
| 292 | }, |
| 293 | marshal: func(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 294 | m := Export{}.MessageOf(ival).Interface() |
| 295 | return appendGroup(b, m, wiretag, opts) |
| 296 | }, |
| 297 | unmarshal: func(b []byte, ival interface{}, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 298 | m := Export{}.MessageOf(ival).Interface() |
| 299 | n, err := consumeGroup(b, m, num, wtyp, opts) |
| 300 | return ival, n, err |
| 301 | }, |
| 302 | isInit: isInitMessageIface, |
| 303 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | func makeMessageSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 307 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 308 | return pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 309 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 310 | return sizeMessageSliceInfo(p, fi, tagsize, opts) |
| 311 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 312 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 313 | return appendMessageSliceInfo(b, p, wiretag, fi, opts) |
| 314 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 315 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 316 | return consumeMessageSliceInfo(b, p, fi, wtyp, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 317 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 318 | isInit: func(p pointer) error { |
| 319 | return isInitMessageSliceInfo(p, fi) |
| 320 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | return pointerCoderFuncs{ |
| 324 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 325 | return sizeMessageSlice(p, ft, tagsize, opts) |
| 326 | }, |
| 327 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 328 | return appendMessageSlice(b, p, wiretag, ft, opts) |
| 329 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 330 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 331 | return consumeMessageSlice(b, p, ft, wtyp, opts) |
| 332 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 333 | isInit: func(p pointer) error { |
| 334 | return isInitMessageSlice(p, ft) |
| 335 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 339 | func sizeMessageSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 340 | s := p.PointerSlice() |
| 341 | n := 0 |
| 342 | for _, v := range s { |
| 343 | n += wire.SizeBytes(mi.sizePointer(v, opts)) + tagsize |
| 344 | } |
| 345 | return n |
| 346 | } |
| 347 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 348 | func appendMessageSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 349 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 350 | var err error |
| 351 | for _, v := range s { |
| 352 | b = wire.AppendVarint(b, wiretag) |
| 353 | siz := mi.sizePointer(v, opts) |
| 354 | b = wire.AppendVarint(b, uint64(siz)) |
| 355 | b, err = mi.marshalAppendPointer(b, v, opts) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 356 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 357 | return b, err |
| 358 | } |
| 359 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 360 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 363 | func consumeMessageSliceInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 364 | if wtyp != wire.BytesType { |
| 365 | return 0, errUnknown |
| 366 | } |
| 367 | v, n := wire.ConsumeBytes(b) |
| 368 | if n < 0 { |
| 369 | return 0, wire.ParseError(n) |
| 370 | } |
| 371 | m := reflect.New(mi.GoType.Elem()).Interface() |
| 372 | mp := pointerOfIface(m) |
| 373 | if _, err := mi.unmarshalPointer(v, mp, 0, opts); err != nil { |
| 374 | return 0, err |
| 375 | } |
| 376 | p.AppendPointerSlice(mp) |
| 377 | return n, nil |
| 378 | } |
| 379 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 380 | func isInitMessageSliceInfo(p pointer, mi *MessageInfo) error { |
| 381 | s := p.PointerSlice() |
| 382 | for _, v := range s { |
| 383 | if err := mi.isInitializedPointer(v); err != nil { |
| 384 | return err |
| 385 | } |
| 386 | } |
| 387 | return nil |
| 388 | } |
| 389 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 390 | func sizeMessageSlice(p pointer, goType reflect.Type, tagsize int, _ marshalOptions) int { |
| 391 | s := p.PointerSlice() |
| 392 | n := 0 |
| 393 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 394 | m := asMessage(v.AsValueOf(goType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 395 | n += wire.SizeBytes(proto.Size(m)) + tagsize |
| 396 | } |
| 397 | return n |
| 398 | } |
| 399 | |
| 400 | func appendMessageSlice(b []byte, p pointer, wiretag uint64, goType reflect.Type, opts marshalOptions) ([]byte, error) { |
| 401 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 402 | var err error |
| 403 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 404 | m := asMessage(v.AsValueOf(goType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 405 | b = wire.AppendVarint(b, wiretag) |
| 406 | siz := proto.Size(m) |
| 407 | b = wire.AppendVarint(b, uint64(siz)) |
| 408 | b, err = opts.Options().MarshalAppend(b, m) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 409 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 410 | return b, err |
| 411 | } |
| 412 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 413 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 416 | func consumeMessageSlice(b []byte, p pointer, goType reflect.Type, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 417 | if wtyp != wire.BytesType { |
| 418 | return 0, errUnknown |
| 419 | } |
| 420 | v, n := wire.ConsumeBytes(b) |
| 421 | if n < 0 { |
| 422 | return 0, wire.ParseError(n) |
| 423 | } |
| 424 | mp := reflect.New(goType.Elem()) |
| 425 | if err := opts.Options().Unmarshal(v, asMessage(mp)); err != nil { |
| 426 | return 0, err |
| 427 | } |
| 428 | p.AppendPointerSlice(pointerOfValue(mp)) |
| 429 | return n, nil |
| 430 | } |
| 431 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 432 | func isInitMessageSlice(p pointer, goType reflect.Type) error { |
| 433 | s := p.PointerSlice() |
| 434 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 435 | m := asMessage(v.AsValueOf(goType.Elem())) |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 436 | if err := proto.IsInitialized(m); err != nil { |
| 437 | return err |
| 438 | } |
| 439 | } |
| 440 | return nil |
| 441 | } |
| 442 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 443 | // Slices of messages |
| 444 | |
| 445 | func sizeMessageSliceIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 446 | p := pointerOfIface(ival) |
| 447 | return sizeMessageSlice(p, reflect.TypeOf(ival).Elem().Elem(), tagsize, opts) |
| 448 | } |
| 449 | |
| 450 | func appendMessageSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 451 | p := pointerOfIface(ival) |
| 452 | return appendMessageSlice(b, p, wiretag, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 453 | } |
| 454 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 455 | func consumeMessageSliceIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 456 | p := pointerOfIface(ival) |
| 457 | n, err := consumeMessageSlice(b, p, reflect.TypeOf(ival).Elem().Elem(), wtyp, opts) |
| 458 | return ival, n, err |
| 459 | } |
| 460 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 461 | func isInitMessageSliceIface(ival interface{}) error { |
| 462 | p := pointerOfIface(ival) |
| 463 | return isInitMessageSlice(p, reflect.TypeOf(ival).Elem().Elem()) |
| 464 | } |
| 465 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 466 | var coderMessageSliceIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 467 | size: sizeMessageSliceIface, |
| 468 | marshal: appendMessageSliceIface, |
| 469 | unmarshal: consumeMessageSliceIface, |
| 470 | isInit: isInitMessageSliceIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | func makeGroupSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 474 | num := fd.Number() |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 475 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 476 | return pointerCoderFuncs{ |
| 477 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 478 | return sizeGroupSliceInfo(p, fi, tagsize, opts) |
| 479 | }, |
| 480 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 481 | return appendGroupSliceInfo(b, p, wiretag, fi, opts) |
| 482 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 483 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 484 | return consumeGroupSliceInfo(b, p, num, wtyp, fi, opts) |
| 485 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 486 | isInit: func(p pointer) error { |
| 487 | return isInitMessageSliceInfo(p, fi) |
| 488 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | return pointerCoderFuncs{ |
| 492 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 493 | return sizeGroupSlice(p, ft, tagsize, opts) |
| 494 | }, |
| 495 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 496 | return appendGroupSlice(b, p, wiretag, ft, opts) |
| 497 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 498 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 499 | return consumeGroupSlice(b, p, num, wtyp, ft, opts) |
| 500 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 501 | isInit: func(p pointer) error { |
| 502 | return isInitMessageSlice(p, ft) |
| 503 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | func sizeGroupSlice(p pointer, messageType reflect.Type, tagsize int, _ marshalOptions) int { |
| 508 | s := p.PointerSlice() |
| 509 | n := 0 |
| 510 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 511 | m := asMessage(v.AsValueOf(messageType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 512 | n += 2*tagsize + proto.Size(m) |
| 513 | } |
| 514 | return n |
| 515 | } |
| 516 | |
| 517 | func appendGroupSlice(b []byte, p pointer, wiretag uint64, messageType reflect.Type, opts marshalOptions) ([]byte, error) { |
| 518 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 519 | var err error |
| 520 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 521 | m := asMessage(v.AsValueOf(messageType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 522 | b = wire.AppendVarint(b, wiretag) // start group |
| 523 | b, err = opts.Options().MarshalAppend(b, m) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 524 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 525 | return b, err |
| 526 | } |
| 527 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 528 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 529 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 532 | func consumeGroupSlice(b []byte, p pointer, num wire.Number, wtyp wire.Type, goType reflect.Type, opts unmarshalOptions) (int, error) { |
| 533 | if wtyp != wire.StartGroupType { |
| 534 | return 0, errUnknown |
| 535 | } |
| 536 | b, n := wire.ConsumeGroup(num, b) |
| 537 | if n < 0 { |
| 538 | return 0, wire.ParseError(n) |
| 539 | } |
| 540 | mp := reflect.New(goType.Elem()) |
| 541 | if err := opts.Options().Unmarshal(b, asMessage(mp)); err != nil { |
| 542 | return 0, err |
| 543 | } |
| 544 | p.AppendPointerSlice(pointerOfValue(mp)) |
| 545 | return n, nil |
| 546 | } |
| 547 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 548 | func sizeGroupSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 549 | s := p.PointerSlice() |
| 550 | n := 0 |
| 551 | for _, v := range s { |
| 552 | n += 2*tagsize + mi.sizePointer(v, opts) |
| 553 | } |
| 554 | return n |
| 555 | } |
| 556 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 557 | func appendGroupSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 558 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 559 | var err error |
| 560 | for _, v := range s { |
| 561 | b = wire.AppendVarint(b, wiretag) // start group |
| 562 | b, err = mi.marshalAppendPointer(b, v, opts) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 563 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 564 | return b, err |
| 565 | } |
| 566 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 567 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 568 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 571 | func consumeGroupSliceInfo(b []byte, p pointer, num wire.Number, wtyp wire.Type, mi *MessageInfo, opts unmarshalOptions) (int, error) { |
| 572 | if wtyp != wire.StartGroupType { |
| 573 | return 0, errUnknown |
| 574 | } |
| 575 | m := reflect.New(mi.GoType.Elem()).Interface() |
| 576 | mp := pointerOfIface(m) |
| 577 | n, err := mi.unmarshalPointer(b, mp, num, opts) |
| 578 | if err != nil { |
| 579 | return 0, err |
| 580 | } |
| 581 | p.AppendPointerSlice(mp) |
| 582 | return n, nil |
| 583 | } |
| 584 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 585 | func sizeGroupSliceIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 586 | p := pointerOfIface(ival) |
| 587 | return sizeGroupSlice(p, reflect.TypeOf(ival).Elem().Elem(), tagsize, opts) |
| 588 | } |
| 589 | |
| 590 | func appendGroupSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 591 | p := pointerOfIface(ival) |
| 592 | return appendGroupSlice(b, p, wiretag, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 593 | } |
| 594 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 595 | func consumeGroupSliceIface(b []byte, ival interface{}, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 596 | p := pointerOfIface(ival) |
| 597 | n, err := consumeGroupSlice(b, p, num, wtyp, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 598 | return ival, n, err |
| 599 | } |
| 600 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 601 | var coderGroupSliceIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 602 | size: sizeGroupSliceIface, |
| 603 | marshal: appendGroupSliceIface, |
| 604 | unmarshal: consumeGroupSliceIface, |
| 605 | isInit: isInitMessageSliceIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // Enums |
| 609 | |
| 610 | func sizeEnumIface(ival interface{}, tagsize int, _ marshalOptions) (n int) { |
| 611 | v := reflect.ValueOf(ival).Int() |
| 612 | return wire.SizeVarint(uint64(v)) + tagsize |
| 613 | } |
| 614 | |
| 615 | func appendEnumIface(b []byte, ival interface{}, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 616 | v := reflect.ValueOf(ival).Int() |
| 617 | b = wire.AppendVarint(b, wiretag) |
| 618 | b = wire.AppendVarint(b, uint64(v)) |
| 619 | return b, nil |
| 620 | } |
| 621 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 622 | func consumeEnumIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, _ unmarshalOptions) (interface{}, int, error) { |
| 623 | if wtyp != wire.VarintType { |
| 624 | return nil, 0, errUnknown |
| 625 | } |
| 626 | v, n := wire.ConsumeVarint(b) |
| 627 | if n < 0 { |
| 628 | return nil, 0, wire.ParseError(n) |
| 629 | } |
| 630 | rv := reflect.New(reflect.TypeOf(ival)).Elem() |
| 631 | rv.SetInt(int64(v)) |
| 632 | return rv.Interface(), n, nil |
| 633 | } |
| 634 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 635 | var coderEnumIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 636 | size: sizeEnumIface, |
| 637 | marshal: appendEnumIface, |
| 638 | unmarshal: consumeEnumIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | func sizeEnumSliceIface(ival interface{}, tagsize int, opts marshalOptions) (size int) { |
| 642 | return sizeEnumSliceReflect(reflect.ValueOf(ival).Elem(), tagsize, opts) |
| 643 | } |
| 644 | |
| 645 | func sizeEnumSliceReflect(s reflect.Value, tagsize int, _ marshalOptions) (size int) { |
| 646 | for i, llen := 0, s.Len(); i < llen; i++ { |
| 647 | size += wire.SizeVarint(uint64(s.Index(i).Int())) + tagsize |
| 648 | } |
| 649 | return size |
| 650 | } |
| 651 | |
| 652 | func appendEnumSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 653 | return appendEnumSliceReflect(b, reflect.ValueOf(ival).Elem(), wiretag, opts) |
| 654 | } |
| 655 | |
| 656 | func appendEnumSliceReflect(b []byte, s reflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 657 | for i, llen := 0, s.Len(); i < llen; i++ { |
| 658 | b = wire.AppendVarint(b, wiretag) |
| 659 | b = wire.AppendVarint(b, uint64(s.Index(i).Int())) |
| 660 | } |
| 661 | return b, nil |
| 662 | } |
| 663 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 664 | func consumeEnumSliceIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 665 | n, err := consumeEnumSliceReflect(b, reflect.ValueOf(ival), wtyp, opts) |
| 666 | return ival, n, err |
| 667 | } |
| 668 | |
| 669 | func consumeEnumSliceReflect(b []byte, s reflect.Value, wtyp wire.Type, _ unmarshalOptions) (n int, err error) { |
| 670 | s = s.Elem() // *[]E -> []E |
| 671 | if wtyp == wire.BytesType { |
| 672 | b, n = wire.ConsumeBytes(b) |
| 673 | if n < 0 { |
| 674 | return 0, wire.ParseError(n) |
| 675 | } |
| 676 | for len(b) > 0 { |
| 677 | v, n := wire.ConsumeVarint(b) |
| 678 | if n < 0 { |
| 679 | return 0, wire.ParseError(n) |
| 680 | } |
| 681 | rv := reflect.New(s.Type().Elem()).Elem() |
| 682 | rv.SetInt(int64(v)) |
| 683 | s.Set(reflect.Append(s, rv)) |
| 684 | b = b[n:] |
| 685 | } |
| 686 | return n, nil |
| 687 | } |
| 688 | if wtyp != wire.VarintType { |
| 689 | return 0, errUnknown |
| 690 | } |
| 691 | v, n := wire.ConsumeVarint(b) |
| 692 | if n < 0 { |
| 693 | return 0, wire.ParseError(n) |
| 694 | } |
| 695 | rv := reflect.New(s.Type().Elem()).Elem() |
| 696 | rv.SetInt(int64(v)) |
| 697 | s.Set(reflect.Append(s, rv)) |
| 698 | return n, nil |
| 699 | } |
| 700 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 701 | var coderEnumSliceIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 702 | size: sizeEnumSliceIface, |
| 703 | marshal: appendEnumSliceIface, |
| 704 | unmarshal: consumeEnumSliceIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 707 | func sizeEnumPackedSliceIface(ival interface{}, tagsize int, opts marshalOptions) (size int) { |
| 708 | return sizeEnumPackedSliceReflect(reflect.ValueOf(ival).Elem(), tagsize, opts) |
| 709 | } |
| 710 | |
| 711 | func sizeEnumPackedSliceReflect(s reflect.Value, tagsize int, _ marshalOptions) (size int) { |
| 712 | llen := s.Len() |
| 713 | if llen == 0 { |
| 714 | return 0 |
| 715 | } |
| 716 | n := 0 |
| 717 | for i := 0; i < llen; i++ { |
| 718 | n += wire.SizeVarint(uint64(s.Index(i).Int())) |
| 719 | } |
| 720 | return tagsize + wire.SizeBytes(n) |
| 721 | } |
| 722 | |
| 723 | func appendEnumPackedSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 724 | return appendEnumPackedSliceReflect(b, reflect.ValueOf(ival).Elem(), wiretag, opts) |
| 725 | } |
| 726 | |
| 727 | func appendEnumPackedSliceReflect(b []byte, s reflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 728 | llen := s.Len() |
| 729 | if llen == 0 { |
| 730 | return b, nil |
| 731 | } |
| 732 | b = wire.AppendVarint(b, wiretag) |
| 733 | n := 0 |
| 734 | for i := 0; i < llen; i++ { |
| 735 | n += wire.SizeVarint(uint64(s.Index(i).Int())) |
| 736 | } |
| 737 | b = wire.AppendVarint(b, uint64(n)) |
| 738 | for i := 0; i < llen; i++ { |
| 739 | b = wire.AppendVarint(b, uint64(s.Index(i).Int())) |
| 740 | } |
| 741 | return b, nil |
| 742 | } |
| 743 | |
| 744 | var coderEnumPackedSliceIface = ifaceCoderFuncs{ |
| 745 | size: sizeEnumPackedSliceIface, |
| 746 | marshal: appendEnumPackedSliceIface, |
| 747 | unmarshal: consumeEnumSliceIface, |
| 748 | } |
| 749 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 750 | // Strings with UTF8 validation. |
| 751 | |
| 752 | func appendStringValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 753 | v := *p.String() |
| 754 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 755 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 756 | if !utf8.ValidString(v) { |
| 757 | return b, errInvalidUTF8{} |
| 758 | } |
| 759 | return b, nil |
| 760 | } |
| 761 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 762 | func consumeStringValidateUTF8(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (n int, err error) { |
| 763 | if wtyp != wire.BytesType { |
| 764 | return 0, errUnknown |
| 765 | } |
| 766 | v, n := wire.ConsumeString(b) |
| 767 | if n < 0 { |
| 768 | return 0, wire.ParseError(n) |
| 769 | } |
| 770 | if !utf8.ValidString(v) { |
| 771 | return 0, errInvalidUTF8{} |
| 772 | } |
| 773 | *p.String() = v |
| 774 | return n, nil |
| 775 | } |
| 776 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 777 | var coderStringValidateUTF8 = pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 778 | size: sizeString, |
| 779 | marshal: appendStringValidateUTF8, |
| 780 | unmarshal: consumeStringValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | func appendStringNoZeroValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 784 | v := *p.String() |
| 785 | if len(v) == 0 { |
| 786 | return b, nil |
| 787 | } |
| 788 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 789 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 790 | if !utf8.ValidString(v) { |
| 791 | return b, errInvalidUTF8{} |
| 792 | } |
| 793 | return b, nil |
| 794 | } |
| 795 | |
| 796 | var coderStringNoZeroValidateUTF8 = pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 797 | size: sizeStringNoZero, |
| 798 | marshal: appendStringNoZeroValidateUTF8, |
| 799 | unmarshal: consumeStringValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | func sizeStringSliceValidateUTF8(p pointer, tagsize int, _ marshalOptions) (size int) { |
| 803 | s := *p.StringSlice() |
| 804 | for _, v := range s { |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 805 | size += tagsize + wire.SizeBytes(len(v)) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 806 | } |
| 807 | return size |
| 808 | } |
| 809 | |
| 810 | func appendStringSliceValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 811 | s := *p.StringSlice() |
| 812 | var err error |
| 813 | for _, v := range s { |
| 814 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 815 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 816 | if !utf8.ValidString(v) { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 817 | return b, errInvalidUTF8{} |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | return b, err |
| 821 | } |
| 822 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 823 | func consumeStringSliceValidateUTF8(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (n int, err error) { |
| 824 | if wtyp != wire.BytesType { |
| 825 | return 0, errUnknown |
| 826 | } |
| 827 | sp := p.StringSlice() |
| 828 | v, n := wire.ConsumeString(b) |
| 829 | if n < 0 { |
| 830 | return 0, wire.ParseError(n) |
| 831 | } |
| 832 | if !utf8.ValidString(v) { |
| 833 | return 0, errInvalidUTF8{} |
| 834 | } |
| 835 | *sp = append(*sp, v) |
| 836 | return n, nil |
| 837 | } |
| 838 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 839 | var coderStringSliceValidateUTF8 = pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 840 | size: sizeStringSliceValidateUTF8, |
| 841 | marshal: appendStringSliceValidateUTF8, |
| 842 | unmarshal: consumeStringSliceValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | func sizeStringIfaceValidateUTF8(ival interface{}, tagsize int, _ marshalOptions) int { |
| 846 | v := ival.(string) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 847 | return tagsize + wire.SizeBytes(len(v)) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | func appendStringIfaceValidateUTF8(b []byte, ival interface{}, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 851 | v := ival.(string) |
| 852 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 853 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 854 | if !utf8.ValidString(v) { |
| 855 | return b, errInvalidUTF8{} |
| 856 | } |
| 857 | return b, nil |
| 858 | } |
| 859 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 860 | func consumeStringIfaceValidateUTF8(b []byte, _ interface{}, _ wire.Number, wtyp wire.Type, _ unmarshalOptions) (interface{}, int, error) { |
| 861 | if wtyp != wire.BytesType { |
| 862 | return nil, 0, errUnknown |
| 863 | } |
| 864 | v, n := wire.ConsumeString(b) |
| 865 | if n < 0 { |
| 866 | return nil, 0, wire.ParseError(n) |
| 867 | } |
| 868 | if !utf8.ValidString(v) { |
| 869 | return nil, 0, errInvalidUTF8{} |
| 870 | } |
| 871 | return v, n, nil |
| 872 | } |
| 873 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 874 | var coderStringIfaceValidateUTF8 = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 875 | size: sizeStringIfaceValidateUTF8, |
| 876 | marshal: appendStringIfaceValidateUTF8, |
| 877 | unmarshal: consumeStringIfaceValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 878 | } |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 879 | |
| 880 | func asMessage(v reflect.Value) pref.ProtoMessage { |
| 881 | if m, ok := v.Interface().(pref.ProtoMessage); ok { |
| 882 | return m |
| 883 | } |
| 884 | return legacyWrapMessage(v) |
| 885 | } |