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: add more list funcs #29

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ jobs:
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7
with:
go-version: 'stable'
- run: go test ./...
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7
with:
go-version: 'stable'
- uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64
with:
version: v1.58.1
Expand Down
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ linters:
- wsl

linters-settings:
exhaustive:
default-signifies-exhaustive: true
gci:
sections:
- standard
Expand Down
20 changes: 20 additions & 0 deletions docs/templatefuncs.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Template Functions

## `compact` *list*

`compact` removes all zero value items from *list*.

```text
{{ list "one" "" list "three" | compact }}

[one three]
```

## `contains` *substring* *string*

`contains` returns whether *substring* is in *string*.
Expand Down Expand Up @@ -152,6 +162,16 @@ far
adcda
```

## `reverse` *list*

`reverse` returns a copy of *list* with its elements in reverse order.

```text
{{ list 1 2 3 | reverse }}

[3 2 1]
```

## `stat` *path*

`stat` returns a map representation of executing
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/chezmoi/templatefuncs

go 1.19
go 1.22

require github.com/alecthomas/assert/v2 v2.9.0

Expand Down
47 changes: 47 additions & 0 deletions templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"
"os"
"os/exec"
"reflect"
"regexp"
"strconv"
"strings"
Expand All @@ -29,6 +30,7 @@ var fileModeTypeNames = map[fs.FileMode]string{
// functions.
func NewFuncMap() template.FuncMap {
return template.FuncMap{
"compact": compactTemplateFunc,
"contains": reverseArgs2(strings.Contains),
"eqFold": eqFoldTemplateFunc,
"fromJSON": eachByteSliceErr(fromJSONTemplateFunc),
Expand All @@ -44,6 +46,7 @@ func NewFuncMap() template.FuncMap {
"quote": eachString(strconv.Quote),
"regexpReplaceAll": regexpReplaceAllTemplateFunc,
"replaceAll": replaceAllTemplateFunc,
"reverse": reverseTemplateFunc,
"stat": eachString(statTemplateFunc),
"toJSON": toJSONTemplateFunc,
"toLower": eachString(strings.ToLower),
Expand All @@ -53,6 +56,23 @@ func NewFuncMap() template.FuncMap {
}
}

// compactTemplateFunc is the core implementation of the `compact` template
// function.
func compactTemplateFunc(list any) any {
v := reflect.ValueOf(list)
if v.Kind() != reflect.Slice {
panic(fmt.Sprintf("unable to compact argument of type %T", list))
}
result := reflect.MakeSlice(v.Type(), 0, v.Len())
for i := 0; i < v.Len(); i++ {
elem := v.Index(i)
if !isZeroValue(elem.Interface()) {
result = reflect.Append(result, elem)
}
}
return result.Interface()
}

// eqFoldTemplateFunc is the core implementation of the `eqFold` template
// function.
func eqFoldTemplateFunc(first, second string, more ...string) bool {
Expand Down Expand Up @@ -159,6 +179,23 @@ func regexpReplaceAllTemplateFunc(expr, repl, s string) string {
return regexp.MustCompile(expr).ReplaceAllString(s, repl)
}

// reverseTemplateFunc is the core implementation of the `reverse`
// template function.
func reverseTemplateFunc(list any) any {
v := reflect.ValueOf(list)
if v.Kind() != reflect.Slice {
panic(fmt.Sprintf("unable to reverse argument of type %T", list))
}
if v.Len() <= 1 {
return list
}
result := reflect.MakeSlice(v.Type(), 0, v.Len())
for i := v.Len() - 1; i >= 0; i-- {
result = reflect.Append(result, v.Index(i))
}
return result.Interface()
}

// statTemplateFunc is the core implementation of the `stat` template function.
func statTemplateFunc(name string) any {
switch fileInfo, err := os.Stat(name); {
Expand Down Expand Up @@ -377,6 +414,16 @@ func fileInfoToMap(fileInfo fs.FileInfo) map[string]any {
}
}

// isZeroValue returns whether a value is the zero value for its type.
// An empty array, map or slice is assumed to be a zero value.
func isZeroValue(v any) bool {
truth, ok := template.IsTrue(v)
if !ok {
panic(fmt.Sprintf("unable to determine zero value for %v (type %T)", v, v))
}
return !truth
}

// reverseArgs2 transforms a function that takes two arguments and returns an
// `R` into a function that takes the arguments in reverse order and returns an
// `R`.
Expand Down
55 changes: 52 additions & 3 deletions templatefuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,40 @@ func TestFuncMap(t *testing.T) {
data any
expected string
}{
{},
{
template: `{{ . | compact | printf "%[1]v\n%[1]T\n" }}`,
data: []string{"", "a", "", "b", "c"},
expected: joinLines(
"[a b c]",
"[]string",
),
},
{
template: `{{ . | compact | printf "%[1]v\n%[1]T\n" }}`,
data: []int{0, 1, 0, 2, 3},
expected: joinLines(
"[1 2 3]",
"[]int",
),
},
{
template: `{{ . | compact | printf "%[1]v\n%[1]T\n" }}`,
data: []any{map[string]any{}, "a", 1, []string{}, 7.7},
expected: joinLines(
"[a 1 7.7]",
"[]interface {}",
),
},
{
template: `{{ . | compact }}`,
data: []any{},
expected: "[]",
},
{
template: `{{ . | compact }}`,
data: []int{0, 0, 0, 0},
expected: "[]",
},
{
template: `{{ "abc" | contains "bc" }}`,
expected: "true",
Expand Down Expand Up @@ -118,6 +151,10 @@ func TestFuncMap(t *testing.T) {
"# b",
),
},
{
template: `{{ quote "a" }}`,
expected: `"a"`,
},
{
template: `{{ "abcba" | replaceAll "b" "d" }}`,
expected: `adcda`,
Expand All @@ -127,8 +164,20 @@ func TestFuncMap(t *testing.T) {
expected: "[adc cda]",
},
{
template: `{{ quote "a" }}`,
expected: `"a"`,
template: `{{ . | reverse | printf "%[1]v\n%[1]T\n" }}`,
data: []string{"a", "b", "c"},
expected: joinLines(
"[c b a]",
"[]string",
),
},
{
template: `{{ . | reverse | printf "%[1]v\n%[1]T\n" }}`,
data: []int{1, 2, 3},
expected: joinLines(
"[3 2 1]",
"[]int",
),
},
{
template: `{{ (stat "testdata/file").type }}`,
Expand Down
Loading