Skip to content

Commit

Permalink
(fix): reading in the ruleset after generation #208
Browse files Browse the repository at this point in the history
After generating a ruleset and reading it back in, some values were not properly re-serialized correctly. This fixes #208
  • Loading branch information
daveshanley committed Dec 16, 2022
1 parent 2413115 commit d107096
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cmd/shared_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func PrintBanner() {
pterm.Println()
_ = pterm.DefaultBigText.WithLetters(
putils.LettersFromStringWithRGB("vacuum", pterm.NewRGB(153, 51, 255))).Render()
pterm.Printf("version: %s | compiled: %s\n\n", Version, Date)
pterm.Printf("version: %s | compiled: %s\n", pterm.LightGreen(Version), pterm.LightGreen(Date))
pterm.Println(pterm.Cyan("🔗 https://quobix.com/vacuum | https://github.com/daveshanley/vacuum"))
pterm.Println()
pterm.Println()
}

Expand Down
10 changes: 9 additions & 1 deletion functions/core/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/daveshanley/vacuum/model"
"github.com/daveshanley/vacuum/parser"
"github.com/mitchellh/mapstructure"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -36,7 +37,14 @@ func (sch Schema) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)

var results []model.RuleFunctionResult

schema := utils.ExtractValueFromInterfaceMap("schema", context.Options).(parser.Schema)
var schema parser.Schema
var ok bool
s := utils.ExtractValueFromInterfaceMap("schema", context.Options)
if schema, ok = s.(parser.Schema); !ok {
var p parser.Schema
_ = mapstructure.Decode(s, &p)
schema = p
}

for x, node := range nodes {
if x%2 == 0 && len(nodes) > 1 {
Expand Down
6 changes: 6 additions & 0 deletions functions/openapi/no_eval_descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func (ne NoEvalInDescriptions) RunRule(nodes []*yaml.Node, context model.RuleFun

descriptions := context.Index.GetAllDescriptions()
compiledRegex := context.Rule.PrecompiledPattern
if compiledRegex == nil {
compiledRegex = model.CompileRegex(context, pattern, &results)
if compiledRegex == nil {
return results
}
}

for _, desc := range descriptions {

Expand Down
16 changes: 16 additions & 0 deletions model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"fmt"
"gopkg.in/yaml.v3"
"regexp"
"strings"
)

Expand Down Expand Up @@ -168,3 +169,18 @@ func MapPathAndNodesToResults(path string, startNode, endNode *yaml.Node, result

return mapped
}

// CompileRegex attempts to compile the provided `Pattern` from the ruleset. If it fails, returns nil
// and adds an error to the result set. Any rule using this should then return the results if there is no *Regexp
// returned.
func CompileRegex(context RuleFunctionContext, pattern string, results *[]RuleFunctionResult) *regexp.Regexp {
compiledRegex, err := regexp.Compile(pattern)
if err != nil {
*results = append(*results, RuleFunctionResult{
Message: fmt.Sprintf("Error: cannot run rule, pattern `%s` cannot be compiled", pattern),
Rule: context.Rule,
})
return nil
}
return compiledRegex
}
17 changes: 17 additions & 0 deletions model/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,23 @@ func TestBuildFunctionResultString(t *testing.T) {
BuildFunctionResultString("wow, a cheese ball").Message)
}

func TestCompileRegex(t *testing.T) {

ctx := RuleFunctionContext{}
var res []RuleFunctionResult
regex := CompileRegex(ctx, "type", &res)
assert.True(t, regex.Match([]byte("type")))
assert.Len(t, res, 0)
}

func TestCompileRegex_Fail(t *testing.T) {

ctx := RuleFunctionContext{}
var res []RuleFunctionResult
_ = CompileRegex(ctx, `^\/(?!\/)(.*?)`, &res)
assert.Len(t, res, 1)
}

type dummyFunc struct {
}

Expand Down

0 comments on commit d107096

Please sign in to comment.