-
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.
♻️ Move recurse function into node package
- Loading branch information
Showing
5 changed files
with
95 additions
and
66 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
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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
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