|
1 | 1 | package types |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "slices" |
5 | 6 | ) |
6 | 7 |
|
| 8 | +type PhaseDefaults struct { |
| 9 | + Comment string `yaml:"comment,omitempty"` |
| 10 | + Transformations Transformations `yaml:",inline"` |
| 11 | + Actions *SeclangActions `yaml:"actions"` |
| 12 | +} |
| 13 | + |
7 | 14 | 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"` |
10 | 18 | } |
11 | 19 |
|
12 | 20 | type ConfigurationList struct { |
@@ -93,3 +101,63 @@ func (c *ConfigurationList) ExtractDefaultValues() { |
93 | 101 | c.Global.Version = version |
94 | 102 | c.Global.Tags = tags |
95 | 103 | } |
| 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