feat(aa-log): add some util functions.

This commit is contained in:
Alexandre Pujol 2024-09-10 18:38:59 +01:00
parent 49b8967bb2
commit e4f963f30f
Failed to generate hash of commit
4 changed files with 201 additions and 102 deletions

81
pkg/util/slice.go Normal file
View file

@ -0,0 +1,81 @@
// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package util
// RemoveDuplicate filter out all duplicates from a slice. Also filter out empty element.
func RemoveDuplicate[T comparable](inlist []T) []T {
var empty T
list := []T{}
seen := map[T]bool{}
seen[empty] = true
for _, item := range inlist {
if _, ok := seen[item]; !ok {
seen[item] = true
list = append(list, item)
}
}
return list
}
// Intersect returns the intersection between two collections.
// From https://github.com/samber/lo
func Intersect[T comparable](list1 []T, list2 []T) []T {
result := []T{}
seen := map[T]struct{}{}
for _, elem := range list1 {
seen[elem] = struct{}{}
}
for _, elem := range list2 {
if _, ok := seen[elem]; ok {
result = append(result, elem)
}
}
return result
}
// Flatten returns an array a single level deep.
// From https://github.com/samber/lo
func Flatten[T comparable](collection [][]T) []T {
totalLen := 0
for i := range collection {
totalLen += len(collection[i])
}
result := make([]T, 0, totalLen)
for i := range collection {
result = append(result, collection[i]...)
}
return result
}
// Invert creates a map composed of the inverted keys and values. If map
// contains duplicate values, subsequent values overwrite property assignments
// of previous values.
// Play: https://go.dev/play/p/rFQ4rak6iA1
func Invert[K comparable, V comparable](in map[K]V) map[V]K {
out := make(map[V]K, len(in))
for k := range in {
out[in[k]] = k
}
return out
}
func InvertFlatten[V comparable](in map[V][]V) map[V]V {
out := make(map[V]V, len(in))
for k := range in {
for _, v := range in[k] {
out[v] = k
}
}
return out
}

120
pkg/util/slice_test.go Normal file
View file

@ -0,0 +1,120 @@
// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package util
import (
"reflect"
"testing"
)
func TestRemoveDuplicate(t *testing.T) {
tests := []struct {
name string
inlist []string
want []string
}{
{
name: "Duplicate",
inlist: []string{"foo", "bar", "foo", "bar", ""},
want: []string{"foo", "bar"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveDuplicate(tt.inlist); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveDuplicate() = %v, want %v", got, tt.want)
}
})
}
}
func TestIntersect(t *testing.T) {
tests := []struct {
name string
list1 []int
list2 []int
want []int
}{
{
name: "1",
list1: []int{0, 1, 2, 3, 4, 5},
list2: []int{0, 2},
want: []int{0, 2},
},
{
name: "2",
list1: []int{0, 1, 2, 3, 4, 5},
list2: []int{0, 6},
want: []int{0},
},
{
name: "3",
list1: []int{0, 1, 2, 3, 4, 5},
list2: []int{-1, 6},
want: []int{},
},
{
name: "4",
list1: []int{0, 6},
list2: []int{0, 1, 2, 3, 4, 5},
want: []int{0},
},
{
name: "5",
list1: []int{0, 6, 0},
list2: []int{0, 1, 2, 3, 4, 5},
want: []int{0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Intersect(tt.list1, tt.list2); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Intersect() = %v, want %v", got, tt.want)
}
})
}
}
func TestFlatten(t *testing.T) {
tests := []struct {
name string
input [][]int
want []int
}{
{
name: "1",
input: [][]int{{0, 1}, {2, 3, 4, 5}},
want: []int{0, 1, 2, 3, 4, 5},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Flatten(tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Intersect() = %v, want %v", got, tt.want)
}
})
}
}
func TestInvert(t *testing.T) {
tests := []struct {
name string
input map[string]int
want map[int]string
}{
{
name: "1",
input: map[string]int{"a": 1, "b": 2},
want: map[int]string{1: "a", 2: "b"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Invert(tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Invert() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -67,40 +67,6 @@ func DecodeHexInString(str string) string {
return str return str
} }
// RemoveDuplicate filter out all duplicates from a slice. Also filter out empty element.
func RemoveDuplicate[T comparable](inlist []T) []T {
var empty T
list := []T{}
seen := map[T]bool{}
seen[empty] = true
for _, item := range inlist {
if _, ok := seen[item]; !ok {
seen[item] = true
list = append(list, item)
}
}
return list
}
// Intersect returns the intersection between two collections.
// From https://github.com/samber/lo
func Intersect[T comparable](list1 []T, list2 []T) []T {
result := []T{}
seen := map[T]struct{}{}
for _, elem := range list1 {
seen[elem] = struct{}{}
}
for _, elem := range list2 {
if _, ok := seen[elem]; ok {
result = append(result, elem)
}
}
return result
}
// CopyTo recursivelly copy all files from a source path to a destination path. // CopyTo recursivelly copy all files from a source path to a destination path.
func CopyTo(src *paths.Path, dst *paths.Path) error { func CopyTo(src *paths.Path, dst *paths.Path) error {
files, err := src.ReadDirRecursiveFiltered(nil, files, err := src.ReadDirRecursiveFiltered(nil,

View file

@ -38,74 +38,6 @@ func TestDecodeHexInString(t *testing.T) {
} }
} }
func TestRemoveDuplicate(t *testing.T) {
tests := []struct {
name string
inlist []string
want []string
}{
{
name: "Duplicate",
inlist: []string{"foo", "bar", "foo", "bar", ""},
want: []string{"foo", "bar"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveDuplicate(tt.inlist); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveDuplicate() = %v, want %v", got, tt.want)
}
})
}
}
func TestIntersect(t *testing.T) {
tests := []struct {
name string
list1 []int
list2 []int
want []int
}{
{
name: "1",
list1: []int{0, 1, 2, 3, 4, 5},
list2: []int{0, 2},
want: []int{0, 2},
},
{
name: "2",
list1: []int{0, 1, 2, 3, 4, 5},
list2: []int{0, 6},
want: []int{0},
},
{
name: "3",
list1: []int{0, 1, 2, 3, 4, 5},
list2: []int{-1, 6},
want: []int{},
},
{
name: "4",
list1: []int{0, 6},
list2: []int{0, 1, 2, 3, 4, 5},
want: []int{0},
},
{
name: "5",
list1: []int{0, 6, 0},
list2: []int{0, 1, 2, 3, 4, 5},
want: []int{0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Intersect(tt.list1, tt.list2); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Intersect() = %v, want %v", got, tt.want)
}
})
}
}
func TestToRegexRepl(t *testing.T) { func TestToRegexRepl(t *testing.T) {
tests := []struct { tests := []struct {
name string name string