Skip to content

Commit

Permalink
merge branch #36 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed Jun 19, 2018
2 parents 16cf81e + 45f0ff1 commit 89bbaca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions template/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var funcs functions = make(map[string]functionWithValidator)
func init() {
funcs.add("timestamp", newTimestampFunc())
funcs.add("gsub", newGsubFunc())
funcs.add("multiply", newMultiplyFunc())
}

type functions map[string]functionWithValidator
Expand Down
36 changes: 36 additions & 0 deletions template/multiply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package template

import (
"fmt"
"strconv"
"text/template/parse"
)

func newMultiplyFunc() functionWithValidator {
return functionWithValidator{
function: multiply,
staticValidator: validateMultiplyCall,
}
}

func multiply(value string, multiplier float64) (float64, error) {
i, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, err
}

return i * multiplier, nil
}

func validateMultiplyCall(cmd *parse.CommandNode) error {
prefix := "syntax error in shift call"
if len(cmd.Args) != 3 {
return fmt.Errorf("%v: expected two parameters, but found %v parameters", prefix, len(cmd.Args)-1)
}
exponentNode, ok := cmd.Args[2].(*parse.NumberNode)
if !ok || !exponentNode.IsFloat {
return fmt.Errorf("%v: unable to parse %v as a float number", prefix, exponentNode.Text)
}

return nil
}

0 comments on commit 89bbaca

Please sign in to comment.