Conditions | 116 |
Total Lines | 405 |
Code Lines | 310 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like proto.UnmarshalOptions.unmarshalList often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | // Copyright 2018 The Go Authors. All rights reserved. |
||
194 | func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list protoreflect.List, fd protoreflect.FieldDescriptor) (n int, err error) { |
||
195 | switch fd.Kind() { |
||
196 | case protoreflect.BoolKind: |
||
197 | if wtyp == protowire.BytesType { |
||
198 | buf, n := protowire.ConsumeBytes(b) |
||
199 | if n < 0 { |
||
200 | return 0, errDecode |
||
201 | } |
||
202 | for len(buf) > 0 { |
||
203 | v, n := protowire.ConsumeVarint(buf) |
||
204 | if n < 0 { |
||
205 | return 0, errDecode |
||
206 | } |
||
207 | buf = buf[n:] |
||
208 | list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v))) |
||
209 | } |
||
210 | return n, nil |
||
211 | } |
||
212 | if wtyp != protowire.VarintType { |
||
213 | return 0, errUnknown |
||
214 | } |
||
215 | v, n := protowire.ConsumeVarint(b) |
||
216 | if n < 0 { |
||
217 | return 0, errDecode |
||
218 | } |
||
219 | list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v))) |
||
220 | return n, nil |
||
221 | case protoreflect.EnumKind: |
||
222 | if wtyp == protowire.BytesType { |
||
223 | buf, n := protowire.ConsumeBytes(b) |
||
224 | if n < 0 { |
||
225 | return 0, errDecode |
||
226 | } |
||
227 | for len(buf) > 0 { |
||
228 | v, n := protowire.ConsumeVarint(buf) |
||
229 | if n < 0 { |
||
230 | return 0, errDecode |
||
231 | } |
||
232 | buf = buf[n:] |
||
233 | list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v))) |
||
234 | } |
||
235 | return n, nil |
||
236 | } |
||
237 | if wtyp != protowire.VarintType { |
||
238 | return 0, errUnknown |
||
239 | } |
||
240 | v, n := protowire.ConsumeVarint(b) |
||
241 | if n < 0 { |
||
242 | return 0, errDecode |
||
243 | } |
||
244 | list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v))) |
||
245 | return n, nil |
||
246 | case protoreflect.Int32Kind: |
||
247 | if wtyp == protowire.BytesType { |
||
248 | buf, n := protowire.ConsumeBytes(b) |
||
249 | if n < 0 { |
||
250 | return 0, errDecode |
||
251 | } |
||
252 | for len(buf) > 0 { |
||
253 | v, n := protowire.ConsumeVarint(buf) |
||
254 | if n < 0 { |
||
255 | return 0, errDecode |
||
256 | } |
||
257 | buf = buf[n:] |
||
258 | list.Append(protoreflect.ValueOfInt32(int32(v))) |
||
259 | } |
||
260 | return n, nil |
||
261 | } |
||
262 | if wtyp != protowire.VarintType { |
||
263 | return 0, errUnknown |
||
264 | } |
||
265 | v, n := protowire.ConsumeVarint(b) |
||
266 | if n < 0 { |
||
267 | return 0, errDecode |
||
268 | } |
||
269 | list.Append(protoreflect.ValueOfInt32(int32(v))) |
||
270 | return n, nil |
||
271 | case protoreflect.Sint32Kind: |
||
272 | if wtyp == protowire.BytesType { |
||
273 | buf, n := protowire.ConsumeBytes(b) |
||
274 | if n < 0 { |
||
275 | return 0, errDecode |
||
276 | } |
||
277 | for len(buf) > 0 { |
||
278 | v, n := protowire.ConsumeVarint(buf) |
||
279 | if n < 0 { |
||
280 | return 0, errDecode |
||
281 | } |
||
282 | buf = buf[n:] |
||
283 | list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32)))) |
||
284 | } |
||
285 | return n, nil |
||
286 | } |
||
287 | if wtyp != protowire.VarintType { |
||
288 | return 0, errUnknown |
||
289 | } |
||
290 | v, n := protowire.ConsumeVarint(b) |
||
291 | if n < 0 { |
||
292 | return 0, errDecode |
||
293 | } |
||
294 | list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32)))) |
||
295 | return n, nil |
||
296 | case protoreflect.Uint32Kind: |
||
297 | if wtyp == protowire.BytesType { |
||
298 | buf, n := protowire.ConsumeBytes(b) |
||
299 | if n < 0 { |
||
300 | return 0, errDecode |
||
301 | } |
||
302 | for len(buf) > 0 { |
||
303 | v, n := protowire.ConsumeVarint(buf) |
||
304 | if n < 0 { |
||
305 | return 0, errDecode |
||
306 | } |
||
307 | buf = buf[n:] |
||
308 | list.Append(protoreflect.ValueOfUint32(uint32(v))) |
||
309 | } |
||
310 | return n, nil |
||
311 | } |
||
312 | if wtyp != protowire.VarintType { |
||
313 | return 0, errUnknown |
||
314 | } |
||
315 | v, n := protowire.ConsumeVarint(b) |
||
316 | if n < 0 { |
||
317 | return 0, errDecode |
||
318 | } |
||
319 | list.Append(protoreflect.ValueOfUint32(uint32(v))) |
||
320 | return n, nil |
||
321 | case protoreflect.Int64Kind: |
||
322 | if wtyp == protowire.BytesType { |
||
323 | buf, n := protowire.ConsumeBytes(b) |
||
324 | if n < 0 { |
||
325 | return 0, errDecode |
||
326 | } |
||
327 | for len(buf) > 0 { |
||
328 | v, n := protowire.ConsumeVarint(buf) |
||
329 | if n < 0 { |
||
330 | return 0, errDecode |
||
331 | } |
||
332 | buf = buf[n:] |
||
333 | list.Append(protoreflect.ValueOfInt64(int64(v))) |
||
334 | } |
||
335 | return n, nil |
||
336 | } |
||
337 | if wtyp != protowire.VarintType { |
||
338 | return 0, errUnknown |
||
339 | } |
||
340 | v, n := protowire.ConsumeVarint(b) |
||
341 | if n < 0 { |
||
342 | return 0, errDecode |
||
343 | } |
||
344 | list.Append(protoreflect.ValueOfInt64(int64(v))) |
||
345 | return n, nil |
||
346 | case protoreflect.Sint64Kind: |
||
347 | if wtyp == protowire.BytesType { |
||
348 | buf, n := protowire.ConsumeBytes(b) |
||
349 | if n < 0 { |
||
350 | return 0, errDecode |
||
351 | } |
||
352 | for len(buf) > 0 { |
||
353 | v, n := protowire.ConsumeVarint(buf) |
||
354 | if n < 0 { |
||
355 | return 0, errDecode |
||
356 | } |
||
357 | buf = buf[n:] |
||
358 | list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v))) |
||
359 | } |
||
360 | return n, nil |
||
361 | } |
||
362 | if wtyp != protowire.VarintType { |
||
363 | return 0, errUnknown |
||
364 | } |
||
365 | v, n := protowire.ConsumeVarint(b) |
||
366 | if n < 0 { |
||
367 | return 0, errDecode |
||
368 | } |
||
369 | list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v))) |
||
370 | return n, nil |
||
371 | case protoreflect.Uint64Kind: |
||
372 | if wtyp == protowire.BytesType { |
||
373 | buf, n := protowire.ConsumeBytes(b) |
||
374 | if n < 0 { |
||
375 | return 0, errDecode |
||
376 | } |
||
377 | for len(buf) > 0 { |
||
378 | v, n := protowire.ConsumeVarint(buf) |
||
379 | if n < 0 { |
||
380 | return 0, errDecode |
||
381 | } |
||
382 | buf = buf[n:] |
||
383 | list.Append(protoreflect.ValueOfUint64(v)) |
||
384 | } |
||
385 | return n, nil |
||
386 | } |
||
387 | if wtyp != protowire.VarintType { |
||
388 | return 0, errUnknown |
||
389 | } |
||
390 | v, n := protowire.ConsumeVarint(b) |
||
391 | if n < 0 { |
||
392 | return 0, errDecode |
||
393 | } |
||
394 | list.Append(protoreflect.ValueOfUint64(v)) |
||
395 | return n, nil |
||
396 | case protoreflect.Sfixed32Kind: |
||
397 | if wtyp == protowire.BytesType { |
||
398 | buf, n := protowire.ConsumeBytes(b) |
||
399 | if n < 0 { |
||
400 | return 0, errDecode |
||
401 | } |
||
402 | for len(buf) > 0 { |
||
403 | v, n := protowire.ConsumeFixed32(buf) |
||
404 | if n < 0 { |
||
405 | return 0, errDecode |
||
406 | } |
||
407 | buf = buf[n:] |
||
408 | list.Append(protoreflect.ValueOfInt32(int32(v))) |
||
409 | } |
||
410 | return n, nil |
||
411 | } |
||
412 | if wtyp != protowire.Fixed32Type { |
||
413 | return 0, errUnknown |
||
414 | } |
||
415 | v, n := protowire.ConsumeFixed32(b) |
||
416 | if n < 0 { |
||
417 | return 0, errDecode |
||
418 | } |
||
419 | list.Append(protoreflect.ValueOfInt32(int32(v))) |
||
420 | return n, nil |
||
421 | case protoreflect.Fixed32Kind: |
||
422 | if wtyp == protowire.BytesType { |
||
423 | buf, n := protowire.ConsumeBytes(b) |
||
424 | if n < 0 { |
||
425 | return 0, errDecode |
||
426 | } |
||
427 | for len(buf) > 0 { |
||
428 | v, n := protowire.ConsumeFixed32(buf) |
||
429 | if n < 0 { |
||
430 | return 0, errDecode |
||
431 | } |
||
432 | buf = buf[n:] |
||
433 | list.Append(protoreflect.ValueOfUint32(uint32(v))) |
||
434 | } |
||
435 | return n, nil |
||
436 | } |
||
437 | if wtyp != protowire.Fixed32Type { |
||
438 | return 0, errUnknown |
||
439 | } |
||
440 | v, n := protowire.ConsumeFixed32(b) |
||
441 | if n < 0 { |
||
442 | return 0, errDecode |
||
443 | } |
||
444 | list.Append(protoreflect.ValueOfUint32(uint32(v))) |
||
445 | return n, nil |
||
446 | case protoreflect.FloatKind: |
||
447 | if wtyp == protowire.BytesType { |
||
448 | buf, n := protowire.ConsumeBytes(b) |
||
449 | if n < 0 { |
||
450 | return 0, errDecode |
||
451 | } |
||
452 | for len(buf) > 0 { |
||
453 | v, n := protowire.ConsumeFixed32(buf) |
||
454 | if n < 0 { |
||
455 | return 0, errDecode |
||
456 | } |
||
457 | buf = buf[n:] |
||
458 | list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v)))) |
||
459 | } |
||
460 | return n, nil |
||
461 | } |
||
462 | if wtyp != protowire.Fixed32Type { |
||
463 | return 0, errUnknown |
||
464 | } |
||
465 | v, n := protowire.ConsumeFixed32(b) |
||
466 | if n < 0 { |
||
467 | return 0, errDecode |
||
468 | } |
||
469 | list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v)))) |
||
470 | return n, nil |
||
471 | case protoreflect.Sfixed64Kind: |
||
472 | if wtyp == protowire.BytesType { |
||
473 | buf, n := protowire.ConsumeBytes(b) |
||
474 | if n < 0 { |
||
475 | return 0, errDecode |
||
476 | } |
||
477 | for len(buf) > 0 { |
||
478 | v, n := protowire.ConsumeFixed64(buf) |
||
479 | if n < 0 { |
||
480 | return 0, errDecode |
||
481 | } |
||
482 | buf = buf[n:] |
||
483 | list.Append(protoreflect.ValueOfInt64(int64(v))) |
||
484 | } |
||
485 | return n, nil |
||
486 | } |
||
487 | if wtyp != protowire.Fixed64Type { |
||
488 | return 0, errUnknown |
||
489 | } |
||
490 | v, n := protowire.ConsumeFixed64(b) |
||
491 | if n < 0 { |
||
492 | return 0, errDecode |
||
493 | } |
||
494 | list.Append(protoreflect.ValueOfInt64(int64(v))) |
||
495 | return n, nil |
||
496 | case protoreflect.Fixed64Kind: |
||
497 | if wtyp == protowire.BytesType { |
||
498 | buf, n := protowire.ConsumeBytes(b) |
||
499 | if n < 0 { |
||
500 | return 0, errDecode |
||
501 | } |
||
502 | for len(buf) > 0 { |
||
503 | v, n := protowire.ConsumeFixed64(buf) |
||
504 | if n < 0 { |
||
505 | return 0, errDecode |
||
506 | } |
||
507 | buf = buf[n:] |
||
508 | list.Append(protoreflect.ValueOfUint64(v)) |
||
509 | } |
||
510 | return n, nil |
||
511 | } |
||
512 | if wtyp != protowire.Fixed64Type { |
||
513 | return 0, errUnknown |
||
514 | } |
||
515 | v, n := protowire.ConsumeFixed64(b) |
||
516 | if n < 0 { |
||
517 | return 0, errDecode |
||
518 | } |
||
519 | list.Append(protoreflect.ValueOfUint64(v)) |
||
520 | return n, nil |
||
521 | case protoreflect.DoubleKind: |
||
522 | if wtyp == protowire.BytesType { |
||
523 | buf, n := protowire.ConsumeBytes(b) |
||
524 | if n < 0 { |
||
525 | return 0, errDecode |
||
526 | } |
||
527 | for len(buf) > 0 { |
||
528 | v, n := protowire.ConsumeFixed64(buf) |
||
529 | if n < 0 { |
||
530 | return 0, errDecode |
||
531 | } |
||
532 | buf = buf[n:] |
||
533 | list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v))) |
||
534 | } |
||
535 | return n, nil |
||
536 | } |
||
537 | if wtyp != protowire.Fixed64Type { |
||
538 | return 0, errUnknown |
||
539 | } |
||
540 | v, n := protowire.ConsumeFixed64(b) |
||
541 | if n < 0 { |
||
542 | return 0, errDecode |
||
543 | } |
||
544 | list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v))) |
||
545 | return n, nil |
||
546 | case protoreflect.StringKind: |
||
547 | if wtyp != protowire.BytesType { |
||
548 | return 0, errUnknown |
||
549 | } |
||
550 | v, n := protowire.ConsumeBytes(b) |
||
551 | if n < 0 { |
||
552 | return 0, errDecode |
||
553 | } |
||
554 | if strs.EnforceUTF8(fd) && !utf8.Valid(v) { |
||
555 | return 0, errors.InvalidUTF8(string(fd.FullName())) |
||
556 | } |
||
557 | list.Append(protoreflect.ValueOfString(string(v))) |
||
558 | return n, nil |
||
559 | case protoreflect.BytesKind: |
||
560 | if wtyp != protowire.BytesType { |
||
561 | return 0, errUnknown |
||
562 | } |
||
563 | v, n := protowire.ConsumeBytes(b) |
||
564 | if n < 0 { |
||
565 | return 0, errDecode |
||
566 | } |
||
567 | list.Append(protoreflect.ValueOfBytes(append(emptyBuf[:], v...))) |
||
568 | return n, nil |
||
569 | case protoreflect.MessageKind: |
||
570 | if wtyp != protowire.BytesType { |
||
571 | return 0, errUnknown |
||
572 | } |
||
573 | v, n := protowire.ConsumeBytes(b) |
||
574 | if n < 0 { |
||
575 | return 0, errDecode |
||
576 | } |
||
577 | m := list.NewElement() |
||
578 | if err := o.unmarshalMessage(v, m.Message()); err != nil { |
||
579 | return 0, err |
||
580 | } |
||
581 | list.Append(m) |
||
582 | return n, nil |
||
583 | case protoreflect.GroupKind: |
||
584 | if wtyp != protowire.StartGroupType { |
||
585 | return 0, errUnknown |
||
586 | } |
||
587 | v, n := protowire.ConsumeGroup(fd.Number(), b) |
||
588 | if n < 0 { |
||
589 | return 0, errDecode |
||
590 | } |
||
591 | m := list.NewElement() |
||
592 | if err := o.unmarshalMessage(v, m.Message()); err != nil { |
||
593 | return 0, err |
||
594 | } |
||
595 | list.Append(m) |
||
596 | return n, nil |
||
597 | default: |
||
598 | return 0, errUnknown |
||
599 | } |
||
604 |