Skip to content

Commit 81bf1d9

Browse files
committed
feat: phase defaults in global config
1 parent 2dd8295 commit 81bf1d9

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ func LoadSeclang(input string) types.ConfigurationList {
108108

109109
// PrintSeclang writes seclang directives to files specified in directive list ids.
110110
func PrintSeclang(configList types.ConfigurationList, dir string) error {
111+
configList.PhaseDefaultsToSeclang()
112+
111113
unfDirs := types.FromCRSLangToUnformattedDirectives(configList)
112114

113115
for _, dirList := range unfDirs.DirectiveList {
@@ -126,6 +128,13 @@ func ToCRSLang(configList types.ConfigurationList) *types.ConfigurationList {
126128
configListWithConditions := types.ToDirectiveWithConditions(configList)
127129

128130
configListWithConditions.ExtractDefaultValues()
131+
132+
err := configListWithConditions.ExtractPhaseDefaults()
133+
134+
if err != nil {
135+
panic(err)
136+
}
137+
129138
return configListWithConditions
130139
}
131140

types/configuration.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package types
22

33
import (
4+
"fmt"
45
"slices"
56
)
67

8+
type PhaseDefaults struct {
9+
Comment string `yaml:"comment,omitempty"`
10+
Transformations Transformations `yaml:",inline"`
11+
Actions *SeclangActions `yaml:"actions"`
12+
}
13+
714
type DefaultConfigs struct {
8-
Version string `yaml:"version,omitempty"`
9-
Tags []string `yaml:"tags,omitempty"`
15+
Version string `yaml:"version,omitempty"`
16+
Tags []string `yaml:"tags,omitempty"`
17+
Phases map[string]PhaseDefaults `yaml:"phases,omitempty"`
1018
}
1119

1220
type ConfigurationList struct {
@@ -93,3 +101,63 @@ func (c *ConfigurationList) ExtractDefaultValues() {
93101
c.Global.Version = version
94102
c.Global.Tags = tags
95103
}
104+
105+
// ExtractPhaseDefaults extract default actions from the directive list and add it to the global config
106+
func (c *ConfigurationList) ExtractPhaseDefaults() error {
107+
defaultActions := map[string]PhaseDefaults{}
108+
109+
for _, dirList := range c.DirectiveList {
110+
for _, directive := range dirList.Directives {
111+
if directive.GetKind() == DefaultActionKind {
112+
da, ok := directive.(DefaultAction)
113+
if !ok {
114+
return fmt.Errorf("Error: casting directive to DefaultAction")
115+
}
116+
pd := PhaseDefaults{
117+
Comment: da.Metadata.Comment,
118+
Transformations: da.Transformations,
119+
Actions: CopyActions(*da.Actions),
120+
}
121+
_, ok = defaultActions[da.Metadata.Phase]
122+
if ok {
123+
return fmt.Errorf("Error: duplicate default actions for phase %s", da.Metadata.Phase)
124+
}
125+
defaultActions[da.Metadata.Phase] = pd
126+
}
127+
}
128+
}
129+
130+
for i := range c.DirectiveList {
131+
c.DirectiveList[i].Directives = slices.DeleteFunc(c.DirectiveList[i].Directives, func(d SeclangDirective) bool {
132+
return d.GetKind() == DefaultActionKind
133+
})
134+
}
135+
136+
c.Global.Phases = defaultActions
137+
return nil
138+
}
139+
140+
// ExtractPhaseDefaults extract default actions from the directive list and add it to the global config
141+
func (c *ConfigurationList) PhaseDefaultsToSeclang() error {
142+
if len(c.Global.Phases) > 0 {
143+
seclangDirs := []SeclangDirective{}
144+
for p, v := range c.Global.Phases {
145+
da := NewDefaultAction()
146+
da.Metadata.SetComment(v.Comment)
147+
da.Metadata.SetPhase(p)
148+
da.Actions = CopyActions(*v.Actions)
149+
seclangDirs = append(seclangDirs, *da)
150+
}
151+
152+
if len(c.DirectiveList) != 0 {
153+
c.DirectiveList[0].Directives = append(seclangDirs, c.DirectiveList[0].Directives...)
154+
} else {
155+
dirList := DirectiveList{}
156+
dirList.Id = "crs-setup"
157+
dirList.Directives = seclangDirs
158+
c.DirectiveList = append(c.DirectiveList, dirList)
159+
}
160+
}
161+
162+
return nil
163+
}

0 commit comments

Comments
 (0)