-
Notifications
You must be signed in to change notification settings - Fork 2
/
templating.go
85 lines (67 loc) · 2.17 KB
/
templating.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package easytemplate
import (
"github.com/dop251/goja"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
func (e *Engine) templateFileJS(call CallContext) goja.Value {
templateFile := call.Argument(0).String()
outFile := call.Argument(1).String()
inputData := call.Argument(2).Export() //nolint:gomnd
ctx := call.Ctx
_, span := e.tracer.Start(ctx, "js:templateFile", trace.WithAttributes(
attribute.String("templateFile", templateFile),
attribute.String("outFile", outFile),
))
defer span.End()
if err := e.templator.TemplateFile(call.Ctx, call.VM, templateFile, outFile, inputData); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
span.End()
panic(call.VM.NewGoError(err))
}
return goja.Undefined()
}
func (e *Engine) templateStringJS(call CallContext) goja.Value {
templateFile := call.Argument(0).String()
inputData := call.Argument(1).Export()
ctx := call.Ctx
_, span := e.tracer.Start(ctx, "js:templateString", trace.WithAttributes(
attribute.String("templateFile", templateFile),
))
defer span.End()
output, err := e.templator.TemplateString(call.Ctx, call.VM, templateFile, inputData)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
span.End()
panic(call.VM.NewGoError(err))
}
return call.VM.ToValue(output)
}
func (e *Engine) templateStringInputJS(call CallContext) goja.Value {
name := call.Argument(0).String()
input := call.Argument(1).String()
inputData := call.Argument(2).Export() //nolint:gomnd
ctx := call.Ctx
_, span := e.tracer.Start(ctx, "js:templateStringInput", trace.WithAttributes(
attribute.String("name", name),
))
defer span.End()
output, err := e.templator.TemplateStringInput(call.Ctx, call.VM, name, input, inputData)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
span.End()
panic(call.VM.NewGoError(err))
}
return call.VM.ToValue(output)
}
func (e *Engine) recurseJS(call CallContext) goja.Value {
output, err := e.templator.Recurse(call.VM, int(call.Argument(0).ToInteger()))
if err != nil {
panic(call.VM.NewGoError(err))
}
return call.VM.ToValue(output)
}