| Conditions | 58 |
| Total Lines | 169 |
| Code Lines | 132 |
| 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.unmarshalScalar 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. |
||
| 22 | func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd protoreflect.FieldDescriptor) (val protoreflect.Value, n int, err error) { |
||
| 23 | switch fd.Kind() { |
||
| 24 | case protoreflect.BoolKind: |
||
| 25 | if wtyp != protowire.VarintType { |
||
| 26 | return val, 0, errUnknown |
||
| 27 | } |
||
| 28 | v, n := protowire.ConsumeVarint(b) |
||
| 29 | if n < 0 { |
||
| 30 | return val, 0, errDecode |
||
| 31 | } |
||
| 32 | return protoreflect.ValueOfBool(protowire.DecodeBool(v)), n, nil |
||
| 33 | case protoreflect.EnumKind: |
||
| 34 | if wtyp != protowire.VarintType { |
||
| 35 | return val, 0, errUnknown |
||
| 36 | } |
||
| 37 | v, n := protowire.ConsumeVarint(b) |
||
| 38 | if n < 0 { |
||
| 39 | return val, 0, errDecode |
||
| 40 | } |
||
| 41 | return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)), n, nil |
||
| 42 | case protoreflect.Int32Kind: |
||
| 43 | if wtyp != protowire.VarintType { |
||
| 44 | return val, 0, errUnknown |
||
| 45 | } |
||
| 46 | v, n := protowire.ConsumeVarint(b) |
||
| 47 | if n < 0 { |
||
| 48 | return val, 0, errDecode |
||
| 49 | } |
||
| 50 | return protoreflect.ValueOfInt32(int32(v)), n, nil |
||
| 51 | case protoreflect.Sint32Kind: |
||
| 52 | if wtyp != protowire.VarintType { |
||
| 53 | return val, 0, errUnknown |
||
| 54 | } |
||
| 55 | v, n := protowire.ConsumeVarint(b) |
||
| 56 | if n < 0 { |
||
| 57 | return val, 0, errDecode |
||
| 58 | } |
||
| 59 | return protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))), n, nil |
||
| 60 | case protoreflect.Uint32Kind: |
||
| 61 | if wtyp != protowire.VarintType { |
||
| 62 | return val, 0, errUnknown |
||
| 63 | } |
||
| 64 | v, n := protowire.ConsumeVarint(b) |
||
| 65 | if n < 0 { |
||
| 66 | return val, 0, errDecode |
||
| 67 | } |
||
| 68 | return protoreflect.ValueOfUint32(uint32(v)), n, nil |
||
| 69 | case protoreflect.Int64Kind: |
||
| 70 | if wtyp != protowire.VarintType { |
||
| 71 | return val, 0, errUnknown |
||
| 72 | } |
||
| 73 | v, n := protowire.ConsumeVarint(b) |
||
| 74 | if n < 0 { |
||
| 75 | return val, 0, errDecode |
||
| 76 | } |
||
| 77 | return protoreflect.ValueOfInt64(int64(v)), n, nil |
||
| 78 | case protoreflect.Sint64Kind: |
||
| 79 | if wtyp != protowire.VarintType { |
||
| 80 | return val, 0, errUnknown |
||
| 81 | } |
||
| 82 | v, n := protowire.ConsumeVarint(b) |
||
| 83 | if n < 0 { |
||
| 84 | return val, 0, errDecode |
||
| 85 | } |
||
| 86 | return protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)), n, nil |
||
| 87 | case protoreflect.Uint64Kind: |
||
| 88 | if wtyp != protowire.VarintType { |
||
| 89 | return val, 0, errUnknown |
||
| 90 | } |
||
| 91 | v, n := protowire.ConsumeVarint(b) |
||
| 92 | if n < 0 { |
||
| 93 | return val, 0, errDecode |
||
| 94 | } |
||
| 95 | return protoreflect.ValueOfUint64(v), n, nil |
||
| 96 | case protoreflect.Sfixed32Kind: |
||
| 97 | if wtyp != protowire.Fixed32Type { |
||
| 98 | return val, 0, errUnknown |
||
| 99 | } |
||
| 100 | v, n := protowire.ConsumeFixed32(b) |
||
| 101 | if n < 0 { |
||
| 102 | return val, 0, errDecode |
||
| 103 | } |
||
| 104 | return protoreflect.ValueOfInt32(int32(v)), n, nil |
||
| 105 | case protoreflect.Fixed32Kind: |
||
| 106 | if wtyp != protowire.Fixed32Type { |
||
| 107 | return val, 0, errUnknown |
||
| 108 | } |
||
| 109 | v, n := protowire.ConsumeFixed32(b) |
||
| 110 | if n < 0 { |
||
| 111 | return val, 0, errDecode |
||
| 112 | } |
||
| 113 | return protoreflect.ValueOfUint32(uint32(v)), n, nil |
||
| 114 | case protoreflect.FloatKind: |
||
| 115 | if wtyp != protowire.Fixed32Type { |
||
| 116 | return val, 0, errUnknown |
||
| 117 | } |
||
| 118 | v, n := protowire.ConsumeFixed32(b) |
||
| 119 | if n < 0 { |
||
| 120 | return val, 0, errDecode |
||
| 121 | } |
||
| 122 | return protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))), n, nil |
||
| 123 | case protoreflect.Sfixed64Kind: |
||
| 124 | if wtyp != protowire.Fixed64Type { |
||
| 125 | return val, 0, errUnknown |
||
| 126 | } |
||
| 127 | v, n := protowire.ConsumeFixed64(b) |
||
| 128 | if n < 0 { |
||
| 129 | return val, 0, errDecode |
||
| 130 | } |
||
| 131 | return protoreflect.ValueOfInt64(int64(v)), n, nil |
||
| 132 | case protoreflect.Fixed64Kind: |
||
| 133 | if wtyp != protowire.Fixed64Type { |
||
| 134 | return val, 0, errUnknown |
||
| 135 | } |
||
| 136 | v, n := protowire.ConsumeFixed64(b) |
||
| 137 | if n < 0 { |
||
| 138 | return val, 0, errDecode |
||
| 139 | } |
||
| 140 | return protoreflect.ValueOfUint64(v), n, nil |
||
| 141 | case protoreflect.DoubleKind: |
||
| 142 | if wtyp != protowire.Fixed64Type { |
||
| 143 | return val, 0, errUnknown |
||
| 144 | } |
||
| 145 | v, n := protowire.ConsumeFixed64(b) |
||
| 146 | if n < 0 { |
||
| 147 | return val, 0, errDecode |
||
| 148 | } |
||
| 149 | return protoreflect.ValueOfFloat64(math.Float64frombits(v)), n, nil |
||
| 150 | case protoreflect.StringKind: |
||
| 151 | if wtyp != protowire.BytesType { |
||
| 152 | return val, 0, errUnknown |
||
| 153 | } |
||
| 154 | v, n := protowire.ConsumeBytes(b) |
||
| 155 | if n < 0 { |
||
| 156 | return val, 0, errDecode |
||
| 157 | } |
||
| 158 | if strs.EnforceUTF8(fd) && !utf8.Valid(v) { |
||
| 159 | return protoreflect.Value{}, 0, errors.InvalidUTF8(string(fd.FullName())) |
||
| 160 | } |
||
| 161 | return protoreflect.ValueOfString(string(v)), n, nil |
||
| 162 | case protoreflect.BytesKind: |
||
| 163 | if wtyp != protowire.BytesType { |
||
| 164 | return val, 0, errUnknown |
||
| 165 | } |
||
| 166 | v, n := protowire.ConsumeBytes(b) |
||
| 167 | if n < 0 { |
||
| 168 | return val, 0, errDecode |
||
| 169 | } |
||
| 170 | return protoreflect.ValueOfBytes(append(emptyBuf[:], v...)), n, nil |
||
| 171 | case protoreflect.MessageKind: |
||
| 172 | if wtyp != protowire.BytesType { |
||
| 173 | return val, 0, errUnknown |
||
| 174 | } |
||
| 175 | v, n := protowire.ConsumeBytes(b) |
||
| 176 | if n < 0 { |
||
| 177 | return val, 0, errDecode |
||
| 178 | } |
||
| 179 | return protoreflect.ValueOfBytes(v), n, nil |
||
| 180 | case protoreflect.GroupKind: |
||
| 181 | if wtyp != protowire.StartGroupType { |
||
| 182 | return val, 0, errUnknown |
||
| 183 | } |
||
| 184 | v, n := protowire.ConsumeGroup(fd.Number(), b) |
||
| 185 | if n < 0 { |
||
| 186 | return val, 0, errDecode |
||
| 187 | } |
||
| 188 | return protoreflect.ValueOfBytes(v), n, nil |
||
| 189 | default: |
||
| 190 | return val, 0, errUnknown |
||
| 191 | } |
||
| 604 |