-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Simplify Visitor code by moving template logic into package
- Loading branch information
Showing
2 changed files
with
108 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package node | ||
|
||
import ( | ||
"bytes" | ||
"github.com/clevyr/go-yampl/internal/config" | ||
template2 "github.com/clevyr/go-yampl/internal/template" | ||
"github.com/goccy/go-yaml/ast" | ||
"github.com/goccy/go-yaml/token" | ||
log "github.com/sirupsen/logrus" | ||
"text/template" | ||
) | ||
|
||
func Template(conf config.Config, n ast.Node) error { | ||
switch n := n.(type) { | ||
case *ast.MappingValueNode: | ||
if comment := GetCommentTmpl(conf.Prefix, n.Value); comment != "" { | ||
newNode, err := templateComment(conf, comment, n.Value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if newNode != nil { | ||
if err := n.Replace(newNode); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
case *ast.SequenceNode: | ||
for i, value := range n.Values { | ||
if comment := GetCommentTmpl(conf.Prefix, value); comment != "" { | ||
newNode, err := templateComment(conf, comment, value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if newNode != nil { | ||
if err := n.Replace(i, newNode); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func templateComment(conf config.Config, comment string, n ast.Node) (ast.Node, error) { | ||
tmpl, err := template.New(""). | ||
Funcs(template2.FuncMap()). | ||
Delims(conf.LeftDelim, conf.RightDelim). | ||
Option("missingkey=error"). | ||
Parse(comment) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if conf.Values != nil { | ||
conf.Values["Value"] = n.(ast.ScalarNode).GetValue() | ||
} | ||
|
||
logEntry := conf.Log.WithField("yamlpath", n.GetPath()) | ||
|
||
if conf.Strip { | ||
if err := n.SetComment(nil); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err = tmpl.Execute(&buf, conf.Values); err != nil { | ||
if !conf.Fail { | ||
logEntry.WithError(err).Warn("skipping value due to template error") | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
oldVal := n.GetToken().Value | ||
if buf.String() != oldVal { | ||
logEntry.WithFields(log.Fields{ | ||
"tmpl": comment, | ||
"from": oldVal, | ||
"to": buf.String(), | ||
}).Debug("updating value") | ||
|
||
tok := token.New(buf.String(), n.GetToken().Origin, n.GetToken().Position) | ||
var newNode ast.Node | ||
switch tok.Type { | ||
case token.IntegerType, token.BinaryIntegerType, token.OctetIntegerType, token.HexIntegerType: | ||
newNode = ast.Integer(tok) | ||
case token.FloatType: | ||
newNode = ast.Float(tok) | ||
default: | ||
newNode = ast.String(tok) | ||
} | ||
|
||
if err := newNode.SetComment(n.GetComment()); err != nil { | ||
return newNode, err | ||
} | ||
|
||
return newNode, nil | ||
} | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters