| 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 |
||
| 47 | func decodeItem(rt reflect.Type, btype binn.Type, bval []byte) (interface{}, error) { |
||
| 48 | 1 | var v interface{} |
|
| 49 | 1 | var err error |
|
| 50 | |||
| 51 | 1 | switch btype { |
|
| 52 | case binn.Null: |
||
| 53 | return nil, nil |
||
| 54 | case binn.True: |
||
| 55 | 1 | return true, nil |
|
| 56 | case binn.False: |
||
| 57 | 1 | return false, nil |
|
| 58 | case binn.Uint8Type: |
||
| 59 | 1 | v = Uint8(bval) |
|
| 60 | case binn.Uint16Type: |
||
| 61 | 1 | v = Uint16(bval) |
|
| 62 | case binn.Uint32Type: |
||
| 63 | 1 | v = Uint32(bval) |
|
| 64 | case binn.Uint64Type: |
||
| 65 | 1 | v = Uint64(bval) |
|
| 66 | case binn.Int8Type: |
||
| 67 | 1 | v = Int8(bval) |
|
| 68 | case binn.Int16Type: |
||
| 69 | 1 | v = Int16(bval) |
|
| 70 | case binn.Int32Type: |
||
| 71 | 1 | v = Int32(bval) |
|
| 72 | case binn.Int64Type: |
||
| 73 | 1 | v = Int64(bval) |
|
| 74 | case binn.Float32Type: |
||
| 75 | 1 | v = Float32(bval) |
|
| 76 | case binn.Float64Type: |
||
| 77 | 1 | v = Float64(bval) |
|
| 78 | case binn.StringType: |
||
| 79 | 1 | v = String(bval[:len(bval)-1]) |
|
| 80 | case binn.BlobType: |
||
| 81 | 1 | v = bval |
|
| 82 | case binn.ListType: |
||
| 83 | 1 | var l []interface{} |
|
| 84 | 1 | br := bytes.NewReader(bval) |
|
| 85 | 1 | _, wasReadLen, _ := readSize(br) |
|
| 86 | 1 | cnt, wasReadCnt, _ := readSize(br) |
|
| 87 | 1 | wasRead := wasReadLen + wasReadCnt |
|
| 88 | |||
| 89 | 1 | err = decodeListItems(br, &l, len(bval)-int(wasRead), wasRead, cnt) |
|
| 90 | 1 | if err != nil { |
|
| 91 | return nil, err |
||
| 92 | } |
||
| 93 | |||
| 94 | 1 | return l, nil |
|
| 95 | case binn.MapType: |
||
| 96 | 1 | br := bytes.NewReader(bval) |
|
| 97 | 1 | sz, rlsize, _ := readSize(br) |
|
| 98 | 1 | cnt, rlcnt, _ := readSize(br) |
|
| 99 | |||
| 100 | 1 | mapType := reflect.MapOf(reflect.TypeOf(int(0)), rt.Elem()) |
|
| 101 | 1 | ptr := reflect.New(mapType) |
|
| 102 | 1 | ptr.Elem().Set(reflect.MakeMap(mapType)) |
|
| 103 | 1 | m := ptr.Interface() |
|
| 104 | |||
| 105 | 1 | err = decodeMapItems(br, m, sz, rlsize+rlcnt, cnt) |
|
| 106 | 1 | if err != nil { |
|
| 107 | return nil, err |
||
| 108 | } |
||
| 109 | |||
| 110 | 1 | return reflect.ValueOf(m).Elem().Interface(), nil |
|
| 111 | case binn.ObjectType: |
||
| 112 | 1 | var obj interface{} |
|
| 113 | 1 | if rt.Kind() == reflect.Interface { |
|
| 114 | obj = map[string]interface{}{} |
||
| 115 | } else { |
||
| 116 | 1 | ptr := reflect.New(rt) |
|
| 117 | 1 | obj = ptr.Interface() |
|
| 118 | } |
||
| 119 | |||
| 120 | 1 | br := bytes.NewReader(bval) |
|
| 121 | |||
| 122 | 1 | err = decodeStorage(btype, br, &obj) |
|
| 123 | |||
| 124 | 1 | if err != nil { |
|
| 125 | return nil, err |
||
| 126 | } |
||
| 127 | |||
| 128 | 1 | return obj, nil |
|
| 129 | } |
||
| 130 | |||
| 131 | 1 | if rt.Kind() == reflect.Interface { |
|
| 132 | 1 | v, err = convertToKind(kindMapper[btype], v) |
|
| 133 | 1 | if err != nil { |
|
| 134 | return nil, err |
||
| 135 | } |
||
| 136 | } else { |
||
| 137 | 1 | v, err = convertToType(rt, v) |
|
| 138 | 1 | if err != nil { |
|
| 139 | return nil, err |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 1 | return v, nil |
|
| 144 | } |
||
| 229 |