Skip to content

Commit

Permalink
♻️ Move recurse function into node package
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 6, 2022
1 parent d6ff7a1 commit d992295
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 66 deletions.
9 changes: 5 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/clevyr/go-yampl/internal/config"
"github.com/clevyr/go-yampl/internal/node"
"github.com/clevyr/go-yampl/internal/template"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -135,9 +136,9 @@ func templateReader(r io.Reader) ([]byte, error) {
var buf bytes.Buffer

for {
var node yaml.Node
var n yaml.Node

if err := decoder.Decode(&node); err != nil {
if err := decoder.Decode(&n); err != nil {
if errors.Is(err, io.EOF) {
break
}
Expand All @@ -148,11 +149,11 @@ func templateReader(r io.Reader) ([]byte, error) {
buf.Write([]byte("---\n"))
}

if err := template.VisitNodes(conf, template.LineComment, &node); err != nil {
if err := node.Visit(conf, template.LineComment, &n); err != nil {
return buf.Bytes(), err
}

b, err := yaml.Marshal(&node)
b, err := yaml.Marshal(&n)
if err != nil {
return buf.Bytes(), err
}
Expand Down
23 changes: 23 additions & 0 deletions internal/node/visit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package node

import (
"github.com/clevyr/go-yampl/internal/config"
"gopkg.in/yaml.v3"
)

type Visitor func(conf config.Config, node *yaml.Node) error

func Visit(conf config.Config, visit Visitor, node *yaml.Node) error {
if len(node.Content) == 0 {
if err := visit(conf, node); err != nil {
return err
}
} else {
for _, node := range node.Content {
if err := Visit(conf, visit, node); err != nil {
return err
}
}
}
return nil
}
59 changes: 59 additions & 0 deletions internal/node/visit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package node

import (
"errors"
"github.com/clevyr/go-yampl/internal/config"
"gopkg.in/yaml.v3"
"testing"
)

func TestVisitNodes(t *testing.T) {
defaultConf := config.Config{
LeftDelim: "{{",
RightDelim: "}}",
Prefix: "#yampl",
Values: map[string]string{
"b": "b",
},
}

type args struct {
conf config.Config
input string
}
tests := []struct {
name string
args args
wantErr bool
}{
{"no error", args{defaultConf, "a: a"}, false},
{"error", args{defaultConf, "a: a"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var visitorCalled bool

var node yaml.Node
_ = yaml.Unmarshal([]byte(tt.args.input), &node)

visitor := func(conf config.Config, node *yaml.Node) error {
visitorCalled = true
if tt.wantErr {
return errors.New("test error")
}
return nil
}

if err := Visit(tt.args.conf, visitor, &node); err != nil {
if (err != nil) != tt.wantErr {
t.Errorf("Visit() error = %v, wantErr %v", err, tt.wantErr)
}
return
}

if !visitorCalled {
t.Errorf("Visit() visitorCalled = %v, want %v", visitorCalled, true)
}
})
}
}
17 changes: 0 additions & 17 deletions internal/template/line_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ func init() {
funcMap["tag"] = DockerTag
}

type Visitor func(conf config.Config, node *yaml.Node) error

func VisitNodes(conf config.Config, visit Visitor, node *yaml.Node) error {
if len(node.Content) == 0 {
if err := visit(conf, node); err != nil {
return err
}
} else {
for _, node := range node.Content {
if err := VisitNodes(conf, visit, node); err != nil {
return err
}
}
}
return nil
}

func LineComment(conf config.Config, node *yaml.Node) error {
if node.LineComment != "" && strings.HasPrefix(node.LineComment, conf.Prefix) {
tmpl, err := template.New("").
Expand Down
53 changes: 8 additions & 45 deletions internal/template/line_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,16 @@ import (
"testing"
)

var defaultConf = config.Config{
LeftDelim: "{{",
RightDelim: "}}",
Prefix: "#yampl",
Values: map[string]string{
"b": "b",
},
}

func TestVisitNodes(t *testing.T) {
type args struct {
conf config.Config
input string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"no comment", args{defaultConf, "a: a"}, "a: a", false},
{"simple comment", args{defaultConf, "a: a #yampl b"}, "a: b #yampl b", false},
{"dynamic comment", args{defaultConf, "a: a #yampl {{ .b }}"}, "a: b #yampl {{ .b }}", false},
{"invalid template", args{defaultConf, "a: a #yampl {{"}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var node yaml.Node
_ = yaml.Unmarshal([]byte(tt.args.input), &node)

if err := VisitNodes(tt.args.conf, LineComment, &node); err != nil {
if (err != nil) != tt.wantErr {
t.Errorf("VisitNodes() error = %v, wantErr %v", err, tt.wantErr)
}
return
}

got, _ := yaml.Marshal(&node)
got = bytes.TrimRight(got, "\n")
if string(got) != tt.want {
t.Errorf("VisitNodes() = %v, want %v", string(got), tt.want)
}
})
func TestTemplateLineComment(t *testing.T) {
defaultConf := config.Config{
LeftDelim: "{{",
RightDelim: "}}",
Prefix: "#yampl",
Values: map[string]string{
"b": "b",
},
}
}

func TestTemplateLineComment(t *testing.T) {
type args struct {
conf config.Config
comment string
Expand Down

0 comments on commit d992295

Please sign in to comment.