Conditions | 201 |
Total Lines | 396 |
Code Lines | 290 |
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.fieldCoder 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. |
||
37 | func fieldCoder(fd pref.FieldDescriptor, ft reflect.Type) (*MessageInfo, pointerCoderFuncs) { |
||
38 | switch { |
||
39 | case fd.IsMap(): |
||
40 | return encoderFuncsForMap(fd, ft) |
||
41 | case fd.Cardinality() == pref.Repeated && !fd.IsPacked(): |
||
42 | // Repeated fields (not packed). |
||
43 | if ft.Kind() != reflect.Slice { |
||
44 | break |
||
45 | } |
||
46 | ft := ft.Elem() |
||
47 | switch fd.Kind() { |
||
48 | case pref.BoolKind: |
||
49 | if ft.Kind() == reflect.Bool { |
||
50 | return nil, coderBoolSlice |
||
51 | } |
||
52 | case pref.EnumKind: |
||
53 | if ft.Kind() == reflect.Int32 { |
||
54 | return nil, coderEnumSlice |
||
55 | } |
||
56 | case pref.Int32Kind: |
||
57 | if ft.Kind() == reflect.Int32 { |
||
58 | return nil, coderInt32Slice |
||
59 | } |
||
60 | case pref.Sint32Kind: |
||
61 | if ft.Kind() == reflect.Int32 { |
||
62 | return nil, coderSint32Slice |
||
63 | } |
||
64 | case pref.Uint32Kind: |
||
65 | if ft.Kind() == reflect.Uint32 { |
||
66 | return nil, coderUint32Slice |
||
67 | } |
||
68 | case pref.Int64Kind: |
||
69 | if ft.Kind() == reflect.Int64 { |
||
70 | return nil, coderInt64Slice |
||
71 | } |
||
72 | case pref.Sint64Kind: |
||
73 | if ft.Kind() == reflect.Int64 { |
||
74 | return nil, coderSint64Slice |
||
75 | } |
||
76 | case pref.Uint64Kind: |
||
77 | if ft.Kind() == reflect.Uint64 { |
||
78 | return nil, coderUint64Slice |
||
79 | } |
||
80 | case pref.Sfixed32Kind: |
||
81 | if ft.Kind() == reflect.Int32 { |
||
82 | return nil, coderSfixed32Slice |
||
83 | } |
||
84 | case pref.Fixed32Kind: |
||
85 | if ft.Kind() == reflect.Uint32 { |
||
86 | return nil, coderFixed32Slice |
||
87 | } |
||
88 | case pref.FloatKind: |
||
89 | if ft.Kind() == reflect.Float32 { |
||
90 | return nil, coderFloatSlice |
||
91 | } |
||
92 | case pref.Sfixed64Kind: |
||
93 | if ft.Kind() == reflect.Int64 { |
||
94 | return nil, coderSfixed64Slice |
||
95 | } |
||
96 | case pref.Fixed64Kind: |
||
97 | if ft.Kind() == reflect.Uint64 { |
||
98 | return nil, coderFixed64Slice |
||
99 | } |
||
100 | case pref.DoubleKind: |
||
101 | if ft.Kind() == reflect.Float64 { |
||
102 | return nil, coderDoubleSlice |
||
103 | } |
||
104 | case pref.StringKind: |
||
105 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
||
106 | return nil, coderStringSliceValidateUTF8 |
||
107 | } |
||
108 | if ft.Kind() == reflect.String { |
||
109 | return nil, coderStringSlice |
||
110 | } |
||
111 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) { |
||
112 | return nil, coderBytesSliceValidateUTF8 |
||
113 | } |
||
114 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
||
115 | return nil, coderBytesSlice |
||
116 | } |
||
117 | case pref.BytesKind: |
||
118 | if ft.Kind() == reflect.String { |
||
119 | return nil, coderStringSlice |
||
120 | } |
||
121 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
||
122 | return nil, coderBytesSlice |
||
123 | } |
||
124 | case pref.MessageKind: |
||
125 | return getMessageInfo(ft), makeMessageSliceFieldCoder(fd, ft) |
||
126 | case pref.GroupKind: |
||
127 | return getMessageInfo(ft), makeGroupSliceFieldCoder(fd, ft) |
||
128 | } |
||
129 | case fd.Cardinality() == pref.Repeated && fd.IsPacked(): |
||
130 | // Packed repeated fields. |
||
131 | // |
||
132 | // Only repeated fields of primitive numeric types |
||
133 | // (Varint, Fixed32, or Fixed64 wire type) can be packed. |
||
134 | if ft.Kind() != reflect.Slice { |
||
135 | break |
||
136 | } |
||
137 | ft := ft.Elem() |
||
138 | switch fd.Kind() { |
||
139 | case pref.BoolKind: |
||
140 | if ft.Kind() == reflect.Bool { |
||
141 | return nil, coderBoolPackedSlice |
||
142 | } |
||
143 | case pref.EnumKind: |
||
144 | if ft.Kind() == reflect.Int32 { |
||
145 | return nil, coderEnumPackedSlice |
||
146 | } |
||
147 | case pref.Int32Kind: |
||
148 | if ft.Kind() == reflect.Int32 { |
||
149 | return nil, coderInt32PackedSlice |
||
150 | } |
||
151 | case pref.Sint32Kind: |
||
152 | if ft.Kind() == reflect.Int32 { |
||
153 | return nil, coderSint32PackedSlice |
||
154 | } |
||
155 | case pref.Uint32Kind: |
||
156 | if ft.Kind() == reflect.Uint32 { |
||
157 | return nil, coderUint32PackedSlice |
||
158 | } |
||
159 | case pref.Int64Kind: |
||
160 | if ft.Kind() == reflect.Int64 { |
||
161 | return nil, coderInt64PackedSlice |
||
162 | } |
||
163 | case pref.Sint64Kind: |
||
164 | if ft.Kind() == reflect.Int64 { |
||
165 | return nil, coderSint64PackedSlice |
||
166 | } |
||
167 | case pref.Uint64Kind: |
||
168 | if ft.Kind() == reflect.Uint64 { |
||
169 | return nil, coderUint64PackedSlice |
||
170 | } |
||
171 | case pref.Sfixed32Kind: |
||
172 | if ft.Kind() == reflect.Int32 { |
||
173 | return nil, coderSfixed32PackedSlice |
||
174 | } |
||
175 | case pref.Fixed32Kind: |
||
176 | if ft.Kind() == reflect.Uint32 { |
||
177 | return nil, coderFixed32PackedSlice |
||
178 | } |
||
179 | case pref.FloatKind: |
||
180 | if ft.Kind() == reflect.Float32 { |
||
181 | return nil, coderFloatPackedSlice |
||
182 | } |
||
183 | case pref.Sfixed64Kind: |
||
184 | if ft.Kind() == reflect.Int64 { |
||
185 | return nil, coderSfixed64PackedSlice |
||
186 | } |
||
187 | case pref.Fixed64Kind: |
||
188 | if ft.Kind() == reflect.Uint64 { |
||
189 | return nil, coderFixed64PackedSlice |
||
190 | } |
||
191 | case pref.DoubleKind: |
||
192 | if ft.Kind() == reflect.Float64 { |
||
193 | return nil, coderDoublePackedSlice |
||
194 | } |
||
195 | } |
||
196 | case fd.Kind() == pref.MessageKind: |
||
197 | return getMessageInfo(ft), makeMessageFieldCoder(fd, ft) |
||
198 | case fd.Kind() == pref.GroupKind: |
||
199 | return getMessageInfo(ft), makeGroupFieldCoder(fd, ft) |
||
200 | case fd.Syntax() == pref.Proto3 && fd.ContainingOneof() == nil: |
||
201 | // Populated oneof fields always encode even if set to the zero value, |
||
202 | // which normally are not encoded in proto3. |
||
203 | switch fd.Kind() { |
||
204 | case pref.BoolKind: |
||
205 | if ft.Kind() == reflect.Bool { |
||
206 | return nil, coderBoolNoZero |
||
207 | } |
||
208 | case pref.EnumKind: |
||
209 | if ft.Kind() == reflect.Int32 { |
||
210 | return nil, coderEnumNoZero |
||
211 | } |
||
212 | case pref.Int32Kind: |
||
213 | if ft.Kind() == reflect.Int32 { |
||
214 | return nil, coderInt32NoZero |
||
215 | } |
||
216 | case pref.Sint32Kind: |
||
217 | if ft.Kind() == reflect.Int32 { |
||
218 | return nil, coderSint32NoZero |
||
219 | } |
||
220 | case pref.Uint32Kind: |
||
221 | if ft.Kind() == reflect.Uint32 { |
||
222 | return nil, coderUint32NoZero |
||
223 | } |
||
224 | case pref.Int64Kind: |
||
225 | if ft.Kind() == reflect.Int64 { |
||
226 | return nil, coderInt64NoZero |
||
227 | } |
||
228 | case pref.Sint64Kind: |
||
229 | if ft.Kind() == reflect.Int64 { |
||
230 | return nil, coderSint64NoZero |
||
231 | } |
||
232 | case pref.Uint64Kind: |
||
233 | if ft.Kind() == reflect.Uint64 { |
||
234 | return nil, coderUint64NoZero |
||
235 | } |
||
236 | case pref.Sfixed32Kind: |
||
237 | if ft.Kind() == reflect.Int32 { |
||
238 | return nil, coderSfixed32NoZero |
||
239 | } |
||
240 | case pref.Fixed32Kind: |
||
241 | if ft.Kind() == reflect.Uint32 { |
||
242 | return nil, coderFixed32NoZero |
||
243 | } |
||
244 | case pref.FloatKind: |
||
245 | if ft.Kind() == reflect.Float32 { |
||
246 | return nil, coderFloatNoZero |
||
247 | } |
||
248 | case pref.Sfixed64Kind: |
||
249 | if ft.Kind() == reflect.Int64 { |
||
250 | return nil, coderSfixed64NoZero |
||
251 | } |
||
252 | case pref.Fixed64Kind: |
||
253 | if ft.Kind() == reflect.Uint64 { |
||
254 | return nil, coderFixed64NoZero |
||
255 | } |
||
256 | case pref.DoubleKind: |
||
257 | if ft.Kind() == reflect.Float64 { |
||
258 | return nil, coderDoubleNoZero |
||
259 | } |
||
260 | case pref.StringKind: |
||
261 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
||
262 | return nil, coderStringNoZeroValidateUTF8 |
||
263 | } |
||
264 | if ft.Kind() == reflect.String { |
||
265 | return nil, coderStringNoZero |
||
266 | } |
||
267 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) { |
||
268 | return nil, coderBytesNoZeroValidateUTF8 |
||
269 | } |
||
270 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
||
271 | return nil, coderBytesNoZero |
||
272 | } |
||
273 | case pref.BytesKind: |
||
274 | if ft.Kind() == reflect.String { |
||
275 | return nil, coderStringNoZero |
||
276 | } |
||
277 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
||
278 | return nil, coderBytesNoZero |
||
279 | } |
||
280 | } |
||
281 | case ft.Kind() == reflect.Ptr: |
||
282 | ft := ft.Elem() |
||
283 | switch fd.Kind() { |
||
284 | case pref.BoolKind: |
||
285 | if ft.Kind() == reflect.Bool { |
||
286 | return nil, coderBoolPtr |
||
287 | } |
||
288 | case pref.EnumKind: |
||
289 | if ft.Kind() == reflect.Int32 { |
||
290 | return nil, coderEnumPtr |
||
291 | } |
||
292 | case pref.Int32Kind: |
||
293 | if ft.Kind() == reflect.Int32 { |
||
294 | return nil, coderInt32Ptr |
||
295 | } |
||
296 | case pref.Sint32Kind: |
||
297 | if ft.Kind() == reflect.Int32 { |
||
298 | return nil, coderSint32Ptr |
||
299 | } |
||
300 | case pref.Uint32Kind: |
||
301 | if ft.Kind() == reflect.Uint32 { |
||
302 | return nil, coderUint32Ptr |
||
303 | } |
||
304 | case pref.Int64Kind: |
||
305 | if ft.Kind() == reflect.Int64 { |
||
306 | return nil, coderInt64Ptr |
||
307 | } |
||
308 | case pref.Sint64Kind: |
||
309 | if ft.Kind() == reflect.Int64 { |
||
310 | return nil, coderSint64Ptr |
||
311 | } |
||
312 | case pref.Uint64Kind: |
||
313 | if ft.Kind() == reflect.Uint64 { |
||
314 | return nil, coderUint64Ptr |
||
315 | } |
||
316 | case pref.Sfixed32Kind: |
||
317 | if ft.Kind() == reflect.Int32 { |
||
318 | return nil, coderSfixed32Ptr |
||
319 | } |
||
320 | case pref.Fixed32Kind: |
||
321 | if ft.Kind() == reflect.Uint32 { |
||
322 | return nil, coderFixed32Ptr |
||
323 | } |
||
324 | case pref.FloatKind: |
||
325 | if ft.Kind() == reflect.Float32 { |
||
326 | return nil, coderFloatPtr |
||
327 | } |
||
328 | case pref.Sfixed64Kind: |
||
329 | if ft.Kind() == reflect.Int64 { |
||
330 | return nil, coderSfixed64Ptr |
||
331 | } |
||
332 | case pref.Fixed64Kind: |
||
333 | if ft.Kind() == reflect.Uint64 { |
||
334 | return nil, coderFixed64Ptr |
||
335 | } |
||
336 | case pref.DoubleKind: |
||
337 | if ft.Kind() == reflect.Float64 { |
||
338 | return nil, coderDoublePtr |
||
339 | } |
||
340 | case pref.StringKind: |
||
341 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
||
342 | return nil, coderStringPtrValidateUTF8 |
||
343 | } |
||
344 | if ft.Kind() == reflect.String { |
||
345 | return nil, coderStringPtr |
||
346 | } |
||
347 | case pref.BytesKind: |
||
348 | if ft.Kind() == reflect.String { |
||
349 | return nil, coderStringPtr |
||
350 | } |
||
351 | } |
||
352 | default: |
||
353 | switch fd.Kind() { |
||
354 | case pref.BoolKind: |
||
355 | if ft.Kind() == reflect.Bool { |
||
356 | return nil, coderBool |
||
357 | } |
||
358 | case pref.EnumKind: |
||
359 | if ft.Kind() == reflect.Int32 { |
||
360 | return nil, coderEnum |
||
361 | } |
||
362 | case pref.Int32Kind: |
||
363 | if ft.Kind() == reflect.Int32 { |
||
364 | return nil, coderInt32 |
||
365 | } |
||
366 | case pref.Sint32Kind: |
||
367 | if ft.Kind() == reflect.Int32 { |
||
368 | return nil, coderSint32 |
||
369 | } |
||
370 | case pref.Uint32Kind: |
||
371 | if ft.Kind() == reflect.Uint32 { |
||
372 | return nil, coderUint32 |
||
373 | } |
||
374 | case pref.Int64Kind: |
||
375 | if ft.Kind() == reflect.Int64 { |
||
376 | return nil, coderInt64 |
||
377 | } |
||
378 | case pref.Sint64Kind: |
||
379 | if ft.Kind() == reflect.Int64 { |
||
380 | return nil, coderSint64 |
||
381 | } |
||
382 | case pref.Uint64Kind: |
||
383 | if ft.Kind() == reflect.Uint64 { |
||
384 | return nil, coderUint64 |
||
385 | } |
||
386 | case pref.Sfixed32Kind: |
||
387 | if ft.Kind() == reflect.Int32 { |
||
388 | return nil, coderSfixed32 |
||
389 | } |
||
390 | case pref.Fixed32Kind: |
||
391 | if ft.Kind() == reflect.Uint32 { |
||
392 | return nil, coderFixed32 |
||
393 | } |
||
394 | case pref.FloatKind: |
||
395 | if ft.Kind() == reflect.Float32 { |
||
396 | return nil, coderFloat |
||
397 | } |
||
398 | case pref.Sfixed64Kind: |
||
399 | if ft.Kind() == reflect.Int64 { |
||
400 | return nil, coderSfixed64 |
||
401 | } |
||
402 | case pref.Fixed64Kind: |
||
403 | if ft.Kind() == reflect.Uint64 { |
||
404 | return nil, coderFixed64 |
||
405 | } |
||
406 | case pref.DoubleKind: |
||
407 | if ft.Kind() == reflect.Float64 { |
||
408 | return nil, coderDouble |
||
409 | } |
||
410 | case pref.StringKind: |
||
411 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
||
412 | return nil, coderStringValidateUTF8 |
||
413 | } |
||
414 | if ft.Kind() == reflect.String { |
||
415 | return nil, coderString |
||
416 | } |
||
417 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) { |
||
418 | return nil, coderBytesValidateUTF8 |
||
419 | } |
||
420 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
||
421 | return nil, coderBytes |
||
422 | } |
||
423 | case pref.BytesKind: |
||
424 | if ft.Kind() == reflect.String { |
||
425 | return nil, coderString |
||
426 | } |
||
427 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
||
428 | return nil, coderBytes |
||
429 | } |
||
430 | } |
||
431 | } |
||
432 | panic(fmt.Sprintf("invalid type: no encoder for %v %v %v/%v", fd.FullName(), fd.Cardinality(), fd.Kind(), ft)) |
||
433 | } |
||
558 |