-
Notifications
You must be signed in to change notification settings - Fork 0
fix: embed templates using go:embed #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
.github/workflows/go-coverage.yml → .github/workflows/coverage.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| name: Go coverage report | ||
| name: Coverage report | ||
|
|
||
| on: | ||
| workflow_run: | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
.github/workflows/go-quality.yml → .github/workflows/quality.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| name: Go quality checks | ||
| name: Quality checks | ||
|
|
||
| on: | ||
| push: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package template | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "strings" | ||
|
|
||
| "github.com/cbroglie/mustache" | ||
| ) | ||
|
|
||
| // NewEngineFromFS creates a new template engine from an embedded filesystem. | ||
| // The subdir parameter specifies the subdirectory within the fs.FS to use as | ||
| // the template root (e.g., "typescript-fetch"). | ||
| func NewEngineFromFS(fsys fs.FS, subdir string) *Engine { | ||
| return &Engine{ | ||
| TemplateDir: subdir, | ||
| partials: make(map[string]string), | ||
| Lambdas: make(map[string]func(text string, render mustache.RenderFunc) (string, error)), | ||
| fsys: fsys, | ||
| } | ||
| } | ||
|
|
||
| // LoadPartialsFromFS loads all partial templates from the embedded filesystem. | ||
| func (e *Engine) LoadPartialsFromFS() error { | ||
| if e.fsys == nil { | ||
| return fmt.Errorf("no embedded filesystem configured") | ||
| } | ||
|
|
||
| return fs.WalkDir(e.fsys, e.TemplateDir, func(path string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if d.IsDir() || !strings.HasSuffix(path, ".mustache") { | ||
| return nil | ||
| } | ||
|
|
||
| content, err := fs.ReadFile(e.fsys, path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read partial %s: %w", path, err) | ||
| } | ||
|
|
||
| // Get relative name without extension | ||
| // path is like "typescript-fetch/models.mustache" | ||
| // We want just "models" as the partial name | ||
| name := strings.TrimPrefix(path, e.TemplateDir+"/") | ||
| name = strings.TrimSuffix(name, ".mustache") | ||
|
|
||
| e.partials[name] = string(content) | ||
| if e.Verbose { | ||
| fmt.Printf("[TEMPLATE] Loaded embedded partial: %s\n", name) | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Package templates provides embedded template files for code generation. | ||
| package templates | ||
|
|
||
| import "embed" | ||
|
|
||
| // FS contains all embedded template files. | ||
| // | ||
| //go:embed typescript-fetch/*.mustache typescript-fetch/*.md | ||
| var FS embed.FS |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path construction in LoadPartialsFromFS uses simple string concatenation with "/" but doesn't handle potential edge cases where partial names might not normalize correctly. The original LoadPartials uses filepath.Rel to compute the relative path, then normalizes separators with strings.ReplaceAll. For consistency and to handle potential subdirectories within the template directory correctly, consider using a similar normalization approach:
Instead of:
Consider handling cases where the template directory path might not have a trailing separator, and ensure proper handling of nested partials. For example, if there are subdirectories in typescript-fetch, the partial name computation should be consistent with the filesystem-based version.