Skip to content

Commit

Permalink
Add arithmatic float support
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Oct 19, 2019
1 parent 92bf6c5 commit 50bc152
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
12 changes: 8 additions & 4 deletions pkg/expressions/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ var defaultFunctions = map[string]KeyBuilderFunction{
"subi": arithmaticHelperi(func(a, b int) int { return a - b }),
"multi": arithmaticHelperi(func(a, b int) int { return a * b }),
"divi": arithmaticHelperi(func(a, b int) int { return a / b }),
"sumf": arithmaticHelperf(func(a, b float64) float64 { return a + b }),
"subf": arithmaticHelperf(func(a, b float64) float64 { return a - b }),
"multf": arithmaticHelperf(func(a, b float64) float64 { return a * b }),
"divf": arithmaticHelperf(func(a, b float64) float64 { return a / b }),
"eq": stringComparator(func(a, b string) string {
if a == b {
return a
Expand All @@ -22,10 +26,10 @@ var defaultFunctions = map[string]KeyBuilderFunction{
return ""
}),
"not": KeyBuilderFunction(kfNot),
"lt": arithmaticEqualityHelper(func(a, b int) bool { return a < b }),
"gt": arithmaticEqualityHelper(func(a, b int) bool { return a > b }),
"lte": arithmaticEqualityHelper(func(a, b int) bool { return a <= b }),
"gte": arithmaticEqualityHelper(func(a, b int) bool { return a >= b }),
"lt": arithmaticEqualityHelper(func(a, b float64) bool { return a < b }),
"gt": arithmaticEqualityHelper(func(a, b float64) bool { return a > b }),
"lte": arithmaticEqualityHelper(func(a, b float64) bool { return a <= b }),
"gte": arithmaticEqualityHelper(func(a, b float64) bool { return a >= b }),
"and": KeyBuilderFunction(kfAnd),
"or": KeyBuilderFunction(kfOr),
"like": KeyBuilderFunction(kfLike),
Expand Down
25 changes: 25 additions & 0 deletions pkg/expressions/funcsArithmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,28 @@ func arithmaticHelperi(equation func(int, int) int) KeyBuilderFunction {
})
})
}

// Simple helper that will take 2 or more integers, and apply an operation
func arithmaticHelperf(equation func(float64, float64) float64) KeyBuilderFunction {
return KeyBuilderFunction(func(args []KeyBuilderStage) KeyBuilderStage {
if len(args) < 2 {
return stageError(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
final, err := strconv.ParseFloat(args[0](context), 64)
if err != nil {
return ErrorType
}

for i := 1; i < len(args); i++ {
val, err := strconv.ParseFloat(args[i](context), 64)
if err != nil {
return ErrorType
}
final = equation(final, val)
}

return strconv.FormatFloat(final, 'f', -1, 64)
})
})
}
6 changes: 3 additions & 3 deletions pkg/expressions/funcsComparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func stringComparator(equation func(string, string) string) KeyBuilderFunction {
}

// Checks equality, and returns truthy if equals, and empty if not
func arithmaticEqualityHelper(test func(int, int) bool) KeyBuilderFunction {
func arithmaticEqualityHelper(test func(float64, float64) bool) KeyBuilderFunction {
return KeyBuilderFunction(func(args []KeyBuilderStage) KeyBuilderStage {
if len(args) != 2 {
return stageError(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
left, err := strconv.Atoi(args[0](context))
left, err := strconv.ParseFloat(args[0](context), 64)
if err != nil {
return ErrorType
}
right, err := strconv.Atoi(args[1](context))
right, err := strconv.ParseFloat(args[1](context), 64)
if err != nil {
return ErrorType
}
Expand Down

0 comments on commit 50bc152

Please sign in to comment.