Skip to content

Commit

Permalink
🚚 Rename package: node to comment
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 2, 2022
1 parent 1ed90ce commit 88079e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 44 deletions.
40 changes: 5 additions & 35 deletions internal/node/has_comment.go → internal/comment/comment.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
package node
package comment

import (
"gopkg.in/yaml.v3"
"strings"
)

type TmplTag string

func (t TmplTag) ToYaml() string {
if t == DynamicTag {
return ""
}
return "!!" + string(t)
}

const tagSep = ":"

var (
DynamicTag TmplTag = ""
BoolTag TmplTag = "bool"
StrTag TmplTag = "str"
IntTag TmplTag = "int"
FloatTag TmplTag = "float"
SeqTag TmplTag = "seq"
MapTag TmplTag = "map"
)

var tags = []TmplTag{
BoolTag,
StrTag,
IntTag,
FloatTag,
SeqTag,
MapTag,
}

// GetCommentTmpl returns the template and tag from a yaml.Node LineComment
func GetCommentTmpl(prefix string, n *yaml.Node) (string, TmplTag) {
// Parse returns the template and tag from a yaml.Node LineComment
func Parse(prefix string, n *yaml.Node) (string, Tag) {
comment := n.LineComment
if strings.HasPrefix(comment, prefix) {
// Comment has #yampl prefix
Expand All @@ -62,7 +32,7 @@ func GetCommentTmpl(prefix string, n *yaml.Node) (string, TmplTag) {
return "", DynamicTag
}

// MoveComment moves a comment between yaml.Node entries after a style change.
// Move moves a comment between yaml.Node entries after a style change.
// When a yaml.MappingNode or yaml.SequenceNode has an inline comment,
// the decoder will set LineComment differently according to the node's style.
//
Expand All @@ -71,7 +41,7 @@ func GetCommentTmpl(prefix string, n *yaml.Node) (string, TmplTag) {
//
// If templating changes the node's style, the comment needs to move or else
// encoding errors will occur.
func MoveComment(key, val *yaml.Node) {
func Move(key, val *yaml.Node) {
if val.Kind != yaml.SequenceNode && val.Kind != yaml.MappingNode {
return
}
Expand Down
31 changes: 31 additions & 0 deletions internal/comment/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package comment

type Tag string

func (t Tag) ToYaml() string {
if t == DynamicTag {
return ""
}
return "!!" + string(t)
}

const tagSep = ":"

var (
DynamicTag Tag = ""
BoolTag Tag = "bool"
StrTag Tag = "str"
IntTag Tag = "int"
FloatTag Tag = "float"
SeqTag Tag = "seq"
MapTag Tag = "map"
)

var tags = []Tag{
BoolTag,
StrTag,
IntTag,
FloatTag,
SeqTag,
MapTag,
}
4 changes: 2 additions & 2 deletions internal/visitor/find_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package visitor

import (
"fmt"
"github.com/clevyr/yampl/internal/comment"
"github.com/clevyr/yampl/internal/config"
"github.com/clevyr/yampl/internal/node"
template2 "github.com/clevyr/yampl/internal/template"
"gopkg.in/yaml.v3"
"regexp"
Expand Down Expand Up @@ -75,7 +75,7 @@ func (visitor *FindArgs) Run(n *yaml.Node) error {
}

func (visitor *FindArgs) FindArgs(n *yaml.Node) error {
if tmplSrc, _ := node.GetCommentTmpl(visitor.conf.Prefix, n); tmplSrc != "" {
if tmplSrc, _ := comment.Parse(visitor.conf.Prefix, n); tmplSrc != "" {
tmpl, err := template.New("").
Funcs(template2.FuncMap()).
Delims(visitor.conf.LeftDelim, visitor.conf.RightDelim).
Expand Down
14 changes: 7 additions & 7 deletions internal/visitor/template_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package visitor
import (
"bytes"
"fmt"
"github.com/clevyr/yampl/internal/comment"
"github.com/clevyr/yampl/internal/config"
"github.com/clevyr/yampl/internal/node"
template2 "github.com/clevyr/yampl/internal/template"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
Expand All @@ -24,7 +24,7 @@ type TemplateComments struct {
func (t TemplateComments) Run(n *yaml.Node) error {
if len(n.Content) == 0 {
// Node has no children. Template current node.
tmplSrc, tmplTag := node.GetCommentTmpl(t.conf.Prefix, n)
tmplSrc, tmplTag := comment.Parse(t.conf.Prefix, n)
if tmplSrc != "" {
if err := t.Template(n, tmplSrc, tmplTag); err != nil {
if t.conf.Fail {
Expand All @@ -43,7 +43,7 @@ func (t TemplateComments) Run(n *yaml.Node) error {
// Attempt to fetch template from comments on the key.
key, val := n.Content[i], n.Content[i+1]

tmplSrc, tmplTag := node.GetCommentTmpl(t.conf.Prefix, key)
tmplSrc, tmplTag := comment.Parse(t.conf.Prefix, key)
if tmplSrc != "" {
if err := t.Template(val, tmplSrc, tmplTag); err != nil {
if t.conf.Fail {
Expand All @@ -53,7 +53,7 @@ func (t TemplateComments) Run(n *yaml.Node) error {
}
} else {
// Current node was templated, do not need to traverse children
node.MoveComment(key, val)
comment.Move(key, val)
continue
}
}
Expand All @@ -63,7 +63,7 @@ func (t TemplateComments) Run(n *yaml.Node) error {
return err
}

node.MoveComment(key, val)
comment.Move(key, val)
}
default:
for _, n := range n.Content {
Expand All @@ -75,7 +75,7 @@ func (t TemplateComments) Run(n *yaml.Node) error {
return nil
}

func (t TemplateComments) Template(n *yaml.Node, tmplSrc string, tmplTag node.TmplTag) error {
func (t TemplateComments) Template(n *yaml.Node, tmplSrc string, tmplTag comment.Tag) error {
t.conf.Log = t.conf.Log.WithFields(log.Fields{
"tmpl": tmplSrc,
"filePos": fmt.Sprintf("%d:%d", n.Line, n.Column),
Expand Down Expand Up @@ -105,7 +105,7 @@ func (t TemplateComments) Template(n *yaml.Node, tmplSrc string, tmplTag node.Tm
n.Style = 0

switch tmplTag {
case node.SeqTag, node.MapTag:
case comment.SeqTag, comment.MapTag:
var tmpNode yaml.Node

if err := yaml.Unmarshal(buf.Bytes(), &tmpNode); err != nil {
Expand Down

0 comments on commit 88079e0

Please sign in to comment.