Skip to content

Commit

Permalink
feat: text/mustacheutil: init
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Jan 12, 2025
1 parent 88b96b4 commit dd76959
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/aws/aws-sdk-go v1.55.5
github.com/buaazp/fasthttprouter v0.1.1
github.com/buger/jsonparser v1.1.1
github.com/cbroglie/mustache v1.4.0
github.com/derekstavis/go-qs v0.0.0-20180720192143-9eef69e6c4e7
github.com/emersion/go-imap v1.2.1
github.com/go-logfmt/logfmt v0.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMU
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
github.com/cbroglie/mustache v1.4.0 h1:Azg0dVhxTml5me+7PsZ7WPrQq1Gkf3WApcHMjMprYoU=
github.com/cbroglie/mustache v1.4.0/go.mod h1:SS1FTIghy0sjse4DUVGV1k/40B1qE1XkD9DtDsHo9iM=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
Expand Down
46 changes: 46 additions & 0 deletions text/mustacheutil/mustacheutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mustacheutil

import (
"bytes"
"fmt"

"github.com/cbroglie/mustache"
)

type MustacheSet struct {
Filenames map[string]string
Templates map[string]*mustache.Template
}

func (ms *MustacheSet) ReadTemplates() error {
for key, filename := range ms.Filenames {
if filename == "" {
continue
}
if tmpl, err := mustache.ParseFile(filename); err != nil {
return err
} else {
if ms.Templates == nil {
ms.Templates = map[string]*mustache.Template{}
}
ms.Templates[key] = tmpl
}
}
return nil
}

func (ms *MustacheSet) RenderTemplate(key string, data map[string]string) (*bytes.Buffer, error) {
tmpl, ok := ms.Templates[key]
if !ok {
return nil, fmt.Errorf("template key not present for key (%s)", key)
} else if tmpl == nil {
return nil, fmt.Errorf("template is nil for key (%s)", key)
} else {
var buf bytes.Buffer
if err := tmpl.FRender(&buf, data); err != nil {
return nil, err
} else {
return &buf, nil
}
}
}

0 comments on commit dd76959

Please sign in to comment.