4
4
package core
5
5
6
6
import (
7
- "fmt"
8
7
"github.com/daveshanley/vacuum/model"
9
8
vacuumUtils "github.com/daveshanley/vacuum/utils"
10
9
"github.com/pb33f/doctor/model/high/v3"
@@ -48,8 +47,8 @@ func (p Pattern) GetCategory() string {
48
47
// RunRule will execute the Pattern rule, based on supplied context and a supplied []*yaml.Node slice.
49
48
func (p Pattern ) RunRule (nodes []* yaml.Node , context model.RuleFunctionContext ) []model.RuleFunctionResult {
50
49
51
- // check supplied type
52
- props := utils . ConvertInterfaceIntoStringMap ( context .Options )
50
+ // check supplied type - use cached options to avoid repeated interface conversions
51
+ props := context .GetOptionsStringMap ( )
53
52
54
53
if props ["match" ] != "" {
55
54
p .match = props ["match" ] // TODO: there should be no state in here, clean this up.
@@ -105,7 +104,7 @@ func (p Pattern) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
105
104
}
106
105
if p .match != "" {
107
106
rx , err := p .getPatternFromCache (p .match , context .Rule )
108
- expPath := fmt . Sprintf ( "%s['%s']" , pathValue , currentField )
107
+ expPath := model . GetStringTemplates (). BuildQuotedPath ( pathValue , currentField )
109
108
if err != nil {
110
109
locatedObjects , lErr := context .DrDocument .LocateModel (node )
111
110
locatedPath := expPath
@@ -120,8 +119,7 @@ func (p Pattern) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
120
119
}
121
120
result := model.RuleFunctionResult {
122
121
Message : vacuumUtils .SuppliedOrDefault (message ,
123
- fmt .Sprintf ("%s: `%s` cannot be compiled into a regular expression [`%s`]" ,
124
- ruleMessage , p .match , err .Error ())),
122
+ model .GetStringTemplates ().BuildRegexCompileErrorMessage (ruleMessage , p .match , err .Error ())),
125
123
StartNode : node ,
126
124
EndNode : vacuumUtils .BuildEndNode (node ),
127
125
Path : locatedPath ,
@@ -151,8 +149,7 @@ func (p Pattern) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
151
149
}
152
150
result := model.RuleFunctionResult {
153
151
Message : vacuumUtils .SuppliedOrDefault (message ,
154
- fmt .Sprintf ("%s: `%s` does not match the expression `%s`" , ruleMessage ,
155
- node .Value , p .match )),
152
+ model .GetStringTemplates ().BuildPatternMessage (ruleMessage , node .Value , p .match )),
156
153
StartNode : node ,
157
154
EndNode : vacuumUtils .BuildEndNode (node ),
158
155
Path : locatedPath ,
@@ -174,7 +171,7 @@ func (p Pattern) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
174
171
// not match
175
172
if p .notMatch != "" {
176
173
rx , err := p .getPatternFromCache (p .notMatch , context .Rule )
177
- expPath := fmt . Sprintf ( "%s['%s']" , pathValue , currentField )
174
+ expPath := model . GetStringTemplates (). BuildQuotedPath ( pathValue , currentField )
178
175
if err != nil {
179
176
locatedObjects , lErr := context .DrDocument .LocateModel (node )
180
177
locatedPath := expPath
@@ -189,8 +186,7 @@ func (p Pattern) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
189
186
}
190
187
result := model.RuleFunctionResult {
191
188
Message : vacuumUtils .SuppliedOrDefault (message ,
192
- fmt .Sprintf ("%s: cannot be compiled into a regular expression [`%s`]" ,
193
- ruleMessage , err .Error ())),
189
+ model .GetStringTemplates ().BuildRegexCompileErrorMessage (ruleMessage , p .notMatch , err .Error ())),
194
190
StartNode : node ,
195
191
EndNode : vacuumUtils .BuildEndNode (node ),
196
192
Path : locatedPath ,
@@ -222,7 +218,7 @@ func (p Pattern) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
222
218
223
219
result := model.RuleFunctionResult {
224
220
Message : vacuumUtils .SuppliedOrDefault (message ,
225
- fmt . Sprintf ( "%s: matches the expression `%s`" , ruleMessage , p .notMatch )),
221
+ model . GetStringTemplates (). BuildPatternMatchMessage ( ruleMessage , p .notMatch )),
226
222
StartNode : node ,
227
223
EndNode : vacuumUtils .BuildEndNode (node ),
228
224
Path : locatedPath ,
0 commit comments