-
Notifications
You must be signed in to change notification settings - Fork 18
/
normalization.go
69 lines (55 loc) · 1.58 KB
/
normalization.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package compdesc
import (
"sort"
"sync"
"github.com/open-component-model/ocm/v2/pkg/errors"
)
// NormalisationAlgorithm types and versions the algorithm used for digest generation.
type NormalisationAlgorithm = string
const (
JsonNormalisationV1 NormalisationAlgorithm = "jsonNormalisation/v1"
JsonNormalisationV2 NormalisationAlgorithm = "jsonNormalisation/v2"
)
type Normalization interface {
Normalize(cd *ComponentDescriptor) ([]byte, error)
}
type NormalizationAlgorithms struct {
sync.RWMutex
algos map[string]Normalization
}
func (n *NormalizationAlgorithms) Register(name string, norm Normalization) {
n.Lock()
defer n.Unlock()
n.algos[name] = norm
}
func (n *NormalizationAlgorithms) Get(algo string) Normalization {
n.RLock()
defer n.RUnlock()
return n.algos[algo]
}
func (n *NormalizationAlgorithms) Names() []string {
n.RLock()
defer n.RUnlock()
names := []string{}
for n := range n.algos {
names = append(names, n)
}
sort.Strings(names)
return names
}
func (n *NormalizationAlgorithms) Normalize(cd *ComponentDescriptor, algo string) ([]byte, error) {
n.RLock()
defer n.RUnlock()
norm := n.algos[algo]
if norm == nil {
return nil, errors.ErrUnknown("normalization algorithm", algo)
}
return norm.Normalize(cd)
}
var Normalizations = NormalizationAlgorithms{algos: map[string]Normalization{}}
func Normalize(cd *ComponentDescriptor, normAlgo string) ([]byte, error) {
return Normalizations.Normalize(cd, normAlgo)
}