MCPcopy Create free account
hub / github.com/Permify/permify / ValidateBundleOperation

Function ValidateBundleOperation

internal/validation/validation.go:170–234  ·  view source on GitHub ↗

ValidateBundleOperation checks for duplicate keys in various operations of a bundle.

(op *base.Operation)

Source from the content-addressed store, hash-verified

168
169// ValidateBundleOperation checks for duplicate keys in various operations of a bundle.
170func ValidateBundleOperation(op *base.Operation) error {
171 // Initialize a map to store keys for write operations on relationships.
172 relationshipsWrite := map[string]struct{}{}
173
174 // Iterate over all keys obtained from the GetRelationshipsWrite method.
175 for _, rw := range op.GetRelationshipsWrite() {
176 // Check if the key already exists in the relationshipsWrite map.
177 if _, exists := relationshipsWrite[rw]; exists {
178 // If the key exists, return an error indicating a duplicate key.
179 return errors.New(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())
180 }
181
182 // Add the key to the relationshipsWrite map.
183 relationshipsWrite[rw] = struct{}{}
184 }
185
186 // Initialize a map to store keys for delete operations on relationships.
187 relationshipsDelete := map[string]struct{}{}
188
189 // Iterate over all keys obtained from the GetRelationshipsDelete method.
190 for _, rd := range op.GetRelationshipsDelete() {
191 // Check if the key already exists in the relationshipsDelete map.
192 if _, exists := relationshipsDelete[rd]; exists {
193 // If the key exists, return an error indicating a duplicate key.
194 return errors.New(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())
195 }
196
197 // Add the key to the relationshipsDelete map.
198 relationshipsDelete[rd] = struct{}{}
199 }
200
201 // Initialize a map to store keys for write operations on attributes.
202 attributesWrite := map[string]struct{}{}
203
204 // Iterate over all keys obtained from the GetAttributesWrite method.
205 for _, aw := range op.GetAttributesWrite() {
206 // Check if the key already exists in the attributesWrite map.
207 if _, exists := attributesWrite[aw]; exists {
208 // If the key exists, return an error indicating a duplicate key.
209 return errors.New(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())
210 }
211
212 // Add the key to the attributesWrite map.
213 attributesWrite[aw] = struct{}{}
214 }
215
216 // Initialize a map to store keys for delete operations on attributes.
217 attributesDelete := map[string]struct{}{}
218
219 // Iterate over all keys obtained from the GetAttributesWrite method.
220 // Note: Should this be GetAttributesDelete instead of GetAttributesWrite?
221 for _, ad := range op.GetAttributesDelete() {
222 // Check if the key already exists in the attributesDelete map.
223 if _, exists := attributesDelete[ad]; exists {
224 // If the key exists, return an error indicating a duplicate key.
225 return errors.New(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())
226 }
227

Callers 2

WriteMethod · 0.92
validation_test.goFile · 0.85

Calls 5

GetRelationshipsWriteMethod · 0.80
GetAttributesWriteMethod · 0.80
GetAttributesDeleteMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected