Skip to content

Commit 5e0563b

Browse files
authored
chore: golangci cleanup (#107)
1 parent 0733292 commit 5e0563b

18 files changed

+147
-133
lines changed

.github/workflows/lint.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
- v1
6+
pull_request:
7+
branches:
8+
- "**"
9+
name: Linter
10+
jobs:
11+
golangci:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
with:
16+
path: src/github.com/unrolled/render
17+
- uses: golangci/golangci-lint-action@v6
18+
with:
19+
working-directory: src/github.com/unrolled/render

.github/workflows/test.yaml

+5-14
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,17 @@ on:
66
pull_request:
77
branches:
88
- "**"
9-
name: Test
9+
name: Tests
1010
jobs:
1111
tests:
1212
strategy:
1313
matrix:
14-
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x]
14+
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x]
1515
os: [ubuntu-latest]
1616
runs-on: ${{ matrix.os }}
1717
steps:
18-
- name: Install Go
19-
uses: actions/setup-go@v2
18+
- uses: actions/checkout@v4
19+
- uses: actions/setup-go@v5
2020
with:
2121
go-version: ${{ matrix.go-version }}
22-
- name: Checkout code
23-
uses: actions/checkout@v2
24-
- name: Test
25-
run: make ci
26-
golangci:
27-
runs-on: ubuntu-latest
28-
steps:
29-
- uses: actions/checkout@v2
30-
- name: golangci-lint
31-
uses: golangci/golangci-lint-action@v2
22+
- run: make ci

.golangci.yaml

+21-30
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
1+
run:
2+
timeout: 10m
3+
modules-download-mode: readonly
4+
allow-parallel-runners: true
5+
16
issues:
27
exclude:
38
- G203
4-
5-
run:
6-
timeout: 5m
9+
exclude-rules:
10+
- path: _test\.go
11+
linters:
12+
- lll
13+
- err113
714

815
linters:
916
enable-all: true
1017
disable:
11-
# Deprecated linters
12-
- varcheck
13-
- exhaustivestruct
14-
- ifshort
15-
- structcheck
16-
- golint
17-
- maligned
18-
- interfacer
19-
- nosnakecase
20-
- deadcode
21-
- scopelint
22-
- rowserrcheck
23-
- sqlclosecheck
24-
- structcheck
25-
- wastedassign
26-
# Ignoring
27-
- lll
28-
- varnamelen
29-
- paralleltest
30-
- testpackage
31-
- goerr113
18+
- copyloopvar
19+
- cyclop
20+
- depguard
21+
- execinquery
3222
- exhaustruct
33-
- nestif
23+
- exportloopref
3424
- funlen
35-
- goconst
36-
- cyclop
37-
- gocyclo
3825
- gocognit
39-
- maintidx
26+
- goconst
27+
- gomnd
28+
- intrange
29+
- paralleltest
30+
- testpackage
31+
- varnamelen
4032
- wrapcheck
41-
- depguard

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ r := render.New(render.Options{
9999
HTMLContentType: "application/xhtml+xml", // Output XHTML content type instead of default "text/html".
100100
IsDevelopment: true, // Render will now recompile the templates on every HTML response.
101101
UseMutexLock: true, // Overrides the default no lock implementation and uses the standard `sync.RWMutex` lock.
102-
UnEscapeHTML: true, // Replace ensure '&<>' are output correctly (JSON only).
102+
UnEscapeHTML: true, // Ensure '&<>' are output correctly (JSON only).
103103
StreamingJSON: true, // Streams the JSON response via json.Encoder.
104104
HTMLTemplateOption: "missingkey=error", // Sets the option value for HTML templates. See https://pkg.go.dev/html/template#Template.Option for a list of known options.
105105
RequirePartials: true, // Return an error if a template is missing a partial used in a layout.

engine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// Engine is the generic interface for all responses.
1313
type Engine interface {
14-
Render(io.Writer, interface{}) error
14+
Render(w io.Writer, v interface{}) error
1515
}
1616

1717
// Head defines the basic ContentType and Status fields.

fs_embed_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestEmbedFileSystemHTMLBasic(t *testing.T) {
4444

4545
var err error
4646

47-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
4848
err = render.HTML(w, http.StatusOK, "hello", "gophers")
4949
})
5050

fs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestIOFSEmbedHTMLBasic(t *testing.T) {
5656

5757
var err error
5858

59-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
6060
err = render.HTML(w, http.StatusOK, "hello", "gophers")
6161
})
6262

@@ -78,7 +78,7 @@ func TestIOFSDirHTMLBasic(t *testing.T) {
7878

7979
var err error
8080

81-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
81+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
8282
err = render.HTML(w, http.StatusOK, "hello", "gophers")
8383
})
8484

genericbufferpool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import "bytes"
55
// GenericBufferPool abstracts buffer pool implementations.
66
type GenericBufferPool interface {
77
Get() *bytes.Buffer
8-
Put(*bytes.Buffer)
8+
Put(b *bytes.Buffer)
99
}

helpers.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package render
22

33
import (
4-
"fmt"
4+
"errors"
55
"html/template"
66
)
77

8+
var (
9+
ErrYieldNoLayoutDefined = errors.New("yield called with no layout defined")
10+
ErrBlockNoLayoutDefined = errors.New("block called with no layout defined")
11+
)
12+
813
// Included helper functions for use when rendering HTML.
914
func helperFuncs() template.FuncMap {
1015
return template.FuncMap{
1116
"yield": func() (string, error) {
12-
return "", fmt.Errorf("yield called with no layout defined")
17+
return "", ErrYieldNoLayoutDefined
1318
},
1419
"partial": func() (string, error) {
15-
return "", fmt.Errorf("block called with no layout defined")
20+
return "", ErrBlockNoLayoutDefined
1621
},
1722
"current": func() (string, error) {
1823
return "", nil

helpers_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestRenderPartial(t *testing.T) {
1414

1515
var renErr error
1616

17-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1818
renErr = render.HTML(w, http.StatusOK, "content", "gophers")
1919
})
2020

@@ -38,7 +38,7 @@ func TestRenderPartialRequirePartialsOff(t *testing.T) {
3838
RequirePartials: false,
3939
})
4040

41-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
41+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
4242
_ = render.HTML(w, http.StatusOK, "content-partial", "gophers")
4343
})
4444

