Skip to content

Commit

Permalink
feat: capability to override template funcs (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRooney authored Mar 27, 2023
1 parent b2006ab commit deb8777
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
*.iml
22 changes: 17 additions & 5 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ func New(opts ...Opt) *Engine {
t.ReadFunc = e.readFile

e.jsFuncs = map[string]func(call CallContext) goja.Value{
"require": e.require,
"templateFile": e.templateFileJS,
"templateString": e.templateStringJS,
"templateStringInput": e.templateStringInputJS,
"registerTemplateFunc": e.registerTemplateFunc,
"require": e.require,
"templateFile": e.templateFileJS,
"templateString": e.templateStringJS,
"templateStringInput": e.templateStringInputJS,
"registerTemplateFunc": e.registerTemplateFunc,
"unregisterTemplateFunc": e.unregisterTemplateFunc,
}

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

func (e *Engine) unregisterTemplateFunc(call CallContext) goja.Value {
name := call.Argument(0).String()
if _, ok := e.templator.TmplFuncs[name]; !ok {
panic(call.VM.NewGoError(fmt.Errorf("%w: template function %s does not exist", ErrReserved, name)))
}

delete(e.templator.TmplFuncs, name)

return goja.Undefined()
}

func (e *Engine) require(call CallContext) goja.Value {
vm := call.VM

Expand Down
10 changes: 6 additions & 4 deletions engine_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func TestEngine_RunScript_Success(t *testing.T) {
easytemplate.WithSearchLocations([]string{"./testdata"}),
easytemplate.WithWriteFunc(func(outFile string, data []byte) error {
expectedData, ok := expectedFiles[outFile]
assert.True(t, ok, "unexpected file written: %s", outFile)
assert.Equal(t, expectedData, string(data))

delete(expectedFiles, outFile)
if ok {
assert.Equal(t, expectedData, string(data))
delete(expectedFiles, outFile)
} else {
require.NoError(t, os.WriteFile("./testdata/expected/"+outFile, data, 0o644))
}

return nil
}),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require (
github.com/dop251/goja v0.0.0-20230216112155-746f7ebdc514
github.com/dop251/goja_nodejs v0.0.0-20221211191749-434192f0843e
github.com/evanw/esbuild v0.17.8
github.com/go-sourcemap/sourcemap v2.1.3+incompatible
github.com/golang/mock v1.6.0
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
github.com/dop251/goja v0.0.0-20221118162653-d4bf6fde1b86/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
github.com/dop251/goja v0.0.0-20230128084908-78b980256d04 h1:iQQgQ1wBsFmpu6OjINCY2ekdknKNNpxO/GOzzww2Amk=
github.com/dop251/goja v0.0.0-20230128084908-78b980256d04/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
github.com/dop251/goja v0.0.0-20230216112155-746f7ebdc514 h1:4Gkw6jJWCNKRwhOvqZZXwmFezg0r6IHCi0g4+WzL/84=
github.com/dop251/goja v0.0.0-20230216112155-746f7ebdc514/go.mod h1:yRkwfj0CBpOGre+TwBsqPV0IH0Pk73e4PXJOeNDboGs=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
Expand Down
1 change: 1 addition & 0 deletions testdata/expected/testMethod1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
1 change: 1 addition & 0 deletions testdata/expected/testMethod2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
1 change: 1 addition & 0 deletions testdata/expected/testMethod3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
overridden test method
22 changes: 22 additions & 0 deletions testdata/scripts/registerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function testMethod() {
return "hello world"
}

registerTemplateFunc("testMethod", testMethod)

templateFile("templates/testMethod.stmpl", "testMethod1.txt", {});

try {
registerTemplateFunc("testMethod", testMethod)
} catch (e) {
templateFile("templates/testMethod.stmpl", "testMethod2.txt", {});
}

unregisterTemplateFunc("testMethod")

function newTestMethod() {
return "overridden test method"
}
registerTemplateFunc("testMethod", newTestMethod)

templateFile("templates/testMethod.stmpl", "testMethod3.txt", {});
2 changes: 2 additions & 0 deletions testdata/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ templateFile("templates/test.stmpl", "test.txt", {
Value: multiply(add(reduced, 2), 2),
});
templateFile("templates/test5.stmpl", "test5.txt", {});

require("./registerTest.js")
1 change: 1 addition & 0 deletions testdata/templates/testMethod.stmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{testMethod}}

0 comments on commit deb8777

Please sign in to comment.