| Conditions | 30 |
| Total Lines | 78 |
| Code Lines | 59 |
| 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 defval.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. |
||
| 38 | func Unmarshal(s string, k pref.Kind, evs pref.EnumValueDescriptors, f Format) (pref.Value, pref.EnumValueDescriptor, error) { |
||
| 39 | switch k { |
||
| 40 | case pref.BoolKind: |
||
| 41 | if f == GoTag { |
||
| 42 | switch s { |
||
| 43 | case "1": |
||
| 44 | return pref.ValueOfBool(true), nil, nil |
||
| 45 | case "0": |
||
| 46 | return pref.ValueOfBool(false), nil, nil |
||
| 47 | } |
||
| 48 | } else { |
||
| 49 | switch s { |
||
| 50 | case "true": |
||
| 51 | return pref.ValueOfBool(true), nil, nil |
||
| 52 | case "false": |
||
| 53 | return pref.ValueOfBool(false), nil, nil |
||
| 54 | } |
||
| 55 | } |
||
| 56 | case pref.EnumKind: |
||
| 57 | if f == GoTag { |
||
| 58 | // Go tags use the numeric form of the enum value. |
||
| 59 | if n, err := strconv.ParseInt(s, 10, 32); err == nil { |
||
| 60 | if ev := evs.ByNumber(pref.EnumNumber(n)); ev != nil { |
||
| 61 | return pref.ValueOfEnum(ev.Number()), ev, nil |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } else { |
||
| 65 | // Descriptor default_value use the enum identifier. |
||
| 66 | ev := evs.ByName(pref.Name(s)) |
||
| 67 | if ev != nil { |
||
| 68 | return pref.ValueOfEnum(ev.Number()), ev, nil |
||
| 69 | } |
||
| 70 | } |
||
| 71 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
||
| 72 | if v, err := strconv.ParseInt(s, 10, 32); err == nil { |
||
| 73 | return pref.ValueOfInt32(int32(v)), nil, nil |
||
| 74 | } |
||
| 75 | case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
||
| 76 | if v, err := strconv.ParseInt(s, 10, 64); err == nil { |
||
| 77 | return pref.ValueOfInt64(int64(v)), nil, nil |
||
| 78 | } |
||
| 79 | case pref.Uint32Kind, pref.Fixed32Kind: |
||
| 80 | if v, err := strconv.ParseUint(s, 10, 32); err == nil { |
||
| 81 | return pref.ValueOfUint32(uint32(v)), nil, nil |
||
| 82 | } |
||
| 83 | case pref.Uint64Kind, pref.Fixed64Kind: |
||
| 84 | if v, err := strconv.ParseUint(s, 10, 64); err == nil { |
||
| 85 | return pref.ValueOfUint64(uint64(v)), nil, nil |
||
| 86 | } |
||
| 87 | case pref.FloatKind, pref.DoubleKind: |
||
| 88 | var v float64 |
||
| 89 | var err error |
||
| 90 | switch s { |
||
| 91 | case "-inf": |
||
| 92 | v = math.Inf(-1) |
||
| 93 | case "inf": |
||
| 94 | v = math.Inf(+1) |
||
| 95 | case "nan": |
||
| 96 | v = math.NaN() |
||
| 97 | default: |
||
| 98 | v, err = strconv.ParseFloat(s, 64) |
||
| 99 | } |
||
| 100 | if err == nil { |
||
| 101 | if k == pref.FloatKind { |
||
| 102 | return pref.ValueOfFloat32(float32(v)), nil, nil |
||
| 103 | } else { |
||
| 104 | return pref.ValueOfFloat64(float64(v)), nil, nil |
||
| 105 | } |
||
| 106 | } |
||
| 107 | case pref.StringKind: |
||
| 108 | // String values are already unescaped and can be used as is. |
||
| 109 | return pref.ValueOfString(s), nil, nil |
||
| 110 | case pref.BytesKind: |
||
| 111 | if b, ok := unmarshalBytes(s); ok { |
||
| 112 | return pref.ValueOfBytes(b), nil, nil |
||
| 113 | } |
||
| 114 | } |
||
| 115 | return pref.Value{}, nil, errors.New("could not parse value for %v: %q", k, s) |
||
| 116 | } |
||
| 214 |