| Conditions | 23 |
| Total Lines | 64 |
| Code Lines | 45 |
| 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.Marshal 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. |
||
| 143 | func Marshal(fd pref.FieldDescriptor, enumName string) string { |
||
| 144 | var tag []string |
||
| 145 | switch fd.Kind() { |
||
| 146 | case pref.BoolKind, pref.EnumKind, pref.Int32Kind, pref.Uint32Kind, pref.Int64Kind, pref.Uint64Kind: |
||
| 147 | tag = append(tag, "varint") |
||
| 148 | case pref.Sint32Kind: |
||
| 149 | tag = append(tag, "zigzag32") |
||
| 150 | case pref.Sint64Kind: |
||
| 151 | tag = append(tag, "zigzag64") |
||
| 152 | case pref.Sfixed32Kind, pref.Fixed32Kind, pref.FloatKind: |
||
| 153 | tag = append(tag, "fixed32") |
||
| 154 | case pref.Sfixed64Kind, pref.Fixed64Kind, pref.DoubleKind: |
||
| 155 | tag = append(tag, "fixed64") |
||
| 156 | case pref.StringKind, pref.BytesKind, pref.MessageKind: |
||
| 157 | tag = append(tag, "bytes") |
||
| 158 | case pref.GroupKind: |
||
| 159 | tag = append(tag, "group") |
||
| 160 | } |
||
| 161 | tag = append(tag, strconv.Itoa(int(fd.Number()))) |
||
| 162 | switch fd.Cardinality() { |
||
| 163 | case pref.Optional: |
||
| 164 | tag = append(tag, "opt") |
||
| 165 | case pref.Required: |
||
| 166 | tag = append(tag, "req") |
||
| 167 | case pref.Repeated: |
||
| 168 | tag = append(tag, "rep") |
||
| 169 | } |
||
| 170 | if fd.IsPacked() { |
||
| 171 | tag = append(tag, "packed") |
||
| 172 | } |
||
| 173 | name := string(fd.Name()) |
||
| 174 | if fd.Kind() == pref.GroupKind { |
||
| 175 | // The name of the FieldDescriptor for a group field is |
||
| 176 | // lowercased. To find the original capitalization, we |
||
| 177 | // look in the field's MessageType. |
||
| 178 | name = string(fd.Message().Name()) |
||
| 179 | } |
||
| 180 | tag = append(tag, "name="+name) |
||
| 181 | if jsonName := fd.JSONName(); jsonName != "" && jsonName != name && !fd.IsExtension() { |
||
| 182 | // NOTE: The jsonName != name condition is suspect, but it preserve |
||
| 183 | // the exact same semantics from the previous generator. |
||
| 184 | tag = append(tag, "json="+jsonName) |
||
| 185 | } |
||
| 186 | if fd.IsWeak() { |
||
| 187 | tag = append(tag, "weak="+string(fd.Message().FullName())) |
||
| 188 | } |
||
| 189 | // The previous implementation does not tag extension fields as proto3, |
||
| 190 | // even when the field is defined in a proto3 file. Match that behavior |
||
| 191 | // for consistency. |
||
| 192 | if fd.Syntax() == pref.Proto3 && !fd.IsExtension() { |
||
| 193 | tag = append(tag, "proto3") |
||
| 194 | } |
||
| 195 | if fd.Kind() == pref.EnumKind && enumName != "" { |
||
| 196 | tag = append(tag, "enum="+enumName) |
||
| 197 | } |
||
| 198 | if fd.ContainingOneof() != nil { |
||
| 199 | tag = append(tag, "oneof") |
||
| 200 | } |
||
| 201 | // This must appear last in the tag, since commas in strings aren't escaped. |
||
| 202 | if fd.HasDefault() { |
||
| 203 | def, _ := defval.Marshal(fd.Default(), fd.DefaultEnumValue(), fd.Kind(), defval.GoTag) |
||
| 204 | tag = append(tag, "def="+def) |
||
| 205 | } |
||
| 206 | return strings.Join(tag, ",") |
||
| 207 | } |
||
| 208 |