/
standards.go
72 lines (61 loc) · 1.77 KB
/
standards.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
70
71
72
/*
Copyright (C) 2018 OpenControl Contributors. See LICENSE.md for license.
*/
package lib
import (
"sync"
"github.com/opencontrol/compliance-masonry/pkg/lib/common"
"github.com/opencontrol/compliance-masonry/pkg/lib/standards"
)
// standardsMap struct is a thread save mapping of Standards
type standardsMap struct {
mapping map[string]common.Standard
sync.RWMutex
}
// newStandards creates an instance of standardsMap struct
func newStandards() *standardsMap {
return &standardsMap{mapping: make(map[string]common.Standard)}
}
// add adds a standard to the standards mapping
func (s *standardsMap) add(standard common.Standard) {
s.Lock()
s.mapping[standard.GetName()] = standard
s.Unlock()
}
// Get retrieves a standard
func (s *standardsMap) get(standardName string) (standard common.Standard, found bool) {
s.Lock()
defer s.Unlock()
standard, found = s.mapping[standardName]
return
}
// GetAll retrieves all the standards
func (s *standardsMap) getAll() []common.Standard {
s.RLock()
defer s.RUnlock()
standardSlice := make([]common.Standard, len(s.mapping))
idx := 0
for _, value := range s.mapping {
standardSlice[idx] = value
idx++
}
return standardSlice
}
// LoadStandard imports a standard into the Standard struct and adds it to the
// main object.
func (ws *localWorkspace) LoadStandard(standardFile string) error {
standard, err := standards.Load(standardFile)
if err != nil {
return common.ErrStandardSchema
}
ws.standards.add(standard)
return nil
}
// GetAllStandards retrieves all standards
func (ws *localWorkspace) GetAllStandards() []common.Standard {
return ws.standards.getAll()
}
// GetStandard retrieves a specific standard
func (ws *localWorkspace) GetStandard(standardKey string) (common.Standard, bool) {
return ws.standards.get(standardKey)
}