| Conditions | 35 |
| Total Lines | 117 |
| Code Lines | 88 |
| 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 impl.*MessageInfo.unmarshalPointer 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 2019 The Go Authors. All rights reserved. |
||
| 87 | func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag protowire.Number, opts unmarshalOptions) (out unmarshalOutput, err error) { |
||
| 88 | mi.init() |
||
| 89 | opts.depth-- |
||
| 90 | if opts.depth < 0 { |
||
| 91 | return out, errRecursionDepth |
||
| 92 | } |
||
| 93 | if flags.ProtoLegacy && mi.isMessageSet { |
||
| 94 | return unmarshalMessageSet(mi, b, p, opts) |
||
| 95 | } |
||
| 96 | initialized := true |
||
| 97 | var requiredMask uint64 |
||
| 98 | var exts *map[int32]ExtensionField |
||
| 99 | start := len(b) |
||
| 100 | for len(b) > 0 { |
||
| 101 | // Parse the tag (field number and wire type). |
||
| 102 | var tag uint64 |
||
| 103 | if b[0] < 0x80 { |
||
| 104 | tag = uint64(b[0]) |
||
| 105 | b = b[1:] |
||
| 106 | } else if len(b) >= 2 && b[1] < 128 { |
||
| 107 | tag = uint64(b[0]&0x7f) + uint64(b[1])<<7 |
||
| 108 | b = b[2:] |
||
| 109 | } else { |
||
| 110 | var n int |
||
| 111 | tag, n = protowire.ConsumeVarint(b) |
||
| 112 | if n < 0 { |
||
| 113 | return out, errDecode |
||
| 114 | } |
||
| 115 | b = b[n:] |
||
| 116 | } |
||
| 117 | var num protowire.Number |
||
| 118 | if n := tag >> 3; n < uint64(protowire.MinValidNumber) || n > uint64(protowire.MaxValidNumber) { |
||
| 119 | return out, errDecode |
||
| 120 | } else { |
||
| 121 | num = protowire.Number(n) |
||
| 122 | } |
||
| 123 | wtyp := protowire.Type(tag & 7) |
||
| 124 | |||
| 125 | if wtyp == protowire.EndGroupType { |
||
| 126 | if num != groupTag { |
||
| 127 | return out, errDecode |
||
| 128 | } |
||
| 129 | groupTag = 0 |
||
| 130 | break |
||
| 131 | } |
||
| 132 | |||
| 133 | var f *coderFieldInfo |
||
| 134 | if int(num) < len(mi.denseCoderFields) { |
||
| 135 | f = mi.denseCoderFields[num] |
||
| 136 | } else { |
||
| 137 | f = mi.coderFields[num] |
||
| 138 | } |
||
| 139 | var n int |
||
| 140 | err := errUnknown |
||
| 141 | switch { |
||
| 142 | case f != nil: |
||
| 143 | if f.funcs.unmarshal == nil { |
||
| 144 | break |
||
| 145 | } |
||
| 146 | var o unmarshalOutput |
||
| 147 | o, err = f.funcs.unmarshal(b, p.Apply(f.offset), wtyp, f, opts) |
||
| 148 | n = o.n |
||
| 149 | if err != nil { |
||
| 150 | break |
||
| 151 | } |
||
| 152 | requiredMask |= f.validation.requiredBit |
||
| 153 | if f.funcs.isInit != nil && !o.initialized { |
||
| 154 | initialized = false |
||
| 155 | } |
||
| 156 | default: |
||
| 157 | // Possible extension. |
||
| 158 | if exts == nil && mi.extensionOffset.IsValid() { |
||
| 159 | exts = p.Apply(mi.extensionOffset).Extensions() |
||
| 160 | if *exts == nil { |
||
| 161 | *exts = make(map[int32]ExtensionField) |
||
| 162 | } |
||
| 163 | } |
||
| 164 | if exts == nil { |
||
| 165 | break |
||
| 166 | } |
||
| 167 | var o unmarshalOutput |
||
| 168 | o, err = mi.unmarshalExtension(b, num, wtyp, *exts, opts) |
||
| 169 | if err != nil { |
||
| 170 | break |
||
| 171 | } |
||
| 172 | n = o.n |
||
| 173 | if !o.initialized { |
||
| 174 | initialized = false |
||
| 175 | } |
||
| 176 | } |
||
| 177 | if err != nil { |
||
| 178 | if err != errUnknown { |
||
| 179 | return out, err |
||
| 180 | } |
||
| 181 | n = protowire.ConsumeFieldValue(num, wtyp, b) |
||
| 182 | if n < 0 { |
||
| 183 | return out, errDecode |
||
| 184 | } |
||
| 185 | if !opts.DiscardUnknown() && mi.unknownOffset.IsValid() { |
||
| 186 | u := mi.mutableUnknownBytes(p) |
||
| 187 | *u = protowire.AppendTag(*u, num, wtyp) |
||
| 188 | *u = append(*u, b[:n]...) |
||
| 189 | } |
||
| 190 | } |
||
| 191 | b = b[n:] |
||
| 192 | } |
||
| 193 | if groupTag != 0 { |
||
| 194 | return out, errDecode |
||
| 195 | } |
||
| 196 | if mi.numRequiredFields > 0 && bits.OnesCount64(requiredMask) != int(mi.numRequiredFields) { |
||
| 197 | initialized = false |
||
| 198 | } |
||
| 199 | if initialized { |
||
| 200 | out.initialized = true |
||
| 201 | } |
||
| 202 | out.n = start - len(b) |
||
| 203 | return out, nil |
||
| 204 | } |
||
| 285 |