-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
template.go
69 lines (51 loc) · 1.14 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
60
61
62
63
64
65
66
67
68
package hof
import (
"cuelang.org/go/cue"
hofcontext "github.com/hofstadter-io/hof/flow/context"
"github.com/hofstadter-io/hof/lib/templates"
)
type HofTemplate struct{
Name string
Data any
Template string
Partials map[string]string
Delims templates.Delims
}
func NewHofTemplate(val cue.Value) (hofcontext.Runner, error) {
return &HofTemplate{}, nil
}
func (T *HofTemplate) Run(ctx *hofcontext.Context) (interface{}, error) {
v := ctx.Value
ferr := func() error {
ctx.CUELock.Lock()
defer func() {
ctx.CUELock.Unlock()
}()
err := v.Decode(T)
return err
}()
if ferr != nil {
return nil, ferr
}
t, err := templates.CreateFromString(T.Name, T.Template, T.Delims)
if err != nil {
return nil, err
}
for k, P := range T.Partials {
p := t.T.New(k)
// do we need to do this, does the partial use the helpers already registered?
// T.AddGolangHelpers()
_, err := p.Parse(P)
if err != nil {
return nil, err
}
}
bs, err := t.Render(T.Data)
if err != nil {
return nil, err
}
ctx.CUELock.Lock()
defer ctx.CUELock.Unlock()
res := v.FillPath(cue.ParsePath("out"), string(bs))
return res, nil
}