feat(aa): add the Merge method to the Rule interface.

This commit is contained in:
Alexandre Pujol 2024-06-20 23:23:39 +01:00
parent d6424cb950
commit 42ca1be858
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
6 changed files with 25 additions and 18 deletions

View file

@ -39,6 +39,7 @@ func (k Kind) Tok() string {
type Rule interface {
Validate() error
Compare(other Rule) int
Merge(other Rule) bool
String() string
Constraint() constraint
Kind() Kind
@ -156,30 +157,20 @@ func (r Rules) Merge() Rules {
if r[i] == nil || r[j] == nil {
continue
}
kindOfI := r[i].Kind()
if kindOfI != r[j].Kind() {
if r[i].Kind() != r[j].Kind() {
continue
}
// If rules are identical, merge them. Ignore comments
if kindOfI != COMMENT && r[i].Compare(r[j]) == 0 {
if r[i].Kind() != COMMENT && r[i].Compare(r[j]) == 0 {
r = r.Delete(j)
j--
continue
}
// File rule
if kindOfI == FILE {
// Merge access
fileI := r[i].(*File)
fileJ := r[j].(*File)
if fileI.Path == fileJ.Path {
fileI.Access = append(fileI.Access, fileJ.Access...)
slices.SortFunc(fileI.Access, compareFileAccess)
fileI.Access = slices.Compact(fileI.Access)
r = r.Delete(j)
j--
}
if r[i].Merge(r[j]) {
r = r.Delete(j)
j--
}
}
}