Conditions | 33 |
Total Lines | 114 |
Code Lines | 77 |
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: %v", err) |
||
60 | } |
||
61 | wrapped = &base.BooleanValue{Data: boolVal} |
||
62 | case "boolean[]": |
||
63 | var ba []bool |
||
64 | val := strings.Split(v[1], ",") |
||
65 | for _, value := range val { |
||
66 | boolVal, err := strconv.ParseBool(value) |
||
67 | if err != nil { |
||
68 | return nil, fmt.Errorf("failed to parse boolean: %v", err) |
||
69 | } |
||
70 | ba = append(ba, boolVal) |
||
71 | } |
||
72 | wrapped = &base.BooleanArrayValue{Data: ba} |
||
73 | case "string": |
||
74 | wrapped = &base.StringValue{Data: v[1]} |
||
75 | case "string[]": |
||
76 | var sa []string |
||
77 | val := strings.Split(v[1], ",") |
||
78 | for _, value := range val { |
||
79 | sa = append(sa, value) |
||
80 | } |
||
81 | wrapped = &base.StringArrayValue{Data: sa} |
||
82 | case "double": |
||
83 | doubleVal, err := strconv.ParseFloat(v[1], 64) |
||
84 | if err != nil { |
||
85 | return nil, fmt.Errorf("failed to parse float: %v", err) |
||
86 | } |
||
87 | wrapped = &base.DoubleValue{Data: doubleVal} |
||
88 | case "double[]": |
||
89 | var da []float64 |
||
90 | val := strings.Split(v[1], ",") |
||
91 | for _, value := range val { |
||
92 | doubleVal, err := strconv.ParseFloat(value, 64) |
||
93 | if err != nil { |
||
94 | return nil, fmt.Errorf("failed to parse float: %v", err) |
||
95 | } |
||
96 | da = append(da, doubleVal) |
||
97 | } |
||
98 | wrapped = &base.DoubleArrayValue{Data: da} |
||
99 | case "integer": |
||
100 | intVal, err := strconv.ParseInt(v[1], 10, 32) |
||
101 | if err != nil { |
||
102 | return nil, fmt.Errorf("failed to parse integer: %v", err) |
||
103 | } |
||
104 | wrapped = &base.IntegerValue{Data: int32(intVal)} |
||
105 | case "integer[]": |
||
106 | |||
107 | var ia []int32 |
||
108 | val := strings.Split(v[1], ",") |
||
109 | for _, value := range val { |
||
110 | intVal, err := strconv.ParseInt(value, 10, 32) |
||
111 | if err != nil { |
||
112 | return nil, fmt.Errorf("failed to parse integer: %v", err) |
||
113 | } |
||
114 | ia = append(ia, int32(intVal)) |
||
115 | } |
||
116 | wrapped = &base.IntegerArrayValue{Data: ia} |
||
117 | default: |
||
118 | return nil, ErrInvalidValue |
||
119 | } |
||
120 | |||
121 | // Convert the wrapped attribute value into Any proto message |
||
122 | value, err := anypb.New(wrapped) |
||
123 | if err != nil { |
||
124 | return nil, err |
||
125 | } |
||
126 | |||
127 | // Return the attribute object |
||
128 | return &base.Attribute{ |
||
129 | Entity: &base.Entity{ |
||
130 | Type: et[0], |
||
131 | Id: et[1], |
||
132 | }, |
||
133 | Attribute: e[1], |
||
134 | Value: value, |
||
135 | }, nil |
||
136 | } |
||
358 |