-
Notifications
You must be signed in to change notification settings - Fork 1
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
bradenhilton
wants to merge
9
commits into
master
Choose a base branch
from
list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf5e4c5
feat: add more list funcs
bradenhilton 3fd6298
reverse
bradenhilton b674fe9
terrible sort
bradenhilton dc69ef8
ugly sort
bradenhilton 5d26c4e
slightly less ugly sort
bradenhilton 0e91528
uniq
bradenhilton ea3fac2
concat
bradenhilton f97e568
new compact
bradenhilton ccbc3a5
new reverse
bradenhilton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/chezmoi/templatefuncs | ||
|
||
go 1.19 | ||
go 1.21 | ||
|
||
require github.com/alecthomas/assert/v2 v2.9.0 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ import ( | |
"io/fs" | ||
"os" | ||
"os/exec" | ||
"reflect" | ||
"regexp" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
|
@@ -29,13 +31,16 @@ 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), | ||
"has": reverseArgs2(slices.Contains[[]any]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as for |
||
"hasPrefix": reverseArgs2(strings.HasPrefix), | ||
"hasSuffix": reverseArgs2(strings.HasSuffix), | ||
"hexDecode": eachStringErr(hex.DecodeString), | ||
"hexEncode": eachByteSlice(hex.EncodeToString), | ||
"indexOf": reverseArgs2(slices.Index[[]any]), | ||
"join": reverseArgs2(strings.Join), | ||
"list": listTemplateFunc, | ||
"lookPath": eachStringErr(lookPathTemplateFunc), | ||
|
@@ -44,6 +49,8 @@ func NewFuncMap() template.FuncMap { | |
"quote": eachString(strconv.Quote), | ||
"regexpReplaceAll": regexpReplaceAllTemplateFunc, | ||
"replaceAll": replaceAllTemplateFunc, | ||
"reverse": reverseTemplateFunc, | ||
"sort": sortTemplateFunc, | ||
"stat": eachString(statTemplateFunc), | ||
"toJSON": toJSONTemplateFunc, | ||
"toLower": eachString(strings.ToLower), | ||
|
@@ -53,6 +60,12 @@ func NewFuncMap() template.FuncMap { | |
} | ||
} | ||
|
||
// compactTemplateFunc is the core implementation of the `compact` template | ||
// function. | ||
func compactTemplateFunc(list []any) []any { | ||
return slices.DeleteFunc(list, isZeroValue) | ||
} | ||
|
||
// eqFoldTemplateFunc is the core implementation of the `eqFold` template | ||
// function. | ||
func eqFoldTemplateFunc(first, second string, more ...string) bool { | ||
|
@@ -159,6 +172,33 @@ 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 { | ||
listcopy := append([]any(nil), list...) | ||
slices.Reverse(listcopy) | ||
return listcopy | ||
} | ||
|
||
// sortTemplateFunc is the core implementation of the `sort` template function. | ||
func sortTemplateFunc(list []any) []any { | ||
strCopy := make([]string, len(list)) | ||
for i, v := range list { | ||
strCopy[i] = toStringTemplateFunc(v) | ||
} | ||
slices.Sort(strCopy) | ||
for i, newValue := range strCopy { | ||
for j, v := range list { | ||
strv := toStringTemplateFunc(v) | ||
if strv == newValue { | ||
list[i], list[j] = list[j], list[i] | ||
break | ||
} | ||
} | ||
} | ||
return list | ||
} | ||
|
||
// statTemplateFunc is the core implementation of the `stat` template function. | ||
func statTemplateFunc(name string) any { | ||
switch fileInfo, err := os.Stat(name); { | ||
|
@@ -377,6 +417,33 @@ 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 { | ||
vval := reflect.ValueOf(v) | ||
if !vval.IsValid() { | ||
return true | ||
} | ||
switch vval.Kind() { //nolint:exhaustive | ||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String: | ||
return vval.Len() == 0 | ||
case reflect.Bool: | ||
return !vval.Bool() | ||
case reflect.Complex64, reflect.Complex128: | ||
return vval.Complex() == 0 | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return vval.Int() == 0 | ||
case reflect.Float32, reflect.Float64: | ||
return vval.Float() == 0 | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return vval.Uint() == 0 | ||
case reflect.Struct: | ||
return false | ||
default: | ||
return vval.IsNil() | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be replaced with something like truth, ok := template.IsTrue(v)
if !ok {
panic(fmt.Sprintf("unable to determine zero value for %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`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: duplicated
list
.