package template_functions import ( "errors" "strconv" "time" "git.ctdo.de/ctdo/machinelock-manager/db" uuid "github.com/satori/go.uuid" ) 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 } func IsAllowed(token db.Token, id *uuid.UUID) bool { if id == nil { return false } for _, m := range token.Machines { if m.ID != nil && uuid.Equal(*m.ID, *id) { return true } } return false } var FunctionMap = map[string]interface{}{"For": For, "Price": Price, "Now": Now, "Add": Add, "Dict": Dict, "IsAllowed": IsAllowed}