-
Notifications
You must be signed in to change notification settings - Fork 128
/
template.go
59 lines (49 loc) 路 1.39 KB
/
template.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
package golang
import (
"bytes"
_ "embed"
"text/template"
)
// MainFuncRxTmpl the rxstream serverless of the main function template
//
//go:embed templates/main_rx.tmpl
var MainFuncRxTmpl []byte
// MainFuncTmpl the raw bytes serverless of the main function template
//
//go:embed templates/main.tmpl
var MainFuncTmpl []byte
//go:embed templates/init.tmpl
var InitTmpl []byte
//go:embed templates/init_test.tmpl
var InitTestTmpl []byte
//go:embed templates/init_rx.tmpl
var InitRxTmpl []byte
//go:embed templates/wasi_main.tmpl
var WasiMainFuncTmpl []byte
// Context defines context for the template
type Context struct {
// Name of the servcie
Name string
// ZipperAddr is the address of the zipper server
ZipperAddr string
// Client credential
Credential string
// WithInitFunc determines whether to work with init function
WithInitFunc bool
// WithWantedTarget determines whether to work with SetWantedTarget
WithWantedTarget bool
// WithDescription determines whether to work with description
WithDescription bool
// WithInputSchema determines whether to work with input schema
WithInputSchema bool
}
// RenderTmpl renders the template with the given context
func RenderTmpl(tpl string, ctx *Context) ([]byte, error) {
t := template.Must(template.New("tpl").Parse(tpl))
buf := bytes.NewBuffer([]byte{})
err := t.Execute(buf, ctx)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}