providers: Add slice functions provider

This commit is contained in:
Sasha Koshka 2024-12-07 22:18:45 -05:00
parent 7f64686323
commit d76374719d
2 changed files with 67 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import "git.tebibyte.media/sashakoshka/step"
import fpos "git.tebibyte.media/sashakoshka/step/providers/os"
import fphttp "git.tebibyte.media/sashakoshka/step/providers/http"
import fppath "git.tebibyte.media/sashakoshka/step/providers/path"
import fpslice "git.tebibyte.media/sashakoshka/step/providers/slice"
import fpsprig "git.tebibyte.media/sashakoshka/step/providers/sprig"
import fpimport "git.tebibyte.media/sashakoshka/step/providers/import"
import fpmarkdown "git.tebibyte.media/sashakoshka/step/providers/markdown"
@ -14,6 +15,7 @@ func All () []step.FuncProvider {
new(fpos.Provider),
new(fphttp.Provider),
new(fppath.Provider),
new(fpslice.Provider),
new(fpsprig.Provider),
new(fpimport.Provider),
fpmarkdown.Default(),

65
providers/slice/slice.go Normal file
View File

@ -0,0 +1,65 @@
package slice
import "fmt"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
const hiddenPrefix = "."
var _ step.FuncProvider = new(Provider)
// Provider provides slice functions.
type Provider struct {
}
// FuncMap fulfills the step.FuncProvider interface.
func (this *Provider) FuncMap () template.FuncMap {
return template.FuncMap {
"sAppend": funcSAppend,
"iAppend": funcIAppend,
"fAppend": funcFAppend,
}
}
func funcSAppend (slice any, addition string) []string {
switch slice := slice.(type) {
case []string:
newSlice := make([]string, len(slice) + 1)
copy(newSlice, slice)
newSlice[len(slice)] = addition
return newSlice
case string:
return []string { slice, addition }
default:
return funcSAppend(fmt.Sprint(slice), addition)
}
}
func funcIAppend (slice any, addition int) []int {
switch slice := slice.(type) {
case []int:
newSlice := make([]int, len(slice) + 1)
copy(newSlice, slice)
newSlice[len(slice)] = addition
return newSlice
case int:
return []int { slice, addition }
default:
return funcIAppend(0, addition)
}
}
func funcFAppend (slice any, addition float64) []float64 {
switch slice := slice.(type) {
case []float64:
newSlice := make([]float64, len(slice) + 1)
copy(newSlice, slice)
newSlice[len(slice)] = addition
return newSlice
case float64:
return []float64 { slice, addition }
default:
return funcFAppend(0, addition)
}
}