Skip to content

Commit deb8777

Browse files
authored
feat: capability to override template funcs (#5)
1 parent b2006ab commit deb8777

11 files changed

+53
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
*.iml

engine.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ func New(opts ...Opt) *Engine {
126126
t.ReadFunc = e.readFile
127127

128128
e.jsFuncs = map[string]func(call CallContext) goja.Value{
129-
"require": e.require,
130-
"templateFile": e.templateFileJS,
131-
"templateString": e.templateStringJS,
132-
"templateStringInput": e.templateStringInputJS,
133-
"registerTemplateFunc": e.registerTemplateFunc,
129+
"require": e.require,
130+
"templateFile": e.templateFileJS,
131+
"templateString": e.templateStringJS,
132+
"templateStringInput": e.templateStringInputJS,
133+
"registerTemplateFunc": e.registerTemplateFunc,
134+
"unregisterTemplateFunc": e.unregisterTemplateFunc,
134135
}
135136

136137
for _, opt := range opts {
@@ -277,6 +278,17 @@ func (e *Engine) init(data any) (*vm.VM, error) {
277278
return v, nil
278279
}
279280

281+
func (e *Engine) unregisterTemplateFunc(call CallContext) goja.Value {
282+
name := call.Argument(0).String()
283+
if _, ok := e.templator.TmplFuncs[name]; !ok {
284+
panic(call.VM.NewGoError(fmt.Errorf("%w: template function %s does not exist", ErrReserved, name)))
285+
}
286+
287+
delete(e.templator.TmplFuncs, name)
288+
289+
return goja.Undefined()
290+
}
291+
280292
func (e *Engine) require(call CallContext) goja.Value {
281293
vm := call.VM
282294

engine_integration_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ func TestEngine_RunScript_Success(t *testing.T) {
2828
easytemplate.WithSearchLocations([]string{"./testdata"}),
2929
easytemplate.WithWriteFunc(func(outFile string, data []byte) error {
3030
expectedData, ok := expectedFiles[outFile]
31-
assert.True(t, ok, "unexpected file written: %s", outFile)
32-
assert.Equal(t, expectedData, string(data))
33-
34-
delete(expectedFiles, outFile)
31+
if ok {
32+
assert.Equal(t, expectedData, string(data))
33+
delete(expectedFiles, outFile)
34+
} else {
35+
require.NoError(t, os.WriteFile("./testdata/expected/"+outFile, data, 0o644))
36+
}
3537

3638
return nil
3739
}),

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ require (
66
github.com/dop251/goja v0.0.0-20230216112155-746f7ebdc514
77
github.com/dop251/goja_nodejs v0.0.0-20221211191749-434192f0843e
88
github.com/evanw/esbuild v0.17.8
9+
github.com/go-sourcemap/sourcemap v2.1.3+incompatible
910
github.com/golang/mock v1.6.0
1011
github.com/stretchr/testify v1.8.1
1112
)
1213

1314
require (
1415
github.com/davecgh/go-spew v1.1.1 // indirect
1516
github.com/dlclark/regexp2 v1.7.0 // indirect
16-
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
1717
github.com/pmezard/go-difflib v1.0.0 // indirect
1818
golang.org/x/sys v0.3.0 // indirect
1919
golang.org/x/text v0.5.0 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo
77
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
88
github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
99
github.com/dop251/goja v0.0.0-20221118162653-d4bf6fde1b86/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
10-
github.com/dop251/goja v0.0.0-20230128084908-78b980256d04 h1:iQQgQ1wBsFmpu6OjINCY2ekdknKNNpxO/GOzzww2Amk=
11-
github.com/dop251/goja v0.0.0-20230128084908-78b980256d04/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
1210
github.com/dop251/goja v0.0.0-20230216112155-746f7ebdc514 h1:4Gkw6jJWCNKRwhOvqZZXwmFezg0r6IHCi0g4+WzL/84=
1311
github.com/dop251/goja v0.0.0-20230216112155-746f7ebdc514/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
1412
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=

testdata/expected/testMethod1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

testdata/expected/testMethod2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

testdata/expected/testMethod3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
overridden test method

testdata/scripts/registerTest.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function testMethod() {
2+
return "hello world"
3+
}
4+
5+
registerTemplateFunc("testMethod", testMethod)
6+
7+
templateFile("templates/testMethod.stmpl", "testMethod1.txt", {});
8+
9+
try {
10+
registerTemplateFunc("testMethod", testMethod)
11+
} catch (e) {
12+
templateFile("templates/testMethod.stmpl", "testMethod2.txt", {});
13+
}
14+
15+
unregisterTemplateFunc("testMethod")
16+
17+
function newTestMethod() {
18+
return "overridden test method"
19+
}
20+
registerTemplateFunc("testMethod", newTestMethod)
21+
22+
templateFile("templates/testMethod.stmpl", "testMethod3.txt", {});

testdata/scripts/test.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ templateFile("templates/test.stmpl", "test.txt", {
1515
Value: multiply(add(reduced, 2), 2),
1616
});
1717
templateFile("templates/test5.stmpl", "test5.txt", {});
18+
19+
require("./registerTest.js")

testdata/templates/testMethod.stmpl

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{testMethod}}

0 commit comments

Comments
 (0)