From 1ebb29eba0f11c04948e0db81595187da3281f4d Mon Sep 17 00:00:00 2001 From: Egor Kovetskiy Date: Tue, 7 Jun 2022 14:21:11 +0600 Subject: [PATCH] small style fixes related to templates --- pkg/mark/includes/templates.go | 18 ++++++++++++------ pkg/mark/macro/macro.go | 21 ++++++++++++++++----- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/mark/includes/templates.go b/pkg/mark/includes/templates.go index 27ef0d0..6fac980 100644 --- a/pkg/mark/includes/templates.go +++ b/pkg/mark/includes/templates.go @@ -20,9 +20,9 @@ import ( // --> var reIncludeDirective = regexp.MustCompile( `(?s)` + - ``, + ``, ) func LoadTemplate( @@ -111,15 +111,21 @@ func ProcessIncludes( groups := reIncludeDirective.FindSubmatch(spec) var ( - path, none, left, right, config = string(groups[1]), string(groups[2]), string(groups[3]), string(groups[4]), groups[5] - data = map[string]interface{}{} + path = string(groups[1]) + delimsNone = string(groups[2]) + left = string(groups[3]) + right = string(groups[4]) + config = groups[5] + data = map[string]interface{}{} facts = karma.Describe("path", path) ) - if none == "none" { + + if delimsNone == "none" { left = "\x00" right = "\x01" } + err = yaml.Unmarshal(config, &data) if err != nil { err = facts. diff --git a/pkg/mark/macro/macro.go b/pkg/mark/macro/macro.go index 9ca7126..2c4b547 100644 --- a/pkg/mark/macro/macro.go +++ b/pkg/mark/macro/macro.go @@ -130,10 +130,10 @@ func ExtractMacros( "template", ) config = regexputil.Subexp(reMacroDirective, groups, "config") - - macro Macro ) + var macro Macro + if strings.HasPrefix(template, "#") { cfg := map[string]interface{}{} @@ -143,27 +143,38 @@ func ExtractMacros( err, "unable to unmarshal macros config template", ) + return nil } - body, _ := cfg[template[1:]].(string) + + body, ok := cfg[template[1:]].(string) + if !ok { + err = fmt.Errorf( + "the template config doesn't have '%s' field", + template[1:], + ) + + return nil + } + macro.Template, err = templates.New(template).Parse(body) if err != nil { err = karma.Format( err, "unable to parse template", ) + return nil } } else { macro.Template, err = includes.LoadTemplate(base, template, "{{", "}}", templates) if err != nil { err = karma.Format(err, "unable to load template") - + return nil } } - facts := karma. Describe("template", template). Describe("expr", expr)