Some checks failed
CI / Lint (push) Successful in -27m59s
CI / Test (1.25) (push) Successful in -27m46s
CI / Test (1.24) (push) Failing after 59s
CI / Build (push) Successful in -28m14s
Integration Tests / Integration Tests (push) Failing after -28m16s
Release / Build and Release (push) Successful in 1m1s
feat(templ): ✨ added templ to command line that reads go template and outputs code Reviewed-on: #1 Co-authored-by: Hein <hein.puth@gmail.com> Co-committed-by: Hein <hein.puth@gmail.com>
158 lines
3.5 KiB
Go
158 lines
3.5 KiB
Go
package template
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// ToJSON converts a value to JSON string
|
|
// Usage: {{ .Database | toJSON }}
|
|
func ToJSON(v interface{}) string {
|
|
data, err := json.Marshal(v)
|
|
if err != nil {
|
|
return fmt.Sprintf("{\"error\": \"failed to marshal: %v\"}", err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
// ToJSONPretty converts a value to pretty-printed JSON string
|
|
// Usage: {{ .Database | toJSONPretty " " }}
|
|
func ToJSONPretty(v interface{}, indent string) string {
|
|
data, err := json.MarshalIndent(v, "", indent)
|
|
if err != nil {
|
|
return fmt.Sprintf("{\"error\": \"failed to marshal: %v\"}", err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
// ToYAML converts a value to YAML string
|
|
// Usage: {{ .Database | toYAML }}
|
|
func ToYAML(v interface{}) string {
|
|
data, err := yaml.Marshal(v)
|
|
if err != nil {
|
|
return fmt.Sprintf("error: failed to marshal: %v", err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
// Indent indents each line of a string by the specified number of spaces
|
|
// Usage: {{ .Column.Description | indent 4 }}
|
|
func Indent(s string, spaces int) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
|
|
prefix := strings.Repeat(" ", spaces)
|
|
lines := strings.Split(s, "\n")
|
|
for i, line := range lines {
|
|
if line != "" {
|
|
lines[i] = prefix + line
|
|
}
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
// IndentWith indents each line of a string with a custom prefix
|
|
// Usage: {{ .Column.Description | indentWith " " }}
|
|
func IndentWith(s string, prefix string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
|
|
lines := strings.Split(s, "\n")
|
|
for i, line := range lines {
|
|
if line != "" {
|
|
lines[i] = prefix + line
|
|
}
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
// Escape escapes special characters in a string for use in code
|
|
// Usage: {{ .Column.Default | escape }}
|
|
func Escape(s string) string {
|
|
s = strings.ReplaceAll(s, "\\", "\\\\")
|
|
s = strings.ReplaceAll(s, "\"", "\\\"")
|
|
s = strings.ReplaceAll(s, "\n", "\\n")
|
|
s = strings.ReplaceAll(s, "\r", "\\r")
|
|
s = strings.ReplaceAll(s, "\t", "\\t")
|
|
return s
|
|
}
|
|
|
|
// EscapeQuotes escapes only quote characters
|
|
// Usage: {{ .Column.Comment | escapeQuotes }}
|
|
func EscapeQuotes(s string) string {
|
|
s = strings.ReplaceAll(s, "\"", "\\\"")
|
|
s = strings.ReplaceAll(s, "'", "\\'")
|
|
return s
|
|
}
|
|
|
|
// Comment adds comment prefix to a string
|
|
// Supports: "//" (Go, C++, etc.), "#" (Python, shell), "--" (SQL), "/* */" (block)
|
|
// Usage: {{ .Table.Description | comment "//" }}
|
|
func Comment(s string, style string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
|
|
lines := strings.Split(s, "\n")
|
|
|
|
switch style {
|
|
case "//":
|
|
for i, line := range lines {
|
|
lines[i] = "// " + line
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
|
|
case "#":
|
|
for i, line := range lines {
|
|
lines[i] = "# " + line
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
|
|
case "--":
|
|
for i, line := range lines {
|
|
lines[i] = "-- " + line
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
|
|
case "/* */", "/**/":
|
|
if len(lines) == 1 {
|
|
return "/* " + lines[0] + " */"
|
|
}
|
|
result := "/*\n"
|
|
for _, line := range lines {
|
|
result += " * " + line + "\n"
|
|
}
|
|
result += " */"
|
|
return result
|
|
|
|
default:
|
|
// Default to // style
|
|
for i, line := range lines {
|
|
lines[i] = "// " + line
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
}
|
|
|
|
// QuoteString adds quotes around a string
|
|
// Usage: {{ .Column.Default | quoteString }}
|
|
func QuoteString(s string) string {
|
|
return "\"" + s + "\""
|
|
}
|
|
|
|
// UnquoteString removes quotes from a string
|
|
// Usage: {{ .Value | unquoteString }}
|
|
func UnquoteString(s string) string {
|
|
if len(s) >= 2 {
|
|
if (s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'') {
|
|
return s[1 : len(s)-1]
|
|
}
|
|
}
|
|
return s
|
|
}
|