From 5e0563b79c70845ef40b5b5ffe2dd40a3a0ff4ff Mon Sep 17 00:00:00 2001 From: Cory Jacobsen Date: Sat, 28 Sep 2024 18:00:47 -0600 Subject: [PATCH] chore: golangci cleanup (#107) --- .github/workflows/lint.yaml | 19 +++++++++++++ .github/workflows/test.yaml | 19 ++++--------- .golangci.yaml | 51 ++++++++++++++------------------- README.md | 2 +- engine.go | 2 +- fs_embed_test.go | 2 +- fs_test.go | 4 +-- genericbufferpool.go | 2 +- helpers.go | 11 ++++++-- helpers_test.go | 6 ++-- render.go | 24 ++++++++++------ render_data_test.go | 6 ++-- render_html_test.go | 38 ++++++++++++------------- render_json_test.go | 56 ++++++++++++++++++------------------- render_jsonp_test.go | 10 +++---- render_test.go | 6 ++-- render_text_test.go | 10 +++---- render_xml_test.go | 12 ++++---- 18 files changed, 147 insertions(+), 133 deletions(-) create mode 100644 .github/workflows/lint.yaml diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..117e017 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,19 @@ +on: + push: + branches: + - master + - v1 + pull_request: + branches: + - "**" +name: Linter +jobs: + golangci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + path: src/github.com/unrolled/render + - uses: golangci/golangci-lint-action@v6 + with: + working-directory: src/github.com/unrolled/render diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 40a94fc..d3b9d04 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -6,26 +6,17 @@ on: pull_request: branches: - "**" -name: Test +name: Tests jobs: tests: strategy: matrix: - go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x] + go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x] os: [ubuntu-latest] runs-on: ${{ matrix.os }} steps: - - name: Install Go - uses: actions/setup-go@v2 + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} - - name: Checkout code - uses: actions/checkout@v2 - - name: Test - run: make ci - golangci: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + - run: make ci diff --git a/.golangci.yaml b/.golangci.yaml index 0ce6920..42a44b7 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,41 +1,32 @@ +run: + timeout: 10m + modules-download-mode: readonly + allow-parallel-runners: true + issues: exclude: - G203 - -run: - timeout: 5m + exclude-rules: + - path: _test\.go + linters: + - lll + - err113 linters: enable-all: true disable: - # Deprecated linters - - varcheck - - exhaustivestruct - - ifshort - - structcheck - - golint - - maligned - - interfacer - - nosnakecase - - deadcode - - scopelint - - rowserrcheck - - sqlclosecheck - - structcheck - - wastedassign - # Ignoring - - lll - - varnamelen - - paralleltest - - testpackage - - goerr113 + - copyloopvar + - cyclop + - depguard + - execinquery - exhaustruct - - nestif + - exportloopref - funlen - - goconst - - cyclop - - gocyclo - gocognit - - maintidx + - goconst + - gomnd + - intrange + - paralleltest + - testpackage + - varnamelen - wrapcheck - - depguard diff --git a/README.md b/README.md index 58ec566..cdbc1d7 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ r := render.New(render.Options{ HTMLContentType: "application/xhtml+xml", // Output XHTML content type instead of default "text/html". IsDevelopment: true, // Render will now recompile the templates on every HTML response. UseMutexLock: true, // Overrides the default no lock implementation and uses the standard `sync.RWMutex` lock. - UnEscapeHTML: true, // Replace ensure '&<>' are output correctly (JSON only). + UnEscapeHTML: true, // Ensure '&<>' are output correctly (JSON only). StreamingJSON: true, // Streams the JSON response via json.Encoder. 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. RequirePartials: true, // Return an error if a template is missing a partial used in a layout. diff --git a/engine.go b/engine.go index db32d48..69701da 100644 --- a/engine.go +++ b/engine.go @@ -11,7 +11,7 @@ import ( // Engine is the generic interface for all responses. type Engine interface { - Render(io.Writer, interface{}) error + Render(w io.Writer, v interface{}) error } // Head defines the basic ContentType and Status fields. diff --git a/fs_embed_test.go b/fs_embed_test.go index eb921d3..42069f2 100644 --- a/fs_embed_test.go +++ b/fs_embed_test.go @@ -44,7 +44,7 @@ func TestEmbedFileSystemHTMLBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) diff --git a/fs_test.go b/fs_test.go index a15547b..fb8bef3 100644 --- a/fs_test.go +++ b/fs_test.go @@ -56,7 +56,7 @@ func TestIOFSEmbedHTMLBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) @@ -78,7 +78,7 @@ func TestIOFSDirHTMLBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) diff --git a/genericbufferpool.go b/genericbufferpool.go index 6052b34..f4bdb51 100644 --- a/genericbufferpool.go +++ b/genericbufferpool.go @@ -5,5 +5,5 @@ import "bytes" // GenericBufferPool abstracts buffer pool implementations. type GenericBufferPool interface { Get() *bytes.Buffer - Put(*bytes.Buffer) + Put(b *bytes.Buffer) } diff --git a/helpers.go b/helpers.go index f979503..37eff0c 100644 --- a/helpers.go +++ b/helpers.go @@ -1,18 +1,23 @@ package render import ( - "fmt" + "errors" "html/template" ) +var ( + ErrYieldNoLayoutDefined = errors.New("yield called with no layout defined") + ErrBlockNoLayoutDefined = errors.New("block called with no layout defined") +) + // Included helper functions for use when rendering HTML. func helperFuncs() template.FuncMap { return template.FuncMap{ "yield": func() (string, error) { - return "", fmt.Errorf("yield called with no layout defined") + return "", ErrYieldNoLayoutDefined }, "partial": func() (string, error) { - return "", fmt.Errorf("block called with no layout defined") + return "", ErrBlockNoLayoutDefined }, "current": func() (string, error) { return "", nil diff --git a/helpers_test.go b/helpers_test.go index f69906b..01784c0 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -14,7 +14,7 @@ func TestRenderPartial(t *testing.T) { var renErr error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { renErr = render.HTML(w, http.StatusOK, "content", "gophers") }) @@ -38,7 +38,7 @@ func TestRenderPartialRequirePartialsOff(t *testing.T) { RequirePartials: false, }) - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _ = render.HTML(w, http.StatusOK, "content-partial", "gophers") }) @@ -61,7 +61,7 @@ func TestRenderPartialRequirePartialsOn(t *testing.T) { RequirePartials: true, }) - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _ = render.HTML(w, http.StatusOK, "content-partial", "gophers") }) diff --git a/render.go b/render.go index 8ab3c63..f6b3a8b 100644 --- a/render.go +++ b/render.go @@ -67,13 +67,15 @@ type Options struct { Layout string // Extensions to parse template files from. Defaults to [".tmpl"]. Extensions []string - // Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Defaults to empty map. + // Funcs is a slice of FuncMaps to apply to the template upon compilation. + // This is useful for helper functions. Defaults to empty map. Funcs []template.FuncMap // Delims sets the action delimiters to the specified strings in the Delims struct. Delims Delims // Appends the given character set to the Content-Type header. Default is "UTF-8". Charset string - // If DisableCharset is set to true, it will not append the above Charset value to the Content-Type header. Default is false. + // If DisableCharset is set to true, it will not append the above Charset value to the Content-Type header. + // Default is false. DisableCharset bool // Outputs human readable JSON. IndentJSON bool @@ -97,8 +99,9 @@ type Options struct { XMLContentType string // If IsDevelopment is set to true, this will recompile the templates on every request. Default is false. IsDevelopment bool - // If UseMutexLock is set to true, the standard `sync.RWMutex` lock will be used instead of the lock free implementation. Default is false. - // Note that when `IsDevelopment` is true, the standard `sync.RWMutex` lock is always used. Lock free is only a production feature. + // If UseMutexLock is set to true, the standard `sync.RWMutex` lock will be used instead of the lock free + // implementation. Default is false. Note that when `IsDevelopment` is true, the standard `sync.RWMutex` + // lock is always used. Lock free is only a production feature. UseMutexLock bool // Unescape HTML characters "&<>" to their original values. Default is false. UnEscapeHTML bool @@ -106,14 +109,17 @@ type Options struct { HTMLTemplateOption string // Streams JSON responses instead of marshalling prior to sending. Default is false. StreamingJSON bool - // Require that all partials executed in the layout are implemented in all templates using the layout. Default is false. + // Require that all partials executed in the layout are implemented in all templates using the layout. + // Default is false. RequirePartials bool // Deprecated: Use the above `RequirePartials` instead of this. As of Go 1.6, blocks are built in. Default is false. RequireBlocks bool // Disables automatic rendering of http.StatusInternalServerError when an error occurs. Default is false. DisableHTTPErrorRendering bool - // 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. - // ***NOTE*** - This option should be named RenderPartialsWithoutSuffix as that is what it does. "Prefix" is a typo. Maintaining the existing name for backwards compatibility. + // Enables using partials without the current filename suffix which allows use of the same template in + // multiple files. e.g {{ partial "carousel" }} inside the home template will match carousel-home or carousel. + // ***NOTE*** - This option should be named RenderPartialsWithoutSuffix as that is what it does. + // "Prefix" is a typo. Maintaining the existing name for backwards compatibility. RenderPartialsWithoutPrefix bool // BufferPool to use when rendering HTML templates. If none is supplied // defaults to SizedBufferPool of size 32 with 512KiB buffers. @@ -247,7 +253,8 @@ func (r *Render) compileTemplatesFromDir() { watcher, err = fsnotify.NewWatcher() if err != nil { - log.Printf("Unable to create new watcher for template files. Templates will be recompiled on every render. Error: %v\n", err) + log.Printf("Unable to create new watcher for template files. "+ + "Templates will be recompiled on every render. Error: %v\n", err) } } @@ -260,6 +267,7 @@ func (r *Render) compileTemplatesFromDir() { if info != nil && watcher != nil { _ = watcher.Add(path) } + if info == nil || info.IsDir() { return nil } diff --git a/render_data_test.go b/render_data_test.go index ba355ab..086dc4d 100644 --- a/render_data_test.go +++ b/render_data_test.go @@ -13,7 +13,7 @@ func TestDataBinaryBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.Data(w, 299, []byte("hello there")) }) @@ -34,7 +34,7 @@ func TestDataCustomMimeType(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set(ContentType, "image/jpeg") err = render.Data(w, http.StatusOK, []byte("..jpeg data..")) }) @@ -56,7 +56,7 @@ func TestDataCustomContentType(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.Data(w, http.StatusOK, []byte("..png data..")) }) diff --git a/render_html_test.go b/render_html_test.go index 3d3bdde..2633270 100644 --- a/render_html_test.go +++ b/render_html_test.go @@ -17,7 +17,7 @@ func TestHTMLBad(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "nope", nil) }) @@ -38,7 +38,7 @@ func TestHTMLBadDisableHTTPErrorRendering(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "nope", nil) }) @@ -58,7 +58,7 @@ func TestHTMLBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) @@ -112,7 +112,7 @@ func TestHTMLXHTML(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) @@ -134,7 +134,7 @@ func TestHTMLExtensions(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hypertext", nil) }) @@ -160,7 +160,7 @@ func TestHTMLFuncs(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "index", "gophers") }) @@ -180,7 +180,7 @@ func TestRenderLayout(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "content", "gophers") }) @@ -200,7 +200,7 @@ func TestHTMLLayoutCurrent(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "content", "gophers") }) @@ -219,7 +219,7 @@ func TestHTMLNested(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "admin/index", "gophers") }) @@ -240,7 +240,7 @@ func TestHTMLBadPath(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) @@ -260,7 +260,7 @@ func TestHTMLDelimiters(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "delims", "gophers") }) @@ -281,7 +281,7 @@ func TestHTMLDefaultCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) @@ -306,7 +306,7 @@ func TestHTMLOverrideLayout(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "content", "gophers", HTMLOptions{ Layout: "another_layout", }) @@ -328,7 +328,7 @@ func TestHTMLNoRace(t *testing.T) { Directory: "testdata/basic", }) - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err := render.HTML(w, http.StatusOK, "hello", "gophers") expectNil(t, err) }) @@ -373,7 +373,7 @@ func TestHTMLLoadFromAssets(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "test", "gophers", HTMLOptions{ Layout: "layout", }) @@ -416,7 +416,7 @@ func TestHTMLDisabledCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "hello", "gophers") }) @@ -440,7 +440,7 @@ func TestHTMLTemplateOptionDefault(t *testing.T) { // Template expects "world" key. templateData := map[string]int{"missing-key-here": 100} - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "map", templateData) }) @@ -465,7 +465,7 @@ func TestHTMLTemplateOptionZero(t *testing.T) { // Template expects "world" key. templateData := map[string]int{"missing-key-here": 100} - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "map", templateData) }) @@ -490,7 +490,7 @@ func TestHTMLTemplateOptionError(t *testing.T) { // Template expects "world" key. templateData := map[string]string{"missing-key-here": "gophers"} - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.HTML(w, http.StatusOK, "map", templateData) }) diff --git a/render_json_test.go b/render_json_test.go index 35b74de..26edc58 100644 --- a/render_json_test.go +++ b/render_json_test.go @@ -15,22 +15,18 @@ type Greeting struct { } type TestEncoder struct { - JSONEncoder w io.Writer } -func (e TestEncoder) NewEncoder(w io.Writer) JSONEncoder { - return TestEncoder{w: w} -} +func (e TestEncoder) Encode(_ interface{}) error { + _, _ = e.w.Write([]byte(e.String())) -func (e TestEncoder) Encode(v interface{}) error { - e.w.Write([]byte(e.String())) return nil } -func (e TestEncoder) SetEscapeHTML(on bool) {} +func (e TestEncoder) SetEscapeHTML(_ bool) {} -func (e TestEncoder) SetIndent(prefix, indent string) {} +func (e TestEncoder) SetIndent(_, _ string) {} func (e TestEncoder) String() string { return "{\"one\":\"world\",\"two\":\"hello\"}" @@ -41,7 +37,7 @@ func TestJSONBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 299, Greeting{"hello", "world"}) }) @@ -63,7 +59,7 @@ func TestJSONPrefix(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 300, Greeting{"hello", "world"}) }) @@ -84,7 +80,7 @@ func TestJSONIndented(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) }) @@ -105,7 +101,7 @@ func TestJSONConsumeIndented(t *testing.T) { var renErr error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { renErr = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) }) @@ -126,7 +122,7 @@ func TestJSONWithError(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 299, math.NaN()) }) @@ -145,7 +141,7 @@ func TestJSONWithOutUnEscapeHTML(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"test&test", "
test&test
"}) }) @@ -164,7 +160,7 @@ func TestJSONWithUnEscapeHTML(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"test&test", "
test&test
"}) }) @@ -183,7 +179,7 @@ func TestJSONStream(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 299, Greeting{"hello", "world"}) }) @@ -206,7 +202,7 @@ func TestJSONStreamPrefix(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 300, Greeting{"hello", "world"}) }) @@ -227,7 +223,7 @@ func TestJSONStreamWithError(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 299, math.NaN()) }) @@ -252,7 +248,7 @@ func TestJSONStreamWithOutUnEscapeHTML(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"test&test", "
test&test
"}) }) @@ -272,7 +268,7 @@ func TestJSONStreamWithUnEscapeHTML(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"test&test", "
test&test
"}) }) @@ -293,7 +289,7 @@ func TestJSONStreamIndented(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) }) @@ -314,7 +310,7 @@ func TestJSONCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 300, Greeting{"hello", "world"}) }) @@ -335,7 +331,7 @@ func TestJSONCustomContentType(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) }) @@ -356,7 +352,7 @@ func TestJSONDisabledCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, http.StatusOK, Greeting{"hello", "world"}) }) @@ -372,12 +368,14 @@ func TestJSONDisabledCharset(t *testing.T) { func TestJSONEncoder(t *testing.T) { render := New(Options{ - JSONEncoder: TestEncoder{}.NewEncoder, + JSONEncoder: func(w io.Writer) JSONEncoder { + return TestEncoder{w: w} + }, }) var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 299, Greeting{"hello", "world"}) }) @@ -393,13 +391,15 @@ func TestJSONEncoder(t *testing.T) { func TestJSONEncoderStream(t *testing.T) { render := New(Options{ - JSONEncoder: TestEncoder{}.NewEncoder, + JSONEncoder: func(w io.Writer) JSONEncoder { + return TestEncoder{w: w} + }, StreamingJSON: true, }) var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSON(w, 299, Greeting{"hello", "world"}) }) diff --git a/render_jsonp_test.go b/render_jsonp_test.go index 78cdfd9..8017420 100644 --- a/render_jsonp_test.go +++ b/render_jsonp_test.go @@ -17,7 +17,7 @@ func TestJSONPBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, 299, "helloCallback", GreetingP{"hello", "world"}) }) @@ -38,7 +38,7 @@ func TestJSONPRenderIndented(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) }) @@ -57,7 +57,7 @@ func TestJSONPWithError(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, 299, "helloCallback", math.NaN()) }) @@ -76,7 +76,7 @@ func TestJSONPCustomContentType(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) }) @@ -97,7 +97,7 @@ func TestJSONPDisabledCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) }) diff --git a/render_test.go b/render_test.go index 5c11fdd..9d303ce 100644 --- a/render_test.go +++ b/render_test.go @@ -45,7 +45,7 @@ func TestLockConfig(t *testing.T) { func BenchmarkNormalJSON(b *testing.B) { render := New() - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _ = render.JSON(w, 200, Greeting{"hello", "world"}) }) @@ -62,7 +62,7 @@ func BenchmarkStreamingJSON(b *testing.B) { StreamingJSON: true, }) - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _ = render.JSON(w, 200, Greeting{"hello", "world"}) }) @@ -79,7 +79,7 @@ func BenchmarkHTML(b *testing.B) { Directory: "testdata/basic", }) - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _ = render.HTML(w, http.StatusOK, "hello", "gophers") }) req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil) diff --git a/render_text_test.go b/render_text_test.go index bc3c689..146913b 100644 --- a/render_text_test.go +++ b/render_text_test.go @@ -13,7 +13,7 @@ func TestTextBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.Text(w, 299, "Hello Text!") }) @@ -34,7 +34,7 @@ func TestTextCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.Text(w, 299, "Hello Text!") }) @@ -55,7 +55,7 @@ func TestTextSuppliedCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set(ContentType, "text/css") err = render.Text(w, 200, "html{color:red}") }) @@ -77,7 +77,7 @@ func TestTextCustomContentType(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.Text(w, http.StatusOK, "Hello Text!") }) @@ -98,7 +98,7 @@ func TestTextDisabledCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.Text(w, http.StatusOK, "Hello Text!") }) diff --git a/render_xml_test.go b/render_xml_test.go index 76c9fe1..1fe9a87 100644 --- a/render_xml_test.go +++ b/render_xml_test.go @@ -20,7 +20,7 @@ func TestXMLBasic(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.XML(w, 299, GreetingXML{One: "hello", Two: "world"}) }) @@ -42,7 +42,7 @@ func TestXMLPrefix(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.XML(w, 300, GreetingXML{One: "hello", Two: "world"}) }) @@ -63,7 +63,7 @@ func TestXMLIndented(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.XML(w, http.StatusOK, GreetingXML{One: "hello", Two: "world"}) }) @@ -84,7 +84,7 @@ func TestXMLWithError(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.XML(w, 299, map[string]string{"foo": "bar"}) }) @@ -103,7 +103,7 @@ func TestXMLCustomContentType(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.XML(w, http.StatusOK, GreetingXML{One: "hello", Two: "world"}) }) @@ -124,7 +124,7 @@ func TestXMLDisabledCharset(t *testing.T) { var err error - h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.XML(w, http.StatusOK, GreetingXML{One: "hello", Two: "world"}) })