feat: added support for registering embedded filesystems as view sources - #1546
feat: added support for registering embedded filesystems as view sources#1546protibimbok wants to merge 3 commits into
Conversation
Added View.LoadViewsFromFS(fsys, root) and View.RegisteredViewFS() so packages can ship templates in an embed.FS instead of an on-disk directory. The fs is rooted with fs.Sub at registration time, so consumers get uniform sources with template paths relative to \".\". Exists() now also checks registered filesystems. Precedence is unchanged: app views > LoadViewsFrom dirs > LoadViewsFromFS, each in registration order.
embedded views
Added an embed.FS fixture under view/testdata and tests for: registering
directories and filesystems side by side, root normalisation ("views/",
"/views", "./views", OS separators, ".", "" and "/"), nil/escaping
roots, Exists() across app, directory and embedded sources, and
end-to-end template resolution with app > directory > embedded
precedence, including a nested layout/partial/block chain and missing
templates.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1546 +/- ##
==========================================
+ Coverage 72.41% 72.69% +0.27%
==========================================
Files 409 412 +3
Lines 26475 26772 +297
==========================================
+ Hits 19172 19462 +290
- Misses 7301 7308 +7
Partials 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks, checking |
|
Not sure if you have noticed but please check goravel/gin#242 too |
ReviewThanks for the PR — clean, well-tested change overall. A few findings below.
Should Fix1.
2. Backslash normalization is a no-op on non-Windows —
3. Panic vs silent divergence between sibling methods —
Nits
Verified correct
|
- LoadViewsFromFS now stats the root after fs.Sub: a missing root panics with ViewInvalidFSRoot and a file root panics with the new ViewFSRootNotDirectory, instead of registering a source that never matches anything. - Exists no longer reports directories, "" or "." as views for app paths, LoadViewsFrom paths or embedded filesystems. - normalizeFSPath replaces backslashes on every platform rather than only on Windows via filepath.ToSlash. - Document accepted root spellings and the eager-panic rationale on the View contract; add tests for the new cases.
📑 Description
Closes goravel/goravel#990
Packages can now ship their views inside the binary with
go:embedinstead ofrequiring template files on disk at runtime.
Added
View.LoadViewsFromFS(fsys fs.FS, root string)— registers anfs.FS(e.g. an
embed.FS) as a package view source.rootselects the directoryinside the FS; pass
"."to use the whole FS.View.RegisteredViewFS() []fs.FS— returns the registered filesystems inregistration order, for HTTP drivers to load templates from.
ViewFSRequiredandViewInvalidFSRoot(panics on a nil FS or aninvalid root, matching how a bad path fails today).
Behaviour
fs.Subat registration time, so every source looksthe same to consumers: template paths are relative to
"."(e.g.layouts/app.tmpl), including nested directories.rootaccepts loose forms ("",./views,/views/,views\admin) and isnormalised to a valid
io/fspath.Exists()also checks registered filesystems.LoadViewsFromdirs >LoadViewsFromFS, each in registration order. ExistingLoadViewsFromusers are unaffected.
Notes
mocks/viewmock is regenerated for the new interface methods.a companion
goravel/ginPR follows once this is tagged.✅ Checks