Skip to content

Commit

Permalink
add jsonDump helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
niko0xdev committed Jan 20, 2024
1 parent 579d10d commit e9ef439
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package templates

import (
"bytes"
"encoding/json"
"fmt"
"go/types"
"io/fs"
Expand Down Expand Up @@ -201,6 +202,7 @@ func Funcs() template.FuncMap {
"quote": strconv.Quote,
"rawQuote": rawQuote,
"dump": Dump,
"jsonDump": jsonDump,
"ref": ref,
"ts": TypeIdentifier,
"call": Call,
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit e9ef439

Please sign in to comment.