@@ -61,7 +61,7 @@ func TestRenderPartialRequirePartialsOn(t *testing.T) {
6161
RequirePartials: true,
6262
})
6363

64-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
64+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
6565
_ = render.HTML(w, http.StatusOK, "content-partial", "gophers")
6666
})
6767

render.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ type Options struct {
6767
Layout string
6868
// Extensions to parse template files from. Defaults to [".tmpl"].
6969
Extensions []string
70-
// Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Defaults to empty map.
70+
// Funcs is a slice of FuncMaps to apply to the template upon compilation.
71+
// This is useful for helper functions. Defaults to empty map.
7172
Funcs []template.FuncMap
7273
// Delims sets the action delimiters to the specified strings in the Delims struct.
7374
Delims Delims
7475
// Appends the given character set to the Content-Type header. Default is "UTF-8".
7576
Charset string
76-
// If DisableCharset is set to true, it will not append the above Charset value to the Content-Type header. Default is false.
77+
// If DisableCharset is set to true, it will not append the above Charset value to the Content-Type header.
78+
// Default is false.
7779
DisableCharset bool
7880
// Outputs human readable JSON.
7981
IndentJSON bool
@@ -97,23 +99,27 @@ type Options struct {
9799
XMLContentType string
98100
// If IsDevelopment is set to true, this will recompile the templates on every request. Default is false.
99101
IsDevelopment bool
100-
// If UseMutexLock is set to true, the standard `sync.RWMutex` lock will be used instead of the lock free implementation. Default is false.
101-
// Note that when `IsDevelopment` is true, the standard `sync.RWMutex` lock is always used. Lock free is only a production feature.
102+
// If UseMutexLock is set to true, the standard `sync.RWMutex` lock will be used instead of the lock free
103+
// implementation. Default is false. Note that when `IsDevelopment` is true, the standard `sync.RWMutex`
104+
// lock is always used. Lock free is only a production feature.
102105
UseMutexLock bool
103106
// Unescape HTML characters "&<>" to their original values. Default is false.
104107
UnEscapeHTML bool
105108
// Sets the `Option` value for HTML templates. Defaults to blank ("").
106109
HTMLTemplateOption string
107110
// Streams JSON responses instead of marshalling prior to sending. Default is false.
108111
StreamingJSON bool
109-
// Require that all partials executed in the layout are implemented in all templates using the layout. Default is false.
112+
// Require that all partials executed in the layout are implemented in all templates using the layout.
113+
// Default is false.
110114
RequirePartials bool
111115
// Deprecated: Use the above `RequirePartials` instead of this. As of Go 1.6, blocks are built in. Default is false.
112116
RequireBlocks bool
113117
// Disables automatic rendering of http.StatusInternalServerError when an error occurs. Default is false.
114118
DisableHTTPErrorRendering bool
115-
// Enables using partials without the current filename suffix which allows use of the same template in multiple files. e.g {{ partial "carosuel" }} inside the home template will match carosel-home or carosel.
116-
// ***NOTE*** - This option should be named RenderPartialsWithoutSuffix as that is what it does. "Prefix" is a typo. Maintaining the existing name for backwards compatibility.
119+
// Enables using partials without the current filename suffix which allows use of the same template in
120+
// multiple files. e.g {{ partial "carousel" }} inside the home template will match carousel-home or carousel.
121+
// ***NOTE*** - This option should be named RenderPartialsWithoutSuffix as that is what it does.
122+
// "Prefix" is a typo. Maintaining the existing name for backwards compatibility.
117123
RenderPartialsWithoutPrefix bool
118124
// BufferPool to use when rendering HTML templates. If none is supplied
119125
// defaults to SizedBufferPool of size 32 with 512KiB buffers.
@@ -247,7 +253,8 @@ func (r *Render) compileTemplatesFromDir() {
247253

248254
watcher, err = fsnotify.NewWatcher()
249255
if err != nil {
250-
log.Printf("Unable to create new watcher for template files. Templates will be recompiled on every render. Error: %v\n", err)
256+
log.Printf("Unable to create new watcher for template files. "+
257+
"Templates will be recompiled on every render. Error: %v\n", err)
251258
}
252259
}
253260

@@ -260,6 +267,7 @@ func (r *Render) compileTemplatesFromDir() {
260267
if info != nil && watcher != nil {
261268
_ = watcher.Add(path)
262269
}
270+
263271
if info == nil || info.IsDir() {
264272
return nil
265273
}

render_data_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestDataBinaryBasic(t *testing.T) {
1313

1414
var err error
1515

16-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1717
err = render.Data(w, 299, []byte("hello there"))
1818
})
1919

@@ -34,7 +34,7 @@ func TestDataCustomMimeType(t *testing.T) {
3434

3535
var err error
3636

37-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
3838
w.Header().Set(ContentType, "image/jpeg")
3939
err = render.Data(w, http.StatusOK, []byte("..jpeg data.."))
4040
})
@@ -56,7 +56,7 @@ func TestDataCustomContentType(t *testing.T) {
5656

5757
var err error
5858

59-
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59+
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
6060
err = render.Data(w, http.StatusOK, []byte("..png data.."))
6161
})
6262

0 commit comments

Comments
 (0)