Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
add cell,eq,hasprefix,hassuffix template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rad committed Jul 4, 2016
1 parent f4a45eb commit 6108cd7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions bin-templater/template/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package template
import (
"bufio"
"encoding/json"
"fmt"
"github.com/leekchan/gtf"
"gopkg.in/yaml.v2"
"io"
Expand Down Expand Up @@ -269,6 +270,45 @@ func sub(x, y int) int {
return x - y
}

func eq(args ...interface{}) bool {
if len(args) == 0 {
return false
}
x := args[0]
switch x := x.(type) {
case string, int, int64, byte, float32, float64:
for _, y := range args[1:] {
if x == y {
return true
}
}
return false
}

for _, y := range args[1:] {
if reflect.DeepEqual(x, y) {
return true
}
}
return false
}

type Cell struct{ v interface{} }

func NewCell(v ...interface{}) (*Cell, error) {
switch len(v) {
case 0:
return new(Cell), nil
case 1:
return &Cell{v[0]}, nil
default:
return nil, fmt.Errorf("wrong number of args: want 0 or 1, got %v", len(v))
}
}

func (c *Cell) Set(v interface{}) *Cell { c.v = v; return c }
func (c *Cell) Get() interface{} { return c.v }

func init() {
TemplateFunctions = make(map[string]interface{})
TemplateFunctions["base"] = path.Base
Expand All @@ -284,6 +324,8 @@ func init() {
TemplateFunctions["contains"] = strings.Contains
TemplateFunctions["replace"] = strings.Replace
TemplateFunctions["repeat"] = strings.Repeat
TemplateFunctions["hasPrefix"] = strings.HasPrefix
TemplateFunctions["hasSuffix"] = strings.HasSuffix
TemplateFunctions["orDef"] = orDef
TemplateFunctions["orDefs"] = orDefs
TemplateFunctions["ifOrDef"] = ifOrDef
Expand All @@ -303,6 +345,8 @@ func init() {
TemplateFunctions["mod"] = mod
TemplateFunctions["toJson"] = toJson
TemplateFunctions["toYaml"] = toYaml
TemplateFunctions["cell"] = NewCell
TemplateFunctions["eq"] = eq

TemplateFunctions["IsMapFirst"] = IsMapFirst
TemplateFunctions["IsMapLast"] = IsMapLast
Expand Down

0 comments on commit 6108cd7

Please sign in to comment.