From 18286461b290b328e3305b9f7cd28220eacbde0a Mon Sep 17 00:00:00 2001 From: Ryan Lathouwers Date: Sun, 3 Mar 2024 10:52:34 -0800 Subject: [PATCH] Add go1.16 embed.FS loader Addresses: https://github.com/CloudyKit/jet/issues/191 --- go.mod | 2 +- loaders/embedfs/embedfs_test.go | 22 ++++++++++++ loaders/embedfs/loader.go | 34 +++++++++++++++++++ .../testData/includeIfNotExists/existent.jet | 1 + .../testData/includeIfNotExists/exists.jet | 1 + .../includeIfNotExists/ifIncludeIfExits.jet | 8 +++++ .../includeIfNotExists/notExistent.jet | 1 + .../testData/includeIfNotExists/wcontext.jet | 2 ++ .../includeIfNotExists/wcontext_child.jet | 1 + 9 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 loaders/embedfs/embedfs_test.go create mode 100644 loaders/embedfs/loader.go create mode 100644 loaders/embedfs/testData/includeIfNotExists/existent.jet create mode 100644 loaders/embedfs/testData/includeIfNotExists/exists.jet create mode 100644 loaders/embedfs/testData/includeIfNotExists/ifIncludeIfExits.jet create mode 100644 loaders/embedfs/testData/includeIfNotExists/notExistent.jet create mode 100644 loaders/embedfs/testData/includeIfNotExists/wcontext.jet create mode 100644 loaders/embedfs/testData/includeIfNotExists/wcontext_child.jet diff --git a/go.mod b/go.mod index 2703675..108eca7 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/CloudyKit/jet/v6 -go 1.12 +go 1.16 require github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 diff --git a/loaders/embedfs/embedfs_test.go b/loaders/embedfs/embedfs_test.go new file mode 100644 index 0000000..160af7f --- /dev/null +++ b/loaders/embedfs/embedfs_test.go @@ -0,0 +1,22 @@ +package embedfs + +import ( + "embed" + "testing" + + "github.com/CloudyKit/jet/v6" + "github.com/CloudyKit/jet/v6/jettest" +) + +//go:embed testData/includeIfNotExists/* +var templateFS embed.FS + +func TestEmbedFileSystemResolve(t *testing.T) { + l := NewLoader("testData/includeIfNotExists", templateFS) + + set := jet.NewSet(l) + jettest.RunWithSet(t, set, nil, nil, "existent", "Hi, i exist!!") + jettest.RunWithSet(t, set, nil, nil, "notExistent", "") + jettest.RunWithSet(t, set, nil, nil, "ifIncludeIfExits", "Hi, i exist!!\n Was included!!\n\n\n Was not included!!\n\n") + jettest.RunWithSet(t, set, nil, "World", "wcontext", "Hi, Buddy!\nHi, World!") +} diff --git a/loaders/embedfs/loader.go b/loaders/embedfs/loader.go new file mode 100644 index 0000000..7b85a5b --- /dev/null +++ b/loaders/embedfs/loader.go @@ -0,0 +1,34 @@ +package embedfs + +import ( + "embed" + "io" + "os" + "path/filepath" + + "github.com/CloudyKit/jet/v6" +) + +type embedFileSystemLoader struct { + dir string + fs embed.FS +} + +// NewLoader returns an initialized loader serving the passed embed.FS. +func NewLoader(dirPath string, fs embed.FS) jet.Loader { + return &embedFileSystemLoader{ + dir: filepath.FromSlash(dirPath), + fs: fs, + } +} + +// Open implements Loader.Open() on top of an embed.FS. +func (l *embedFileSystemLoader) Open(name string) (io.ReadCloser, error) { + return l.fs.Open(filepath.Join(l.dir, filepath.FromSlash(name))) +} + +// Exists implements Loader.Exists() on top of an embed.FS by trying to open the file. +func (l *embedFileSystemLoader) Exists(name string) bool { + _, err := l.fs.Open(filepath.Join(l.dir, filepath.FromSlash(name))) + return err == nil && !os.IsNotExist(err) +} diff --git a/loaders/embedfs/testData/includeIfNotExists/existent.jet b/loaders/embedfs/testData/includeIfNotExists/existent.jet new file mode 100644 index 0000000..754eb15 --- /dev/null +++ b/loaders/embedfs/testData/includeIfNotExists/existent.jet @@ -0,0 +1 @@ +{{ includeIfExists: "exists.jet"}} \ No newline at end of file diff --git a/loaders/embedfs/testData/includeIfNotExists/exists.jet b/loaders/embedfs/testData/includeIfNotExists/exists.jet new file mode 100644 index 0000000..7c07d61 --- /dev/null +++ b/loaders/embedfs/testData/includeIfNotExists/exists.jet @@ -0,0 +1 @@ +Hi, i exist!! \ No newline at end of file diff --git a/loaders/embedfs/testData/includeIfNotExists/ifIncludeIfExits.jet b/loaders/embedfs/testData/includeIfNotExists/ifIncludeIfExits.jet new file mode 100644 index 0000000..fc31b2f --- /dev/null +++ b/loaders/embedfs/testData/includeIfNotExists/ifIncludeIfExits.jet @@ -0,0 +1,8 @@ +{{ if includeIfExists("exists.jet") }} + Was included!! +{{ end }} +{{ if includeIfExists("notExists.jet") }} + Was included!! +{{ else }} + Was not included!! +{{ end }} diff --git a/loaders/embedfs/testData/includeIfNotExists/notExistent.jet b/loaders/embedfs/testData/includeIfNotExists/notExistent.jet new file mode 100644 index 0000000..a22998f --- /dev/null +++ b/loaders/embedfs/testData/includeIfNotExists/notExistent.jet @@ -0,0 +1 @@ +{{ includeIfExists: "notExists.jet"}} \ No newline at end of file diff --git a/loaders/embedfs/testData/includeIfNotExists/wcontext.jet b/loaders/embedfs/testData/includeIfNotExists/wcontext.jet new file mode 100644 index 0000000..95b8c20 --- /dev/null +++ b/loaders/embedfs/testData/includeIfNotExists/wcontext.jet @@ -0,0 +1,2 @@ +{{ includeIfExists("wcontext_child","Buddy") }} +{{ includeIfExists("wcontext_child") }} \ No newline at end of file diff --git a/loaders/embedfs/testData/includeIfNotExists/wcontext_child.jet b/loaders/embedfs/testData/includeIfNotExists/wcontext_child.jet new file mode 100644 index 0000000..076d6c3 --- /dev/null +++ b/loaders/embedfs/testData/includeIfNotExists/wcontext_child.jet @@ -0,0 +1 @@ +Hi, {{.}}! \ No newline at end of file