machinelock-manager/template_functions/main.go

50 lines
988 B
Go
Raw Permalink Normal View History

2024-12-14 21:30:39 +00:00
package template_functions
import (
"errors"
"strconv"
"time"
)
func For(start, end int) <-chan int {
c := make(chan int)
go func() {
for i := start; i <= end; i++ {
c <- i
}
close(c)
}()
return c
}
func Price(price int) string {
amount := strconv.Itoa(price)
for len(amount) < 3 {
amount = "0" + amount
}
amount = amount[:len(amount)-2] + "." + amount[len(amount)-2:]
return amount
}
func Now() time.Time {
return time.Now()
}
func Add(x, y int) int {
return x + y
}
func Dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
var FunctionMap = map[string]interface{}{"For": For, "Price": Price, "Now": Now, "Add": Add, "Dict": Dict}