Skip to content

Commit

Permalink
Add go1.16 embed.FS loader
Browse files Browse the repository at this point in the history
Addresses: CloudyKit#191
  • Loading branch information
ryanlath committed Mar 3, 2024
1 parent 1926e5e commit 1828646
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions loaders/embedfs/embedfs_test.go
Original file line number Diff line number Diff line change
@@ -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!")
}
34 changes: 34 additions & 0 deletions loaders/embedfs/loader.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions loaders/embedfs/testData/includeIfNotExists/existent.jet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ includeIfExists: "exists.jet"}}
1 change: 1 addition & 0 deletions loaders/embedfs/testData/includeIfNotExists/exists.jet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi, i exist!!
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{ if includeIfExists("exists.jet") }}
Was included!!
{{ end }}
{{ if includeIfExists("notExists.jet") }}
Was included!!
{{ else }}
Was not included!!
{{ end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ includeIfExists: "notExists.jet"}}
2 changes: 2 additions & 0 deletions loaders/embedfs/testData/includeIfNotExists/wcontext.jet
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ includeIfExists("wcontext_child","Buddy") }}
{{ includeIfExists("wcontext_child") }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi, {{.}}!

0 comments on commit 1828646

Please sign in to comment.