| Conditions | 26 |
| Total Lines | 97 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 49 |
| CRAP Score | 27.3203 |
| 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 decode.decodeItem 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 decode |
||
| 80 | func decodeItem(rt reflect.Type, btype binn.Type, bval []byte) (interface{}, error) { |
||
| 81 | 1 | var v interface{} |
|
| 82 | 1 | var err error |
|
| 83 | |||
| 84 | 1 | switch btype { |
|
| 85 | case binn.Null: |
||
| 86 | return nil, nil |
||
| 87 | case binn.True: |
||
| 88 | 1 | return true, nil |
|
| 89 | case binn.False: |
||
| 90 | 1 | return false, nil |
|
| 91 | case binn.Uint8Type: |
||
| 92 | 1 | v = Uint8(bval) |
|
| 93 | case binn.Uint16Type: |
||
| 94 | 1 | v = Uint16(bval) |
|
| 95 | case binn.Uint32Type: |
||
| 96 | 1 | v = Uint32(bval) |
|
| 97 | case binn.Uint64Type: |
||
| 98 | 1 | v = Uint64(bval) |
|
| 99 | case binn.Int8Type: |
||
| 100 | 1 | v = Int8(bval) |
|
| 101 | case binn.Int16Type: |
||
| 102 | 1 | v = Int16(bval) |
|
| 103 | case binn.Int32Type: |
||
| 104 | 1 | v = Int32(bval) |
|
| 105 | case binn.Int64Type: |
||
| 106 | 1 | v = Int64(bval) |
|
| 107 | case binn.Float32Type: |
||
| 108 | 1 | v = Float32(bval) |
|
| 109 | case binn.Float64Type: |
||
| 110 | 1 | v = Float64(bval) |
|
| 111 | case binn.StringType: |
||
| 112 | 1 | v = String(bval[:len(bval)-1]) |
|
| 113 | case binn.BlobType: |
||
| 114 | 1 | v = bval |
|
| 115 | case binn.ListType: |
||
| 116 | 1 | var l []interface{} |
|
| 117 | 1 | br := bytes.NewReader(bval) |
|
| 118 | 1 | _, wasReadLen, _ := readSize(br) |
|
| 119 | 1 | cnt, wasReadCnt, _ := readSize(br) |
|
| 120 | 1 | wasRead := wasReadLen + wasReadCnt |
|
| 121 | |||
| 122 | 1 | err = decodeListItems(br, &l, len(bval)-int(wasRead), wasRead, cnt) |
|
| 123 | 1 | if err != nil { |
|
| 124 | return nil, err |
||
| 125 | } |
||
| 126 | |||
| 127 | 1 | return l, nil |
|
| 128 | case binn.MapType: |
||
| 129 | 1 | br := bytes.NewReader(bval) |
|
| 130 | 1 | sz, rlsize, _ := readSize(br) |
|
| 131 | 1 | cnt, rlcnt, _ := readSize(br) |
|
| 132 | |||
| 133 | 1 | mapType := reflect.MapOf(reflect.TypeOf(int(0)), rt.Elem()) |
|
| 134 | 1 | ptr := reflect.New(mapType) |
|
| 135 | 1 | ptr.Elem().Set(reflect.MakeMap(mapType)) |
|
| 136 | 1 | m := ptr.Interface() |
|
| 137 | |||
| 138 | 1 | err = decodeMapItems(br, m, sz, rlsize+rlcnt, cnt) |
|
| 139 | 1 | if err != nil { |
|
| 140 | return nil, err |
||
| 141 | } |
||
| 142 | |||
| 143 | 1 | return reflect.ValueOf(m).Elem().Interface(), nil |
|
| 144 | case binn.ObjectType: |
||
| 145 | 1 | var obj interface{} |
|
| 146 | 1 | if rt.Kind() == reflect.Interface { |
|
| 147 | obj = map[string]interface{}{} |
||
| 148 | } else { |
||
| 149 | 1 | ptr := reflect.New(rt) |
|
| 150 | 1 | obj = ptr.Interface() |
|
| 151 | } |
||
| 152 | |||
| 153 | 1 | br := bytes.NewReader(bval) |
|
| 154 | |||
| 155 | 1 | err = decodeStorage(btype, br, &obj) |
|
| 156 | |||
| 157 | 1 | if err != nil { |
|
| 158 | return nil, err |
||
| 159 | } |
||
| 160 | |||
| 161 | 1 | return obj, nil |
|
| 162 | } |
||
| 163 | |||
| 164 | 1 | if rt.Kind() == reflect.Interface { |
|
| 165 | 1 | v, err = convertToKind(kindMapper[btype], v) |
|
| 166 | 1 | if err != nil { |
|
| 167 | return nil, err |
||
| 168 | } |
||
| 169 | } else { |
||
| 170 | 1 | v, err = convertToType(rt, v) |
|
| 171 | 1 | if err != nil { |
|
| 172 | return nil, err |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | 1 | return v, nil |
|
| 177 | } |
||
| 262 |