Skip to content

Commit

Permalink
Merge pull request #30 from tiendc/improve/use-go-template-lib-in-render
Browse files Browse the repository at this point in the history
Use text/template lib in render
  • Loading branch information
tiendc authored Oct 24, 2023
2 parents e524c8b + 9855ba8 commit 9203f4f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ func localizeViVn(k string, params ParameterMap) (string, error) {
if !ok {
return "", fmt.Errorf("%w: '%s'", errKeyNotFound, k)
}
return processParams(s, params), nil
return processTemplate(s, params)
}

func localizeEnUs(k string, params ParameterMap) (string, error) {
s, ok := mapLanguageEn[k]
if !ok {
return "", fmt.Errorf("%w: '%s'", errKeyNotFound, k)
}
return processParams(s, params), nil
return processTemplate(s, params)
}

func localizeFail(k string, params ParameterMap) (string, error) {
Expand Down
9 changes: 5 additions & 4 deletions errors_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (r *SimpleRenderer) renderCommonError(err error, params ParameterMap) strin

func (r *SimpleRenderer) localizeKey(key string, params ParameterMap) (string, error) {
if r.cfg.LocalizationFunc == nil {
return processParams(key, params), nil
return processTemplate(key, params)
}
msg, err := r.cfg.LocalizationFunc(key, params)
if err != nil {
Expand All @@ -263,8 +263,9 @@ func (r *SimpleRenderer) localizeKey(key string, params ParameterMap) (string, e

func (r *SimpleRenderer) localizeKeySkipError(key string, params ParameterMap) string {
s, err := r.localizeKey(key, params)
if err != nil {
s = key
if err == nil || r.cfg.LocalizationFunc == nil {
return s
}
return processParams(s, params)
s, _ = processTemplate(key, params)
return s
}
9 changes: 5 additions & 4 deletions errors_render_as_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (r *CSVRenderer) renderCommonError(err error, params ParameterMap) string {

func (r *CSVRenderer) localizeKey(key string, params ParameterMap) (string, error) {
if r.cfg.LocalizationFunc == nil {
return processParams(key, params), nil
return processTemplate(key, params)
}
msg, err := r.cfg.LocalizationFunc(key, params)
if err != nil {
Expand All @@ -337,10 +337,11 @@ func (r *CSVRenderer) localizeKey(key string, params ParameterMap) (string, erro

func (r *CSVRenderer) localizeKeySkipError(key string, params ParameterMap) string {
s, err := r.localizeKey(key, params)
if err != nil {
s = key
if err == nil || r.cfg.LocalizationFunc == nil {
return s
}
return processParams(s, params)
s, _ = processTemplate(key, params)
return s
}

func (r *CSVRenderer) estimateCSVBuffer(data [][]string) int {
Expand Down
26 changes: 18 additions & 8 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package csvlib

import (
"bytes"
"fmt"
"strings"
"text/template"

"github.com/hashicorp/go-multierror"
)

func validateHeader(header []string) error {
Expand All @@ -20,14 +24,20 @@ func validateHeader(header []string) error {
return nil
}

func processParams(s string, params ParameterMap) string {
if len(params) == 0 {
return s
func processTemplate(templ string, params ParameterMap) (detail string, retErr error) {
detail = templ
t, err := template.New("error").Parse(detail)
if err != nil {
retErr = multierror.Append(retErr, err)
return
}
for k, v := range params {
key := "{{." + k + "}}"
val := fmt.Sprintf("%v", v)
s = strings.ReplaceAll(s, key, val)

buf := bytes.NewBuffer(make([]byte, 0, 100)) // nolint: gomnd
err = t.Execute(buf, params)
if err != nil {
retErr = multierror.Append(retErr, err)
} else {
detail = buf.String()
}
return s
return
}

0 comments on commit 9203f4f

Please sign in to comment.