-
Notifications
You must be signed in to change notification settings - Fork 18
/
specnorm.go
51 lines (41 loc) · 1.14 KB
/
specnorm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package genericocireg
import (
"slices"
"sync"
)
type SpecificationNormalizer func(s *RepositorySpec)
type Normalizers struct {
lock sync.Mutex
handlers map[string][]SpecificationNormalizer
}
func (n *Normalizers) Register(typ string, f SpecificationNormalizer) {
n.lock.Lock()
defer n.lock.Unlock()
n.handlers[typ] = append(n.handlers[typ], f)
}
func (n *Normalizers) Get(typ string) []SpecificationNormalizer {
n.lock.Lock()
defer n.lock.Unlock()
return slices.Clone(n.handlers[typ])
}
func (n *Normalizers) Normalize(s *RepositorySpec) *RepositorySpec {
n.lock.Lock()
defer n.lock.Unlock()
found := false
for _, f := range n.handlers[s.GetType()] {
found = true
f(s)
}
if !found && s.GetType() != s.GetKind() {
for _, f := range n.handlers[s.GetKind()] {
f(s)
}
}
return s
}
var normalizers = &Normalizers{handlers: map[string][]SpecificationNormalizer{}}
// RegisterSpecificationNormalizer can be used to register OCI repository type
// specific handlers used to normalize an OCI type based OCM repository spec.
func RegisterSpecificationNormalizer(typ string, f SpecificationNormalizer) {
normalizers.Register(typ, f)
}