Conditions | 40 |
Total Lines | 101 |
Code Lines | 85 |
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 tag.Unmarshal 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. |
||
32 | func Unmarshal(tag string, goType reflect.Type, evs pref.EnumValueDescriptors) pref.FieldDescriptor { |
||
33 | f := new(fdesc.Field) |
||
34 | f.L0.ParentFile = fdesc.SurrogateProto2 |
||
35 | for len(tag) > 0 { |
||
36 | i := strings.IndexByte(tag, ',') |
||
37 | if i < 0 { |
||
38 | i = len(tag) |
||
39 | } |
||
40 | switch s := tag[:i]; { |
||
41 | case strings.HasPrefix(s, "name="): |
||
42 | f.L0.FullName = pref.FullName(s[len("name="):]) |
||
43 | case strings.Trim(s, "0123456789") == "": |
||
44 | n, _ := strconv.ParseUint(s, 10, 32) |
||
45 | f.L1.Number = pref.FieldNumber(n) |
||
46 | case s == "opt": |
||
47 | f.L1.Cardinality = pref.Optional |
||
48 | case s == "req": |
||
49 | f.L1.Cardinality = pref.Required |
||
50 | case s == "rep": |
||
51 | f.L1.Cardinality = pref.Repeated |
||
52 | case s == "varint": |
||
53 | switch goType.Kind() { |
||
54 | case reflect.Bool: |
||
55 | f.L1.Kind = pref.BoolKind |
||
56 | case reflect.Int32: |
||
57 | f.L1.Kind = pref.Int32Kind |
||
58 | case reflect.Int64: |
||
59 | f.L1.Kind = pref.Int64Kind |
||
60 | case reflect.Uint32: |
||
61 | f.L1.Kind = pref.Uint32Kind |
||
62 | case reflect.Uint64: |
||
63 | f.L1.Kind = pref.Uint64Kind |
||
64 | } |
||
65 | case s == "zigzag32": |
||
66 | if goType.Kind() == reflect.Int32 { |
||
67 | f.L1.Kind = pref.Sint32Kind |
||
68 | } |
||
69 | case s == "zigzag64": |
||
70 | if goType.Kind() == reflect.Int64 { |
||
71 | f.L1.Kind = pref.Sint64Kind |
||
72 | } |
||
73 | case s == "fixed32": |
||
74 | switch goType.Kind() { |
||
75 | case reflect.Int32: |
||
76 | f.L1.Kind = pref.Sfixed32Kind |
||
77 | case reflect.Uint32: |
||
78 | f.L1.Kind = pref.Fixed32Kind |
||
79 | case reflect.Float32: |
||
80 | f.L1.Kind = pref.FloatKind |
||
81 | } |
||
82 | case s == "fixed64": |
||
83 | switch goType.Kind() { |
||
84 | case reflect.Int64: |
||
85 | f.L1.Kind = pref.Sfixed64Kind |
||
86 | case reflect.Uint64: |
||
87 | f.L1.Kind = pref.Fixed64Kind |
||
88 | case reflect.Float64: |
||
89 | f.L1.Kind = pref.DoubleKind |
||
90 | } |
||
91 | case s == "bytes": |
||
92 | switch { |
||
93 | case goType.Kind() == reflect.String: |
||
94 | f.L1.Kind = pref.StringKind |
||
95 | case goType.Kind() == reflect.Slice && goType.Elem() == byteType: |
||
96 | f.L1.Kind = pref.BytesKind |
||
97 | default: |
||
98 | f.L1.Kind = pref.MessageKind |
||
99 | } |
||
100 | case s == "group": |
||
101 | f.L1.Kind = pref.GroupKind |
||
102 | case strings.HasPrefix(s, "enum="): |
||
103 | f.L1.Kind = pref.EnumKind |
||
104 | case strings.HasPrefix(s, "json="): |
||
105 | jsonName := s[len("json="):] |
||
106 | if jsonName != strs.JSONCamelCase(string(f.L0.FullName.Name())) { |
||
107 | f.L1.StringName.InitJSON(jsonName) |
||
108 | } |
||
109 | case s == "packed": |
||
110 | f.L1.HasPacked = true |
||
111 | f.L1.IsPacked = true |
||
112 | case strings.HasPrefix(s, "weak="): |
||
113 | f.L1.IsWeak = true |
||
114 | f.L1.Message = fdesc.PlaceholderMessage(pref.FullName(s[len("weak="):])) |
||
115 | case strings.HasPrefix(s, "def="): |
||
116 | // The default tag is special in that everything afterwards is the |
||
117 | // default regardless of the presence of commas. |
||
118 | s, i = tag[len("def="):], len(tag) |
||
119 | v, ev, _ := defval.Unmarshal(s, f.L1.Kind, evs, defval.GoTag) |
||
120 | f.L1.Default = fdesc.DefaultValue(v, ev) |
||
121 | case s == "proto3": |
||
122 | f.L0.ParentFile = fdesc.SurrogateProto3 |
||
123 | } |
||
124 | tag = strings.TrimPrefix(tag[i:], ",") |
||
125 | } |
||
126 | |||
127 | // The generator uses the group message name instead of the field name. |
||
128 | // We obtain the real field name by lowercasing the group name. |
||
129 | if f.L1.Kind == pref.GroupKind { |
||
130 | f.L0.FullName = pref.FullName(strings.ToLower(string(f.L0.FullName))) |
||
131 | } |
||
132 | return f |
||
133 | } |
||
208 |