From 30ef6be89087037138daef66a1e1c785382adc9f Mon Sep 17 00:00:00 2001 From: ajinkya Date: Mon, 19 Aug 2024 15:52:51 +0530 Subject: [PATCH 1/3] use json formatter for slice and map --- mustache.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mustache.go b/mustache.go index e268b80..9b51cd1 100644 --- a/mustache.go +++ b/mustache.go @@ -2,6 +2,7 @@ package mustache import ( "bytes" + "encoding/json" "fmt" "html/template" "io" @@ -697,7 +698,16 @@ func (tmpl *Template) renderElement(element interface{}, contextChain []interfac if val.IsValid() { if elem.raw { - fmt.Fprint(buf, val.Interface()) + switch reflect.TypeOf(val.Interface()).Kind() { + case reflect.Slice, reflect.Map: + marshalledJson, err := json.Marshal(val.Interface()) + if err != nil { + return err + } + buf.Write(marshalledJson) + default: + fmt.Fprint(buf, val.Interface()) + } } else { s := fmt.Sprint(val.Interface()) _, _ = buf.Write([]byte(tmpl.escape(s))) From 62433637acaeb68ff3d609dd02a1c773a5b57c2d Mon Sep 17 00:00:00 2001 From: ajinkya Date: Mon, 19 Aug 2024 15:57:11 +0530 Subject: [PATCH 2/3] use json formatter for array as well --- mustache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mustache.go b/mustache.go index 9b51cd1..533d533 100644 --- a/mustache.go +++ b/mustache.go @@ -699,7 +699,7 @@ func (tmpl *Template) renderElement(element interface{}, contextChain []interfac if val.IsValid() { if elem.raw { switch reflect.TypeOf(val.Interface()).Kind() { - case reflect.Slice, reflect.Map: + case reflect.Slice, reflect.Map, reflect.Array: marshalledJson, err := json.Marshal(val.Interface()) if err != nil { return err From 7aaf5466f1dfe0a9bcfc7a178f6faeee13df2456 Mon Sep 17 00:00:00 2001 From: ajinkya Date: Mon, 19 Aug 2024 16:07:15 +0530 Subject: [PATCH 3/3] update module name, go version to v1.21 --- README.md | 12 ++++++------ cmd/mustache/main.go | 2 +- go.mod | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 36bd899..e911acd 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # Mustache Template Engine for Go [![Build Status](https://img.shields.io/travis/cbroglie/mustache.svg)](https://travis-ci.org/cbroglie/mustache) -[![Go Doc](https://godoc.org/github.com/cbroglie/mustache?status.svg)](https://godoc.org/github.com/cbroglie/mustache) -[![Go Report Card](https://goreportcard.com/badge/github.com/cbroglie/mustache)](https://goreportcard.com/report/github.com/cbroglie/mustache) +[![Go Doc](https://godoc.org/github.com/verloop/mustache?status.svg)](https://godoc.org/github.com/verloop/mustache) +[![Go Report Card](https://goreportcard.com/badge/github.com/verloop/mustache)](https://goreportcard.com/report/github.com/verloop/mustache) [![codecov](https://codecov.io/gh/cbroglie/mustache/branch/master/graph/badge.svg)](https://codecov.io/gh/cbroglie/mustache) -[![Downloads](https://img.shields.io/github/downloads/cbroglie/mustache/latest/total.svg)](https://github.com/cbroglie/mustache/releases) -[![Latest release](https://img.shields.io/github/release/cbroglie/mustache.svg)](https://github.com/cbroglie/mustache/releases) +[![Downloads](https://img.shields.io/github/downloads/cbroglie/mustache/latest/total.svg)](https://github.com/verloop/mustache/releases) +[![Latest release](https://img.shields.io/github/release/cbroglie/mustache.svg)](https://github.com/verloop/mustache/releases) logo @@ -24,7 +24,7 @@ I forked [hoisie/mustache](https://github.com/hoisie/mustache) because it does n ## CLI Overview ```bash -➜ ~ go install github.com/cbroglie/mustache/cmd/mustache@latest +➜ ~ go install github.com/verloop/mustache/cmd/mustache@latest ➜ ~ mustache Usage: mustache [data] template [flags] @@ -64,7 +64,7 @@ Also check out some [example mustache files](http://github.com/mustache/mustache ## Installation -To install the CLI, run `go install github.com/cbroglie/mustache/cmd/mustache@latest`. To use it in a program, run `go get github.com/cbroglie/mustache` and use `import "github.com/cbroglie/mustache"`. +To install the CLI, run `go install github.com/verloop/mustache/cmd/mustache@latest`. To use it in a program, run `go get github.com/verloop/mustache` and use `import "github.com/verloop/mustache"`. ---- diff --git a/cmd/mustache/main.go b/cmd/mustache/main.go index 325c2dd..917849f 100644 --- a/cmd/mustache/main.go +++ b/cmd/mustache/main.go @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" "gopkg.in/yaml.v2" - "github.com/cbroglie/mustache" + "github.com/verloop/mustache" ) var rootCmd = &cobra.Command{ diff --git a/go.mod b/go.mod index 93eb093..67c62d4 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module github.com/cbroglie/mustache +module github.com/verloop/mustache -go 1.20 +go 1.21 require ( github.com/spf13/cobra v1.7.0