| Conditions | 24 |
| Total Lines | 82 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 21 |
| CRAP Score | 111.3649 |
| 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 encode.newTypeEncoder 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 | package encode |
||
| 65 | func newTypeEncoder(t reflect.Type) encoderFunc { |
||
| 66 | 1 | if t.Implements(marshalerType) { |
|
| 67 | 1 | return marshalerEncoder |
|
| 68 | } |
||
| 69 | 1 | if t.Implements(textMarshalerType) { |
|
| 70 | return textMarshalerEncoder |
||
| 71 | } |
||
| 72 | |||
| 73 | 1 | switch t.Kind() { |
|
| 74 | case reflect.Bool: |
||
| 75 | return func(v reflect.Value) ([]byte, error) { |
||
| 76 | if v.Bool() { |
||
| 77 | return []byte{binn.True}, nil |
||
| 78 | } |
||
| 79 | return []byte{binn.False}, nil |
||
| 80 | } |
||
| 81 | case reflect.Struct: |
||
| 82 | 1 | return newStructEncoder(t) |
|
| 83 | case reflect.Map: |
||
| 84 | 1 | return newMapEncoder(t) |
|
| 85 | case reflect.Interface: |
||
| 86 | 1 | return func(v reflect.Value) ([]byte, error) { |
|
| 87 | 1 | if v.IsNil() { |
|
| 88 | return []byte{binn.Null}, nil |
||
| 89 | } |
||
| 90 | |||
| 91 | 1 | return loadEncodeFunc(v.Elem().Type())(v.Elem()) |
|
| 92 | } |
||
| 93 | case reflect.String: |
||
| 94 | 1 | return func(v reflect.Value) ([]byte, error) { |
|
| 95 | 1 | var bytes []byte |
|
| 96 | |||
| 97 | 1 | bytes = append(bytes, Uint8(binn.StringType)...) |
|
| 98 | 1 | bytes = append(bytes, String(v.String())...) |
|
| 99 | 1 | bytes = append(bytes, 0x00) |
|
| 100 | |||
| 101 | 1 | return bytes, nil |
|
| 102 | } |
||
| 103 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
||
| 104 | 1 | return func(v reflect.Value) ([]byte, error) { |
|
| 105 | 1 | var bytes []byte |
|
| 106 | |||
| 107 | 1 | bytes = append(bytes, Uint8(uint8(detectIntType(int(v.Int()))))...) |
|
| 108 | 1 | bytes = append(bytes, Int(int(v.Int()))...) |
|
| 109 | |||
| 110 | 1 | return bytes, nil |
|
| 111 | } |
||
| 112 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
||
| 113 | return func(v reflect.Value) ([]byte, error) { |
||
| 114 | var bytes []byte |
||
| 115 | |||
| 116 | bytes = append(bytes, Uint8(uint8(detectUintType(uint(v.Uint()))))...) |
||
| 117 | bytes = append(bytes, Uint(uint(v.Uint()))...) |
||
| 118 | |||
| 119 | return bytes, nil |
||
| 120 | } |
||
| 121 | case reflect.Float32: |
||
| 122 | return func(v reflect.Value) ([]byte, error) { |
||
| 123 | var bytes []byte |
||
| 124 | |||
| 125 | bytes = append(bytes, Uint8(binn.Float32Type)...) |
||
| 126 | bytes = append(bytes, Float32(float32(v.Float()))...) |
||
| 127 | |||
| 128 | return bytes, nil |
||
| 129 | } |
||
| 130 | case reflect.Float64: |
||
| 131 | return func(v reflect.Value) ([]byte, error) { |
||
| 132 | var bytes []byte |
||
| 133 | |||
| 134 | bytes = append(bytes, Uint8(binn.Float64Type)...) |
||
| 135 | bytes = append(bytes, Float64(v.Float())...) |
||
| 136 | |||
| 137 | return bytes, nil |
||
| 138 | } |
||
| 139 | case reflect.Slice, reflect.Array: |
||
| 140 | 1 | return newArrayEncoder(t) |
|
| 141 | case reflect.Ptr: |
||
| 142 | return newPtrEncoder(t) |
||
| 143 | } |
||
| 144 | |||
| 145 | return func(_ reflect.Value) ([]byte, error) { |
||
| 146 | return nil, &UnsupportedTypeError{t} |
||
| 147 | } |
||
| 165 |