From e9ef439c7a85b60698e151efd098bce405460c2d Mon Sep 17 00:00:00 2001 From: niko0xdev Date: Sat, 20 Jan 2024 11:11:07 +0700 Subject: [PATCH] add jsonDump helper func --- codegen/templates/templates.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index 0685bc7..7f81b98 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -2,6 +2,7 @@ package templates import ( "bytes" + "encoding/json" "fmt" "go/types" "io/fs" @@ -201,6 +202,7 @@ func Funcs() template.FuncMap { "quote": strconv.Quote, "rawQuote": rawQuote, "dump": Dump, + "jsonDump": jsonDump, "ref": ref, "ts": TypeIdentifier, "call": Call, @@ -620,6 +622,19 @@ func Dump(val interface{}) string { } } +func jsonDump(v interface{}) string { + // Convert the value to JSON + data, err := json.MarshalIndent(v, "", " ") + if err != nil { + fmt.Println("Error marshaling to JSON:", err) + + return "" + } + + // Print the indented JSON + return string(data) +} + func prefixLines(prefix, s string) string { return prefix + strings.ReplaceAll(s, "\n", "\n"+prefix) }