Skip to content

Commit

Permalink
♻️ Simplify Visitor code by moving template logic into package
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jun 8, 2022
1 parent 6877207 commit 70b4d97
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 104 deletions.
104 changes: 104 additions & 0 deletions internal/node/template.go
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
}
108 changes: 4 additions & 104 deletions internal/visitor/template_comments.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package visitor

import (
"bytes"
"github.com/clevyr/go-yampl/internal/config"
"github.com/clevyr/go-yampl/internal/node"
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 NewTemplateComments(conf config.Config) TemplateComments {
Expand All @@ -23,46 +18,10 @@ type TemplateComments struct {
}

func (v *TemplateComments) Visit(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.MappingValueNode:
if comment := node.GetCommentTmpl(v.conf.Prefix, n.Value); comment != "" {
newNode, err := templateComment(v.conf, comment, n.Value)
if err != nil {
if v.err == nil {
v.err = node.NewPrintableError(err, n)
}
return nil
}

if newNode != nil {
if err := n.Replace(newNode); err != nil {
if v.err == nil {
v.err = node.NewPrintableError(err, n)
}
return nil
}
}
}
case *ast.SequenceNode:
for i, value := range n.Values {
if comment := node.GetCommentTmpl(v.conf.Prefix, value); comment != "" {
newNode, err := templateComment(v.conf, comment, value)
if err != nil {
if v.err == nil {
v.err = node.NewPrintableError(err, value)
}
return nil
}

if newNode != nil {
if err := n.Replace(i, newNode); err != nil {
if v.err == nil {
v.err = node.NewPrintableError(err, value)
}
return nil
}
}
}
if v.err == nil {
if err := node.Template(v.conf, n); err != nil {
v.err = node.NewPrintableError(err, n)
return nil
}
}
return v
Expand All @@ -71,62 +30,3 @@ func (v *TemplateComments) Visit(n ast.Node) ast.Visitor {
func (v TemplateComments) Error() error {
return v.err
}

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
}

0 comments on commit 70b4d97

Please sign in to comment.