Passed
Push — master ( 2d3f7e...154da4 )
by Tolga
01:14 queued 13s
created

bundle.Operation   F

Complexity

Conditions 17

Size

Total Lines 145
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 62
nop 2
dl 0
loc 145
rs 1.8
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like bundle.Operation 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 bundle
2
3
import (
4
	"bytes"
5
	"html/template"
6
7
	"github.com/Permify/permify/pkg/attribute"
8
	"github.com/Permify/permify/pkg/database"
9
	base "github.com/Permify/permify/pkg/pb/base/v1"
10
	"github.com/Permify/permify/pkg/tuple"
11
)
12
13
func Operation(arguments map[string]string, operation *base.Operation) (tb database.TupleBundle, ab database.AttributeBundle, err error) {
14
	rWrites := operation.GetRelationshipsWrite()
15
16
	// Initialize a TupleCollection to store the processed tuples.
17
	var wtc database.TupleCollection
18
19
	// Iterate over each write operation.
20
	for _, w := range rWrites {
21
		// Parse the write operation string into a template.
22
		tmpl, err := template.New("template").Parse(w)
23
		if err != nil {
24
			return tb, ab, err
25
		}
26
27
		// Create a buffer to hold the template execution output.
28
		var buf bytes.Buffer
29
30
		// Execute the template with the provided arguments and store the output in the buffer.
31
		err = tmpl.Execute(&buf, arguments)
32
		if err != nil {
33
			return tb, ab, err
34
		}
35
36
		// Convert the write string into a tuple.
37
		t, err := tuple.Tuple(buf.String())
38
		if err != nil {
39
			return tb, ab, err
40
		}
41
42
		// Add the tuple to the write tuple collection.
43
		wtc.Add(t)
44
	}
45
46
	// Assign the processed write tuples to the tuple bundle.
47
	tb.Write = wtc
48
49
	// Retrieve the delete operations from the operation object.
50
	rDeletes := operation.GetRelationshipsDelete()
51
52
	// Initialize a TupleCollection to store the delete tuples.
53
	var dtc database.TupleCollection
54
55
	// Iterate over each delete operation.
56
	for _, w := range rDeletes {
57
		// Parse the write operation string into a template.
58
		tmpl, err := template.New("template").Parse(w)
59
		if err != nil {
60
			return tb, ab, err
61
		}
62
63
		// Create a buffer to hold the template execution output.
64
		var buf bytes.Buffer
65
66
		// Execute the template with the provided arguments and store the output in the buffer.
67
		err = tmpl.Execute(&buf, arguments)
68
		if err != nil {
69
			return tb, ab, err
70
		}
71
72
		// Convert the delete string into a tuple.
73
		t, err := tuple.Tuple(buf.String())
74
		if err != nil {
75
			return tb, ab, err
76
		}
77
78
		// Add the tuple to the delete tuple collection.
79
		dtc.Add(t)
80
	}
81
82
	// Assign the processed delete tuples to the tuple bundle.
83
	tb.Delete = dtc
84
85
	// Retrieve the write operations from the operation object.
86
	aWrites := operation.GetAttributesWrite()
87
88
	// Initialize an AttributeCollection to store the processed attributes.
89
	var wac database.AttributeCollection
90
91
	// Iterate over each write operation.
92
	for _, w := range aWrites {
93
		// Parse the write operation string into a template.
94
		tmpl, err := template.New("template").Parse(w)
95
		if err != nil {
96
			return tb, ab, err
97
		}
98
99
		// Create a buffer to hold the template execution output.
100
		var buf bytes.Buffer
101
102
		// Execute the template with the provided arguments and store the output in the buffer.
103
		err = tmpl.Execute(&buf, arguments)
104
		if err != nil {
105
			return tb, ab, err
106
		}
107
108
		// Convert the write string into an attribute.
109
		a, err := attribute.Attribute(buf.String())
110
		if err != nil {
111
			return tb, ab, err
112
		}
113
114
		// Add the attribute to the write attribute collection.
115
		wac.Add(a)
116
	}
117
118
	// Assign the processed write attributes to the attribute bundle.
119
	ab.Write = wac
120
121
	// Retrieve the delete operations from the operation object.
122
	aDeletes := operation.GetAttributesDelete()
123
124
	// Initialize an AttributeCollection to store the delete attributes.
125
	var dac database.AttributeCollection
126
127
	// Iterate over each delete operation.
128
	for _, w := range aDeletes {
129
		// Parse the write operation string into a template.
130
		tmpl, err := template.New("template").Parse(w)
131
		if err != nil {
132
			return tb, ab, err
133
		}
134
135
		// Create a buffer to hold the template execution output.
136
		var buf bytes.Buffer
137
138
		// Execute the template with the provided arguments and store the output in the buffer.
139
		err = tmpl.Execute(&buf, arguments)
140
		if err != nil {
141
			return tb, ab, err
142
		}
143
144
		// Convert the delete string into an attribute.
145
		a, err := attribute.Attribute(buf.String())
146
		if err != nil {
147
			return tb, ab, err
148
		}
149
150
		// Add the attribute to the delete attribute collection.
151
		dac.Add(a)
152
	}
153
154
	// Assign the processed delete attributes to the attribute bundle.
155
	ab.Delete = dac
156
157
	return tb, ab, nil
158
}
159