templates: linting: fix "error return value is not checked (errchkjson)

The linter is correct; given that these functions do not allow for an error
to be returned, we panic. Alternatively, we could return the error string
as output, or add a `//nolint:errchkjson` comment.

    templates/templates.go:17:3: Error return value of `(*encoding/json.Encoder).Encode` is not checked: unsafe type `interface{}` found (errchkjson)
            enc.Encode(v)
            ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-09-02 23:49:14 +02:00
parent 4e2477f85f
commit 30d36e977e
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 6 additions and 1 deletions

View File

@ -5,6 +5,7 @@ linters:
- dogsled - dogsled
- dupword # Detects duplicate words. - dupword # Detects duplicate words.
- durationcheck - durationcheck
- errchkjson
- exportloopref # Detects pointers to enclosing loop variables. - exportloopref # Detects pointers to enclosing loop variables.
- gocritic # Metalinter; detects bugs, performance, and styling issues. - gocritic # Metalinter; detects bugs, performance, and styling issues.
- gocyclo - gocyclo

View File

@ -14,7 +14,11 @@ var basicFunctions = template.FuncMap{
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
enc := json.NewEncoder(buf) enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false) enc.SetEscapeHTML(false)
enc.Encode(v) err := enc.Encode(v)
if err != nil {
panic(err)
}
// Remove the trailing new line added by the encoder // Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String()) return strings.TrimSpace(buf.String())
}, },