From f7548a2816d3b093855bd3bcb707200dd5723eab Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Tue, 15 Mar 2022 20:22:04 +0100 Subject: [PATCH] Upgrade to Go 1.18 --- .github/workflows/go.yml | 4 ++-- api/views.go | 4 ++-- config/components.go | 2 +- go.mod | 3 ++- go.sum | 2 ++ rendering/html.go | 8 ++++---- rendering/render_to_reveal.go | 6 +++--- rendering/templates.go | 18 ++++++------------ 8 files changed, 22 insertions(+), 25 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 87bf3a2..8e2c9f1 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -19,10 +19,10 @@ jobs: name: Build runs-on: ubuntu-latest steps: - - name: Set up Go 1.17 + - name: Set up Go 1.18 uses: actions/setup-go@v3 with: - go-version: '^1.17' + go-version: '^1.18' id: go - name: Login to GitHub Container Registry diff --git a/api/views.go b/api/views.go index 230dbe8..6817ffb 100644 --- a/api/views.go +++ b/api/views.go @@ -25,7 +25,7 @@ func init() { if t, err := template. New("index"). Funcs(sprig.FuncMap()). - Funcs(map[string]interface{}{ + Funcs(map[string]any{ "fileId": func(fileName string) string { h := fnv.New32a() return hex.EncodeToString(h.Sum([]byte(path.Base(fileName)))) @@ -60,7 +60,7 @@ func RegisterViews(router *httprouter.Router, logger *log.Logger, wdfs fs.FS, md func (p *Views) IndexPage(writer http.ResponseWriter, _ *http.Request, _ httprouter.Params) { writer.Header().Set("Content-Type", "text/html") - if err := indexTmpl.ExecuteTemplate(writer, "index.gohtml", map[string]interface{}{ + if err := indexTmpl.ExecuteTemplate(writer, "index.gohtml", map[string]any{ "Reveal": p.cfg.Reveal, "Rendering": p.cfg.Rendering, }); err != nil { diff --git a/config/components.go b/config/components.go index 3ee50ef..10c7709 100644 --- a/config/components.go +++ b/config/components.go @@ -5,7 +5,7 @@ const ( defaultHeight uint = 700 ) -var defaults = map[string]interface{}{ +var defaults = map[string]any{ "mermaid.theme": "forest", "theme": "beige", "width": defaultWidth, diff --git a/go.mod b/go.mod index 652bed6..25ddbbc 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/baez90/goveal -go 1.17 +go 1.18 require ( github.com/Masterminds/sprig/v3 v3.2.2 @@ -12,6 +12,7 @@ require ( github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.4.0 github.com/spf13/viper v1.10.1 + github.com/valyala/bytebufferpool v1.0.0 go.uber.org/multierr v1.8.0 ) diff --git a/go.sum b/go.sum index c7139fe..3279d05 100644 --- a/go.sum +++ b/go.sum @@ -201,6 +201,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/rendering/html.go b/rendering/html.go index 66ce628..b49ec69 100644 --- a/rendering/html.go +++ b/rendering/html.go @@ -1,12 +1,12 @@ package rendering import ( - "bytes" "fmt" "html/template" "regexp" "github.com/gomarkdown/markdown/parser" + "github.com/valyala/bytebufferpool" "github.com/baez90/goveal/config" ) @@ -24,17 +24,17 @@ func ToHTML(markdown string, renderCfg config.Rendering) (rendered []byte, err e return nil, err } - buf := templateRenderBufferPool.Get().(*bytes.Buffer) + buf := bytebufferpool.Get() defer func() { buf.Reset() - templateRenderBufferPool.Put(buf) + bytebufferpool.Put(buf) }() for idx := range slides { if rendered, err := slides[idx].ToHTML(); err != nil { return nil, err } else { - buf.WriteString(string(rendered)) + _, _ = buf.WriteString(string(rendered)) } } diff --git a/rendering/render_to_reveal.go b/rendering/render_to_reveal.go index 221b58d..fe035aa 100644 --- a/rendering/render_to_reveal.go +++ b/rendering/render_to_reveal.go @@ -78,7 +78,7 @@ func (r *RevealRenderer) handleListItem(w io.Writer, listItem *ast.ListItem) (as return ast.GoToNext, false } - data := map[string]interface{}{ + data := map[string]any{ "Attributes": getAttributesFromChildSpan(p), } @@ -102,7 +102,7 @@ func (r *RevealRenderer) handleImage(w io.Writer, img *ast.Image) (ast.WalkStatu } } - data := map[string]interface{}{ + data := map[string]any{ "ID": hex.EncodeToString(r.Hash.Sum([]byte(path.Base(string(img.Destination))))), "Attributes": getAttributesFromChildSpan(img.GetParent()), "ImageSource": string(img.Destination), @@ -154,7 +154,7 @@ func extractElementAttributes(htmlSpan *ast.HTMLSpan) (attrs []template.HTMLAttr } func renderCodeTemplate(templateName string, codeBlock *ast.CodeBlock) (output []byte, err error) { - data := map[string]interface{}{ + data := map[string]any{ //nolint:gosec // need to embed the code in original format without escaping "Code": template.HTML(codeBlock.Literal), "LineNumbers": lineNumbers(codeBlock.Attribute), diff --git a/rendering/templates.go b/rendering/templates.go index 4c56c0a..6c52758 100644 --- a/rendering/templates.go +++ b/rendering/templates.go @@ -1,29 +1,23 @@ package rendering import ( - "bytes" "embed" "hash/fnv" "html/template" - "sync" "github.com/Masterminds/sprig/v3" "github.com/gomarkdown/markdown" mdhtml "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/parser" + "github.com/valyala/bytebufferpool" "github.com/baez90/goveal/rendering/emoji" ) var ( //go:embed templates/*.gohtml - templatesFS embed.FS - templates *template.Template - templateRenderBufferPool = &sync.Pool{ - New: func() interface{} { - return new(bytes.Buffer) - }, - } + templatesFS embed.FS + templates *template.Template ) func init() { @@ -54,11 +48,11 @@ func init() { } } -func renderTemplate(templateName string, data interface{}) (output []byte, err error) { - buffer := templateRenderBufferPool.Get().(*bytes.Buffer) +func renderTemplate(templateName string, data any) (output []byte, err error) { + buffer := bytebufferpool.Get() defer func() { buffer.Reset() - templateRenderBufferPool.Put(buffer) + bytebufferpool.Put(buffer) }() err = templates.ExecuteTemplate(buffer, templateName, data) -- 2.45.2