-
Notifications
You must be signed in to change notification settings - Fork 4
feat(text): add TextViewEngine/TextViewer #23
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
Conversation
Reviewer's Guide by SourceryThis pull request introduces support for text-based templates by adding a new Sequence diagram for text template rendering processsequenceDiagram
participant C as Client
participant A as App
participant TV as TextViewer
participant TT as TextTemplate
C->>A: Request text template
A->>TV: Render(w, r, data)
TV->>TV: Set Content-Type header
TV->>TT: Execute(buffer, data)
TT-->>TV: Rendered content
TV-->>A: Write to response
A-->>C: Send response
Class diagram for new TextViewEngine and TextViewer componentsclassDiagram
class TextViewEngine {
-fsys fs.FS
-app *App
-templates map[string]*TextTemplate
+Load(fsys fs.FS, app *App) error
+FileChanged(fsys fs.FS, app *App, event fsnotify.Event) error
-loadText(path string) error
-loadTemplate(path string) *TextTemplate
}
class TextTemplate {
-template *template.Template
-name string
-mime string
-charset string
+Load(fsys fs.FS) error
+Reload(fsys fs.FS) error
+Execute(wr io.Writer, data any) error
}
class TextViewer {
-template *TextTemplate
+MimeType() string
+Render(w http.ResponseWriter, r *http.Request, data any) error
}
TextViewEngine --> TextTemplate : manages
TextViewer --> TextTemplate : uses
note for TextViewEngine "Handles text template loading and file watching"
note for TextViewer "Renders text templates with proper MIME types"
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Here's the code health analysis summary for commits Analysis Summary
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #23 +/- ##
==========================================
+ Coverage 89.27% 89.79% +0.52%
==========================================
Files 28 32 +4
Lines 1063 1176 +113
==========================================
+ Hits 949 1056 +107
- Misses 79 83 +4
- Partials 35 37 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
Hey @cnlangzi - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 2 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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.
Hey @cnlangzi - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟡 Security: 1 issue found
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@sourcery-ai review |
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.
Hey @cnlangzi - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 1 issue found
- 🟢 Security: all looks good
- 🟡 Testing: 1 issue found
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@@ -79,7 +79,7 @@ func (c *Context) View(items ...any) error { | |||
mime := v.MimeType() | |||
ok = false | |||
for _, accept := range c.Accept() { | |||
if mime == accept { | |||
if mime == accept || mime == "*/*" || accept == "*/*" { |
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.
suggestion: Consider reordering MIME type checks to prioritize exact matches
The current order could match wildcards before more specific MIME types. Consider checking exact matches first: if mime == accept || (accept == "*/*" || mime == "*/*")
if mime == accept || mime == "*/*" || accept == "*/*" { | |
if mime == accept || (mime == "*/*" || accept == "*/*") { |
"github.com/yaitoo/xun/fsnotify" | ||
) | ||
|
||
func TestTextViewEngine(t *testing.T) { |
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.
suggestion (testing): Test cases for non-existent files.
Add a test case where the requested text file does not exist in the fsys. This verifies that the engine handles this scenario gracefully, perhaps by returning a 404 error or a default response.
Suggested implementation:
fsys := fstest.MapFS{
"text/sitemap.xml": {Data: []byte(`<?xml version="1.0" encoding="UTF-8"?>`)},
"text/robots.txt": {Data: []byte("User-agent: *")},
"text/empty.md": {},
}
mux := http.NewServeMux()
srv := httptest.NewServer(mux)
defer srv.Close()
app := New(WithMux(mux), WithFsys(fsys))
t.Run("non-existent file returns 404", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/text/nonexistent.txt", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
require.Equal(t, http.StatusNotFound, w.Code)
})
The exact status code (404) assumes that's how your engine handles missing files. If your engine uses a different status code or handling mechanism for missing files, you'll need to adjust the expected status code in the test accordingly.
Changed
Fixed
Added
TextViewEngine
andTextViewer
Tests
Tasks to complete before merging PR:
make unit-test
to check for any regressions 📋make lint
to check for any issuesSummary by Sourcery
Add support for text files as views, allowing them to be rendered directly by the application. Update the view engine to handle empty templates correctly, preventing errors during rendering.
New Features:
TextViewEngine
andTextViewer
to support rendering text files as views.Tests: