Skip to content

Latest commit

 

History

History
194 lines (143 loc) · 5.55 KB

README.md

File metadata and controls

194 lines (143 loc) · 5.55 KB

Gomplate

Flanksource Gomplate is a fork of hairyhenderson/gomplate that provides a unified templating library supporting multiple expression languages.

Features

  • Go Text/Template – Full Go text/template support with an extended function library from gomplate (base64, collections, crypto, data formats, filepath, math, random, regexp, strings, time, and more)
  • CEL (Common Expression Language) – CEL support with:
    • Standard CEL operators and built-ins
    • cel-go extensions (strings, encoders, lists, math, sets)
    • Kubernetes-specific helpers (k8s.*); health, status, neat and topology are opt-in
    • AWS helpers (aws.*)
    • Many gomplate functions remapped into CEL (base64, math, random, regexp, filepath, crypto, sets, etc.)
    • Optional-type support (obj.?field.orValue("default"))
  • JavaScript (otto) – Lightweight JS evaluation via otto, opt-in
  • Struct templating – Walk Go structs/maps and apply templates to string fields in-place

Optional function groups

The Kubernetes health, status, neat and topology functions and JavaScript templates pull in Kubernetes, Lua and otto dependencies, so the core package does not import them. A program that needs them imports github.com/flanksource/gomplate/v4/kubernetes and github.com/flanksource/gomplate/v4/js and registers them once at startup with gomplate.Register(kubernetes.Extension(), js.Extension()).

Without kubernetes.Extension(), CEL expressions such as k8s.isHealthy(pod) fail with an undeclared reference and Go templates fail to parse isHealthy. Without js.Extension(), Template.Javascript returns an error. k8s.cpuAsMillicores, k8s.memoryAsBytes and the list, URL and regex functions stay in the core.

Quick Reference

Language Example Documentation
Go Template {{ .name | strings.ToUpper }} GO_TEMPLATE.md
CEL name.upperAscii() CEL.md

CEL Quick Reference

# Arithmetic
2 + 3           // 5
"hello" + " world"   // "hello world"
[1,2] + [3,4]   // [1,2,3,4]

# Comparison / Logic
x > 5 && y != null
true ? "yes" : "no"

# String methods
"hello world".upperAscii()        // "HELLO WORLD"
"hello world".split(" ")          // ["hello", "world"]
"hello".startsWith("he")          // true
"hello world".contains("world")   // true

# Collections
[1,2,3].filter(e, e > 1)          // [2, 3]
[1,2,3].map(e, e * 2)             // [2, 4, 6]
[1,2,3].all(e, e > 0)             // true
{"a":1,"b":2}.keys()              // ["a","b"]

# Optional / null-safety
obj.?field.orValue("default")
obj.?a.?b.orValue("fallback")

# Kubernetes (k8s.isHealthy needs kubernetes.Extension())
k8s.isHealthy(pod)
k8s.cpuAsMillicores("500m")       // 500
k8s.memoryAsBytes("1Gi")

# Math
math.Add([1,2,3])                 // 6
math.greatest([1,2,3])            // 3

# Time
time.Now()
time.Since(timestamp("2023-01-01T00:00:00Z"))

See CEL.md for full reference.

Go Template Quick Reference

# Variables and output
{{ .name }}
{{ $x := "hello" }}{{ $x | strings.ToUpper }}

# Control flow
{{ if eq .status "ok" }}OK{{ else }}FAIL{{ end }}
{{ range .items }}{{ . }}{{ end }}

# String functions
{{ "hello world" | strings.ToUpper }}        // HELLO WORLD
{{ "hello world" | strings.Split " " }}      // [hello world]
{{ "hello" | strings.Repeat 3 }}             // hellohellohello

# Data
{{ '{"key":"val"}' | json }}                 // map access
{{ .obj | toJSON }}
{{ .obj | toYAML }}

# Collections
{{ coll.Dict "a" 1 "b" 2 | toJSON }}         // {"a":1,"b":2}
{{ slice 1 2 3 | coll.Reverse | toJSON }}     // [3,2,1]
{{ coll.Sort (slice "b" "a" "c") | toJSON }}  // ["a","b","c"]

# Math
{{ math.Add 1 2 3 }}                          // 6
{{ math.Max 1 5 3 }}                          // 5

# base64
{{ base64.Encode "hello" }}                   // aGVsbG8=
{{ "aGVsbG8=" | base64.Decode }}              // hello

# Crypto
{{ crypto.SHA256 "hello" }}

# Delimiters (change if conflict with Helm)
# gotemplate: left-delim=$[[ right-delim=]]
$[[ .name ]]

See GO_TEMPLATE.md for full reference.

Installation

import "github.com/flanksource/gomplate/v4"

Usage

Go Template

result, err := gomplate.RunTemplate(map[string]any{
    "name": "world",
}, gomplate.Template{
    Template: `Hello, {{ .name }}!`,
})

CEL

result, err := gomplate.RunTemplate(map[string]any{
    "name": "world",
}, gomplate.Template{
    Expression: `"Hello, " + name + "!"`,
})

Struct Templating

type Config struct {
    Message string
    Count   int
}

cfg := Config{Message: "Hello, {{ .name }}!"}

err := gomplate.Walk(map[string]any{"name": "world"}, &cfg)
// cfg.Message == "Hello, world!"

Playground

make build creates bin/ceval with the playground UI and Go WASM evaluator embedded. Run ./bin/ceval serve and open http://127.0.0.1:8321; select Server or Browser (WASM) evaluation. See the playground guide for development, hosting and binary releases.

CEL eval helper

A small helper command is available at cmd/ceval to evaluate CEL expressions against an input YAML or JSON file.

Run it with:

go run ./cmd/ceval -f env.yaml -e "labels['a/b/c']"

Example input file:

labels:
  a/b/c: d

Output:

d