-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |