1- // SPDX-License-Identifier: Apache-2.0
2-
31package gemara
42
53import "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