Skip to content

Commit 3832ce0

Browse files
committed
feat: add Sugar() accessors and rename to SugaredControl
Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
1 parent c0976ef commit 3832ce0

2 files changed

Lines changed: 44 additions & 22 deletions

File tree

control.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
// SPDX-License-Identifier: Apache-2.0
2-
31
package gemara
42

53
import "sync"
64

7-
// SugarControl wraps the generated Control with cached
5+
// SugaredControl wraps the generated Control with cached
86
// cross-reference lookups.
9-
type SugarControl struct {
7+
type SugaredControl struct {
108
Control
119

1210
referencesOnce sync.Once
1311
referencesCache []string
1412
}
1513

16-
func (c *SugarControl) GetMappingReferences() []string {
14+
// Sugar wraps this Control in a SugaredControl for convenient
15+
// cached helper access.
16+
func (c Control) Sugar() *SugaredControl {
17+
return &SugaredControl{Control: c}
18+
}
19+
20+
func (c *SugaredControl) GetMappingReferences() []string {
1721
c.referencesOnce.Do(func() {
1822
for _, ref := range c.Guidelines {
1923
c.referencesCache = append(c.referencesCache, ref.ReferenceId)

control_catalog.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
// SPDX-License-Identifier: Apache-2.0
2-
31
package gemara
42

53
import "sync"
64

7-
// SugarControlCatalog wraps the generated ControlCatalog with
5+
// SugaredControlCatalog wraps the generated ControlCatalog with
86
// pre-built indexes for efficient group, control, and requirement lookups.
9-
type SugarControlCatalog struct {
7+
type SugaredControlCatalog struct {
108
ControlCatalog
119

12-
groupsOnce sync.Once
13-
groupsCache []string
10+
groupsOnce sync.Once
11+
groupsCache []string
12+
13+
sugarControlsOnce sync.Once
14+
sugarControlsCache []*SugaredControl
1415

15-
controlsOnce sync.Once
16-
controlsCache map[string][]Control
16+
controlsByGroupOnce sync.Once
17+
controlsByGroupCache map[string][]*SugaredControl
1718

1819
requirementsOnce sync.Once
1920
requirementsCache map[string][]AssessmentRequirement
2021
}
2122

22-
func (c *SugarControlCatalog) GetGroupNames() []string {
23+
// Sugar wraps this ControlCatalog in a SugaredControlCatalog for
24+
// convenient cached helper access.
25+
func (c ControlCatalog) Sugar() *SugaredControlCatalog {
26+
return &SugaredControlCatalog{ControlCatalog: c}
27+
}
28+
29+
// SugaredControls returns all controls as cached SugaredControl instances.
30+
func (c *SugaredControlCatalog) SugaredControls() []*SugaredControl {
31+
c.sugarControlsOnce.Do(func() {
32+
c.sugarControlsCache = make([]*SugaredControl, len(c.Controls))
33+
for i := range c.Controls {
34+
c.sugarControlsCache[i] = &SugaredControl{Control: c.Controls[i]}
35+
}
36+
})
37+
return c.sugarControlsCache
38+
}
39+
40+
func (c *SugaredControlCatalog) GetGroupNames() []string {
2341
c.groupsOnce.Do(func() {
2442
for _, group := range c.Groups {
2543
c.groupsCache = append(c.groupsCache, group.Title)
@@ -28,19 +46,19 @@ func (c *SugarControlCatalog) GetGroupNames() []string {
2846
return c.groupsCache
2947
}
3048

31-
func (c *SugarControlCatalog) GetControlsForGroup(group string) []Control {
32-
c.controlsOnce.Do(func() {
33-
c.controlsCache = make(map[string][]Control)
34-
for _, control := range c.Controls {
35-
c.controlsCache[control.Group] = append(
36-
c.controlsCache[control.Group], control,
49+
func (c *SugaredControlCatalog) GetControlsForGroup(group string) []*SugaredControl {
50+
c.controlsByGroupOnce.Do(func() {
51+
c.controlsByGroupCache = make(map[string][]*SugaredControl)
52+
for _, sc := range c.SugaredControls() {
53+
c.controlsByGroupCache[sc.Group] = append(
54+
c.controlsByGroupCache[sc.Group], sc,
3755
)
3856
}
3957
})
40-
return c.controlsCache[group]
58+
return c.controlsByGroupCache[group]
4159
}
4260

43-
func (c *SugarControlCatalog) GetRequirementForApplicability(applicability string) []AssessmentRequirement {
61+
func (c *SugaredControlCatalog) GetRequirementForApplicability(applicability string) []AssessmentRequirement {
4462
c.requirementsOnce.Do(func() {
4563
c.requirementsCache = make(map[string][]AssessmentRequirement)
4664
for _, control := range c.Controls {

0 commit comments

Comments
 (0)