Skip to content

Commit 7bacacf

Browse files
committed
- [+] add Execute0()
1 parent 570d6a1 commit 7bacacf

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

easygen.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,21 @@ func Process1(t Template, wr io.Writer, fileNameTempl string, fileName string) e
152152
return Execute(t, wr, fileNameT, m)
153153
}
154154

155-
// Execute will execute the Template on the given data map `m`.
155+
// Execute0 will execute the Template given as strTempl with the given data map `m` (i.e., no template file and no data file).
156+
// It parses text template strTempl then applies it to to the specified data
157+
// object m, and writes the output to wr. If an error occurs executing the
158+
// template or writing its output, execution stops, but partial results may
159+
// already have been written to the output writer. A template may be
160+
// executed safely in parallel, although if parallel executions share a
161+
// Writer the output may be interleaved.
162+
func Execute0(t Template, wr io.Writer, strTempl string, m EgData) error {
163+
verbose("Execute with template string: "+strTempl, 1)
164+
tmpl, err := t.Parse(strTempl)
165+
checkError(err)
166+
return tmpl.Execute(wr, m)
167+
}
168+
169+
// Execute will execute the Template from fileNameT on the given data map `m`.
156170
func Execute(t Template, wr io.Writer, fileNameT string, m EgData) error {
157171
// 1. Check locally
158172
verbose("Checking for template locally: "+fileNameT, 1)

example_execute0_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package easygen_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/go-easygen/easygen"
9+
)
10+
11+
// for standalone test, change package to `main` and the next func def to,
12+
// func main() {
13+
func ExampleExecute0_output() {
14+
easygen.Opts.Debug = 1
15+
16+
tmpl := easygen.NewTemplate().Funcs(easygen.FuncDefs())
17+
18+
// define driving data of any tye
19+
v0 := "some-init-method"
20+
// https://godoc.org/github.com/go-easygen/easygen#Execute0
21+
// provide template string, not file name
22+
easygen.Execute0(tmpl, os.Stdout, "{{stringsToUpper .}}\n", v0)
23+
24+
// Demo of using driving data of pure slice/array
25+
v1 := []string{"red", "blue", "white"}
26+
easygen.Execute0(tmpl, os.Stdout, "The colors are: {{range .}}{{.}}, {{end}}.\n", v1)
27+
28+
// Demo output to string
29+
var b strings.Builder
30+
easygen.Execute0(tmpl, &b, "The colors are: {{range $i, $color := .}}{{$color}}{{if lt $i ($ | len | minus1)}}, {{end}}{{end}}.\n", v1)
31+
fmt.Print(b.String())
32+
33+
// Output:
34+
// SOME-INIT-METHOD
35+
// The colors are: red, blue, white, .
36+
// The colors are: red, blue, white.
37+
}
38+
39+
// To show the full code in GoDoc
40+
type dummyExecute0 struct {
41+
}

example_execute_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/go-easygen/easygen"
99
"github.com/go-easygen/easygen/egCal"
1010
"github.com/go-easygen/easygen/egVar"
11+
"gopkg.in/easygen.v2/egFilePath"
1112
)
1213

1314
type variable struct {
@@ -16,11 +17,12 @@ type variable struct {
1617

1718
// for standalone test, change package to `main` and the next func def to,
1819
// func main() {
19-
func ExampleExecute() {
20+
func ExampleExecute_output() {
2021
easygen.Opts.Debug = 1
2122

2223
tmpl0 := easygen.NewTemplate().Customize()
23-
tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs())
24+
tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egFilePath.FuncDefs()).
25+
Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs())
2426

2527
// define driving data of any tye
2628
v0 := variable{"some-init-method"}
@@ -44,7 +46,3 @@ func ExampleExecute() {
4446
// The colors are: red, blue, white, .
4547
// The colors are: red, blue, white.
4648
}
47-
48-
// To show the full code in GoDoc
49-
type dummyExecute struct {
50-
}

0 commit comments

Comments
 (0)