-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
60 lines (56 loc) · 1.61 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"io"
"strings"
"text/template"
"github.com/valyala/fasttemplate"
)
// FastTemplate is driver by fasttemplate
func FastTemplate(content string, data map[string]interface{}, config *Config) io.Reader {
t := fasttemplate.New(content, config.FileStartTag, config.FileEndTag)
return strings.NewReader(t.ExecuteString(data))
}
// TextTemplate is driver by text/template
func TextTemplate(content string, data map[string]interface{}, config *Config) io.Reader {
funcMap := template.FuncMap{
// String operator pipeline
"ToUpper": strings.ToUpper,
"SplitBy": SplitBy,
"FirstUpper": FirstUpper,
"ToLower": strings.ToLower,
"FirstLower": FirstLower,
"SliceStr": SliceStr,
"ParseTmpStr": ParseTmpStr,
"ReplaceAllStr": ReplaceAllStr,
"ParseRouteStr": ParseRouteStr,
// Array operator pipeline
"FirstUpperForArray": FirstUpperForArray,
"FirstLowerForArray": FirstLowerForArray,
"JoinBy": JoinBy,
"Slice": Slice,
"LastStr": LastStr,
"Contains": Contains,
"Distinct": Distinct,
// Collection operator pipeline
"In": In,
"Get": Get,
"GetStr": GetStr,
// Instrumental operator pipeline
"GetRouteParams": GetRouteParams,
"QueryParse": QueryParse,
"SchemaToTsType": SchemaToTsType,
}
tmpl, err := template.New(AppName).Funcs(funcMap).Delims(config.FileStartTag, config.FileEndTag).Parse(content)
if err != nil {
panic(err)
}
reader, writer := io.Pipe()
go func() {
defer writer.Close()
err = tmpl.Execute(writer, data)
if err != nil {
panic(err)
}
}()
return reader
}