template

package
v0.6.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package template provides mache's schema template rendering functions.

This package is pure Go with no CGO dependencies. It was extracted from internal/ingest to break the transitive dependency chain:

graph.SQLiteGraph → ingest.RenderTemplate → internal/lang → tree-sitter (CGO)

After extraction the chain is:

graph.SQLiteGraph → template.RenderTemplate (pure Go)

Index

Constants

This section is empty.

Variables

View Source
var Funcs = template.FuncMap{
	"json": func(v any) string {
		b, err := json.Marshal(v)
		if err != nil {
			return fmt.Sprintf("<json error: %v>", err)
		}
		return string(b)
	},
	"first": func(v any) any {
		switch s := v.(type) {
		case []any:
			if len(s) > 0 {
				return s[0]
			}
		}
		return nil
	},

	"unquote": func(s string) string {
		if u, err := strconv.Unquote(s); err == nil {
			return u
		}
		return s
	},

	"slice": func(s string, start, end int) string {
		if start < 0 {
			start = 0
		}
		if end > len(s) {
			end = len(s)
		}
		if start >= end {
			return ""
		}
		return s[start:end]
	},

	"replace": func(s, old, new string) string {
		return strings.ReplaceAll(s, old, new)
	},

	"lower": func(s string) string {
		return strings.ToLower(s)
	},

	"upper": func(s string) string {
		return strings.ToUpper(s)
	},

	"title": cases.Title(language.Und).String,

	"split": func(s, sep string) []string {
		return strings.Split(s, sep)
	},

	"join": func(sep string, parts any) string {
		switch v := parts.(type) {
		case []string:
			return strings.Join(v, sep)
		case []any:
			strs := make([]string, len(v))
			for i, elem := range v {
				strs[i] = fmt.Sprintf("%v", elem)
			}
			return strings.Join(strs, sep)
		default:
			return fmt.Sprintf("%v", parts)
		}
	},

	"hasPrefix": strings.HasPrefix,

	"hasSuffix": strings.HasSuffix,

	"trimPrefix": strings.TrimPrefix,

	"trimSuffix": strings.TrimSuffix,

	"dict": func(pairs ...any) (map[string]any, error) {
		if len(pairs)%2 != 0 {
			return nil, fmt.Errorf("dict requires even number of args, got %d", len(pairs))
		}
		m := make(map[string]any, len(pairs)/2)
		for i := 0; i < len(pairs); i += 2 {
			m[fmt.Sprint(pairs[i])] = pairs[i+1]
		}
		return m, nil
	},

	"lookup": func(val any, pairs ...any) any {
		s := fmt.Sprint(val)
		for i := 0; i+1 < len(pairs); i += 2 {
			if fmt.Sprint(pairs[i]) == s {
				return pairs[i+1]
			}
		}
		if len(pairs)%2 == 1 {
			return pairs[len(pairs)-1]
		}
		return ""
	},

	"default": func(val, fallback any) any {
		if val == nil {
			return fallback
		}
		if s, ok := val.(string); ok && s == "" {
			return fallback
		}
		return val
	},

	"dig": func(path string, obj any) string {
		parts := strings.Split(path, ".")
		current := obj
		for _, part := range parts {
			if current == nil || part == "" {
				return ""
			}
			switch v := current.(type) {
			case map[string]any:
				val, ok := v[part]
				if !ok {
					return ""
				}
				current = val
			case []any:
				idx := 0
				for _, c := range part {
					if c < '0' || c > '9' {
						return ""
					}
					idx = idx*10 + int(c-'0')
				}
				if idx >= len(v) {
					return ""
				}
				current = v[idx]
			default:
				return ""
			}
		}
		if current == nil {
			return ""
		}
		return fmt.Sprint(current)
	},
}

Funcs is the standard set of template functions available in all mache schemas.

Functions

func Render

func Render(tmpl string, values map[string]any) (string, error)

Render renders a Go text/template with the standard mache template functions. Parsed templates are cached — repeated calls with the same template string skip parsing.

func RenderWithFuncs

func RenderWithFuncs(tmpl string, values map[string]any, extraFuncs template.FuncMap, c *sync.Map) (string, error)

RenderWithFuncs renders a Go text/template with the standard mache template functions plus additional per-engine functions (e.g., {{diagram}}). Templates are cached in the provided cache; the caller must ensure the extraFuncs map is stable for the cache's lifetime.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL