Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Backfill Sprig functions in Liquid templating #34

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
backfill sprig functions in liquid templating
maciaszczykm committed Jun 4, 2024
commit e6a4ce0b055b339bf645f6a00750ff29b552ed04
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE
.idea
23 changes: 8 additions & 15 deletions template/liquid.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
package template

import (
"strings"

"github.com/Masterminds/sprig/v3"
"github.com/osteele/liquid"
)

var (
liquidEngine = liquid.NewEngine()
sprigFunctions = map[string]string{
liquidEngine = liquid.NewEngine()
sprigFunctionNameMap = map[string]string{
"toJson": "to_json",
"fromJson": "from_json",
"b64enc": "b64enc",
"b64dec": "b64dec",
"semverCompare": "semver_compare",
"sha256sum": "sha26sum",
"quote": "quote",
"squote": "squote",
"replace": "replace",
"coalesce": "coalesce",
}
)

func init() {
fncs := sprig.TxtFuncMap()
for key, name := range sprigFunctions {

for name, fnc := range fncs {
liquidEngine.RegisterFilter(name, fnc)
}

for key, name := range sprigFunctionNameMap {
liquidEngine.RegisterFilter(name, fncs[key])
}
liquidEngine.RegisterFilter("indent", indent)
liquidEngine.RegisterFilter("nindent", nindent)
liquidEngine.RegisterFilter("replace", strings.ReplaceAll)

liquidEngine.RegisterFilter("default", dfault)
liquidEngine.RegisterFilter("ternary", ternary)
}

49 changes: 0 additions & 49 deletions template/utils.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,9 @@
package template

import (
"reflect"
"strings"
)

func indent(v string, spaces int) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.ReplaceAll(v, "\n", "\n"+pad)
}

func nindent(v string, spaces int) string {
return "\n" + indent(v, spaces)
}

func ternary(v bool, vt interface{}, vf interface{}) interface{} {
if v {
return vt
}

return vf
}

func dfault(v1, v2 interface{}) interface{} {
if empty(v1) {
return v2
}

return v1
}

func empty(given interface{}) bool {
g := reflect.ValueOf(given)
if !g.IsValid() {
return true
}

// Basically adapted from text/template.isTrue
switch g.Kind() {
default:
return g.IsNil()
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return g.Len() == 0
case reflect.Bool:
return !g.Bool()
case reflect.Complex64, reflect.Complex128:
return g.Complex() == 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return g.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return g.Uint() == 0
case reflect.Float32, reflect.Float64:
return g.Float() == 0
case reflect.Struct:
return false
}
}