From d99229573e1593ae03944cc638a529d14b550895 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Fri, 6 May 2022 14:59:38 -0500 Subject: [PATCH] :recycle: Move recurse function into node package --- cmd/cmd.go | 9 ++-- internal/node/visit.go | 23 ++++++++++ internal/node/visit_test.go | 59 ++++++++++++++++++++++++++ internal/template/line_comment.go | 17 -------- internal/template/line_comment_test.go | 53 ++++------------------- 5 files changed, 95 insertions(+), 66 deletions(-) create mode 100644 internal/node/visit.go create mode 100644 internal/node/visit_test.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 9e1080f..38bd6f4 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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" @@ -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 } @@ -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 } diff --git a/internal/node/visit.go b/internal/node/visit.go new file mode 100644 index 0000000..c61844e --- /dev/null +++ b/internal/node/visit.go @@ -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 +} diff --git a/internal/node/visit_test.go b/internal/node/visit_test.go new file mode 100644 index 0000000..2cd98b8 --- /dev/null +++ b/internal/node/visit_test.go @@ -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) + } + }) + } +} diff --git a/internal/template/line_comment.go b/internal/template/line_comment.go index 563eb07..d76f3cf 100644 --- a/internal/template/line_comment.go +++ b/internal/template/line_comment.go @@ -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(""). diff --git a/internal/template/line_comment_test.go b/internal/template/line_comment_test.go index 68a6f53..6427bbf 100644 --- a/internal/template/line_comment_test.go +++ b/internal/template/line_comment_test.go @@ -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