Skip to content

Commit

Permalink
refactor: refactor template handling and request functions
Browse files Browse the repository at this point in the history
- Add `.golangci.yml` configuration file with specific linters enabled and a 3-minute timeout.
- Refactor `AddFromStringsFuncs` method signature in `dynamic.go` and `multitemplate.go` for better readability.
- Update `performRequest` function to remove method and path parameters, defaulting to `GET` and `/`.
- Modify `createFromStringsWithFuncsDynamic` and `createFromStringsWithFuncs` tests to use the new `AddFromStringsFuncs` method signature.
- Simplify `performRequest` calls in multiple test functions by removing redundant parameters.
- Adjust template loading logic in `example/advanced/example.go` and `example/multibase/example.go` to use `layoutCopy` directly.

Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy committed Sep 27, 2024
1 parent bb7ab1d commit d2d77df
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 24 deletions.
39 changes: 39 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
linters:
enable-all: false
disable-all: true
fast: false
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- exhaustive
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- gofumpt

run:
timeout: 3m
6 changes: 5 additions & 1 deletion dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ func (r DynamicRender) AddFromString(name, templateString string) *template.Temp
}

// AddFromStringsFuncs supply add template from strings
func (r DynamicRender) AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template {
func (r DynamicRender) AddFromStringsFuncs(
name string,
funcMap template.FuncMap,
templateStrings ...string,
) *template.Template {
builder := &templateBuilder{
templateName: name, funcMap: funcMap,
templateStrings: templateStrings,
Expand Down
18 changes: 11 additions & 7 deletions dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func createFromStringDynamic() Renderer {

func createFromStringsWithFuncsDynamic() Renderer {
r := NewRenderer()
r.AddFromStringsFuncs("index", template.FuncMap{}, `Welcome to {{ .name }} {{template "content"}}`, `{{define "content"}}template{{end}}`)
r.AddFromStringsFuncs(
"index",
template.FuncMap{},
`Welcome to {{ .name }} {{template "content"}}`, `{{define "content"}}template{{end}}`,
)

return r
}
Expand Down Expand Up @@ -79,7 +83,7 @@ func TestAddFromFilesDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
Expand All @@ -93,7 +97,7 @@ func TestAddFromGlobDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is login template\n", w.Body.String())
}
Expand All @@ -107,7 +111,7 @@ func TestAddFromFSDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
Expand All @@ -121,7 +125,7 @@ func TestAddFromStringDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
Expand All @@ -135,7 +139,7 @@ func TestAddFromStringsFruncsDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
Expand All @@ -149,7 +153,7 @@ func TestAddFromFilesFruncsDynamic(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template\n", w.Body.String())
}
Expand Down
4 changes: 2 additions & 2 deletions example/advanced/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func loadTemplates(templatesDir string) multitemplate.Renderer {
for _, include := range includes {
layoutCopy := make([]string, len(layouts))
copy(layoutCopy, layouts)
files := append(layoutCopy, include)
r.AddFromFiles(filepath.Base(include), files...)
layoutCopy = append(layoutCopy, include)
r.AddFromFiles(filepath.Base(include), layoutCopy...)
}
return r
}
8 changes: 4 additions & 4 deletions example/multibase/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func loadTemplates(templatesDir string) multitemplate.Renderer {
for _, article := range articles {
layoutCopy := make([]string, len(articleLayouts))
copy(layoutCopy, articleLayouts)
files := append(layoutCopy, article)
r.AddFromFiles(filepath.Base(article), files...)
layoutCopy = append(layoutCopy, article)
r.AddFromFiles(filepath.Base(article), layoutCopy...)
}

adminLayouts, err := filepath.Glob(templatesDir + "/layouts/admin-base.html")
Expand All @@ -62,8 +62,8 @@ func loadTemplates(templatesDir string) multitemplate.Renderer {
for _, admin := range admins {
layoutCopy := make([]string, len(adminLayouts))
copy(layoutCopy, adminLayouts)
files := append(layoutCopy, admin)
r.AddFromFiles(filepath.Base(admin), files...)
layoutCopy = append(layoutCopy, admin)
r.AddFromFiles(filepath.Base(admin), layoutCopy...)
}
return r
}
6 changes: 5 additions & 1 deletion multitemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ func (r Render) AddFromString(name, templateString string) *template.Template {
}

// AddFromStringsFuncs supply add template from strings
func (r Render) AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template {
func (r Render) AddFromStringsFuncs(
name string,
funcMap template.FuncMap,
templateStrings ...string,
) *template.Template {
tmpl := template.New(name).Funcs(funcMap)

for _, ts := range templateStrings {
Expand Down
22 changes: 13 additions & 9 deletions multitemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/stretchr/testify/assert"
)

func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequestWithContext(context.Background(), method, path, nil)
func performRequest(r http.Handler) *httptest.ResponseRecorder {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
Expand Down Expand Up @@ -49,7 +49,11 @@ func createFromString() Render {

func createFromStringsWithFuncs() Render {
r := New()
r.AddFromStringsFuncs("index", template.FuncMap{}, `Welcome to {{ .name }} {{template "content"}}`, `{{define "content"}}template{{end}}`)
r.AddFromStringsFuncs(
"index",
template.FuncMap{},
`Welcome to {{ .name }} {{template "content"}}`, `{{define "content"}}template{{end}}`,
)

return r
}
Expand Down Expand Up @@ -82,7 +86,7 @@ func TestAddFromFiles(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
Expand All @@ -96,7 +100,7 @@ func TestAddFromGlob(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is login template\n", w.Body.String())
}
Expand All @@ -110,7 +114,7 @@ func TestAddFromFS(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
Expand All @@ -124,7 +128,7 @@ func TestAddFromString(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
Expand All @@ -138,7 +142,7 @@ func TestAddFromStringsFruncs(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
Expand All @@ -152,7 +156,7 @@ func TestAddFromFilesFruncs(t *testing.T) {
})
})

w := performRequest(router, "GET", "/")
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template\n", w.Body.String())
}
Expand Down

0 comments on commit d2d77df

Please sign in to comment.