| Conditions | 32 |
| Total Lines | 109 |
| Code Lines | 74 |
| 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 attribute.Attribute 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 attribute |
||
| 22 | func Attribute(attribute string) (*base.Attribute, error) { |
||
| 23 | // Splitting the attribute string by "@" delimiter |
||
| 24 | s := strings.Split(strings.TrimSpace(attribute), "|") |
||
| 25 | if len(s) != 2 || s[0] == "" || s[1] == "" { |
||
| 26 | // The attribute string should have exactly two parts |
||
| 27 | return nil, ErrInvalidAttribute |
||
| 28 | } |
||
| 29 | |||
| 30 | // Splitting the entity part of the string by "#" delimiter |
||
| 31 | e := strings.Split(s[0], "$") |
||
| 32 | if len(e) != 2 || e[0] == "" || e[1] == "" { |
||
| 33 | // The entity string should have exactly two parts |
||
| 34 | return nil, ErrInvalidEntity |
||
| 35 | } |
||
| 36 | |||
| 37 | // Splitting the entity type and id by ":" delimiter |
||
| 38 | et := strings.Split(e[0], ":") |
||
| 39 | if len(et) != 2 || et[0] == "" || et[1] == "" { |
||
| 40 | // The entity type and id should have exactly two parts |
||
| 41 | return nil, ErrInvalidAttribute |
||
| 42 | } |
||
| 43 | |||
| 44 | // Splitting the attribute value part of the string by ":" delimiter |
||
| 45 | v := strings.Split(s[1], ":") |
||
| 46 | if len(v) != 2 || v[0] == "" || v[1] == "" { |
||
| 47 | // The attribute value string should have exactly two parts |
||
| 48 | return nil, ErrInvalidAttribute |
||
| 49 | } |
||
| 50 | |||
| 51 | // Declare a proto message to hold the attribute value |
||
| 52 | var wrapped proto.Message |
||
| 53 | |||
| 54 | // Parse the attribute value based on its type |
||
| 55 | switch v[0] { |
||
| 56 | case "boolean": |
||
| 57 | boolVal, err := strconv.ParseBool(v[1]) |
||
| 58 | if err != nil { |
||
| 59 | return nil, fmt.Errorf("failed to parse boolean value: %w", err) |
||
|
|
|||
| 60 | } |
||
| 61 | wrapped = &base.BooleanValue{Data: boolVal} |
||
| 62 | case "boolean[]": |
||
| 63 | val := strings.Split(v[1], ",") |
||
| 64 | ba := make([]bool, len(val)) |
||
| 65 | for i, value := range val { // Parse each boolean |
||
| 66 | boolVal, err := strconv.ParseBool(value) |
||
| 67 | if err != nil { |
||
| 68 | return nil, fmt.Errorf("failed to parse boolean array element: %w", err) |
||
| 69 | } |
||
| 70 | ba[i] = boolVal // Store parsed value |
||
| 71 | } |
||
| 72 | wrapped = &base.BooleanArrayValue{Data: ba} |
||
| 73 | case "string": |
||
| 74 | wrapped = &base.StringValue{Data: v[1]} |
||
| 75 | case "string[]": |
||
| 76 | sa := strings.Split(v[1], ",") |
||
| 77 | wrapped = &base.StringArrayValue{Data: sa} |
||
| 78 | case "double": |
||
| 79 | doubleVal, err := strconv.ParseFloat(v[1], 64) |
||
| 80 | if err != nil { |
||
| 81 | return nil, fmt.Errorf("failed to parse float value: %w", err) |
||
| 82 | } |
||
| 83 | wrapped = &base.DoubleValue{Data: doubleVal} |
||
| 84 | case "double[]": |
||
| 85 | val := strings.Split(v[1], ",") |
||
| 86 | da := make([]float64, len(val)) |
||
| 87 | for i, value := range val { // Parse each double |
||
| 88 | doubleVal, err := strconv.ParseFloat(value, 64) |
||
| 89 | if err != nil { |
||
| 90 | return nil, fmt.Errorf("failed to parse float: %w", err) |
||
| 91 | } |
||
| 92 | da[i] = doubleVal // Store parsed value |
||
| 93 | } |
||
| 94 | wrapped = &base.DoubleArrayValue{Data: da} |
||
| 95 | case "integer": |
||
| 96 | intVal, err := strconv.ParseInt(v[1], 10, 32) |
||
| 97 | if err != nil { |
||
| 98 | return nil, fmt.Errorf("failed to parse integer: %w", err) |
||
| 99 | } |
||
| 100 | wrapped = &base.IntegerValue{Data: int32(intVal)} |
||
| 101 | case "integer[]": |
||
| 102 | val := strings.Split(v[1], ",") |
||
| 103 | ia := make([]int32, len(val)) |
||
| 104 | for i, value := range val { // Parse each integer |
||
| 105 | intVal, err := strconv.ParseInt(value, 10, 32) |
||
| 106 | if err != nil { |
||
| 107 | return nil, fmt.Errorf("failed to parse integer: %w", err) |
||
| 108 | } |
||
| 109 | ia[i] = int32(intVal) // Store parsed value |
||
| 110 | } |
||
| 111 | wrapped = &base.IntegerArrayValue{Data: ia} |
||
| 112 | default: |
||
| 113 | return nil, ErrInvalidValue |
||
| 114 | } |
||
| 115 | |||
| 116 | // Convert the wrapped attribute value into Any proto message |
||
| 117 | value, err := anypb.New(wrapped) |
||
| 118 | if err != nil { |
||
| 119 | return nil, err |
||
| 120 | } |
||
| 121 | |||
| 122 | // Return the attribute object |
||
| 123 | return &base.Attribute{ |
||
| 124 | Entity: &base.Entity{ |
||
| 125 | Type: et[0], |
||
| 126 | Id: et[1], |
||
| 127 | }, |
||
| 128 | Attribute: e[1], |
||
| 129 | Value: value, |
||
| 130 | }, nil |
||
| 131 | } |
||
| 345 |