ApplyOperationsToModel applies all buffered operations to a model and returns the result. This simulates what the model would look like after all operations are applied. It's used for validation and preview purposes within the transaction.
(baseModel model.Model)
| 79 | // This simulates what the model would look like after all operations are applied. |
| 80 | // It's used for validation and preview purposes within the transaction. |
| 81 | func (tb *TransactionBuffer) ApplyOperationsToModel(baseModel model.Model) (model.Model, error) { |
| 82 | tb.mutex.RLock() |
| 83 | defer tb.mutex.RUnlock() |
| 84 | |
| 85 | resultModel := baseModel.Copy() |
| 86 | |
| 87 | for _, op := range tb.operations { |
| 88 | switch op.Type { |
| 89 | case persist.OperationAdd: |
| 90 | for _, rule := range op.Rules { |
| 91 | if err := resultModel.AddPolicy(op.Section, op.PolicyType, rule); err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | } |
| 95 | case persist.OperationRemove: |
| 96 | for _, rule := range op.Rules { |
| 97 | if _, err := resultModel.RemovePolicy(op.Section, op.PolicyType, rule); err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | } |
| 101 | case persist.OperationUpdate: |
| 102 | // For update operations, remove old rules and add new ones. |
| 103 | for i, oldRule := range op.OldRules { |
| 104 | if i < len(op.Rules) { |
| 105 | if _, err := resultModel.RemovePolicy(op.Section, op.PolicyType, oldRule); err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | if err := resultModel.AddPolicy(op.Section, op.PolicyType, op.Rules[i]); err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return resultModel, nil |
| 117 | } |
| 118 | |
| 119 | // HasOperations returns true if there are any buffered operations. |
| 120 | func (tb *TransactionBuffer) HasOperations() bool { |
no test coverage detected