-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
158 additions
and
10 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,48 @@ | ||
package interpolation | ||
|
||
import ( | ||
"errors" | ||
"math" | ||
|
||
"github.com/hashicorp/hil/ast" | ||
) | ||
|
||
// interpolationFuncMax returns the largest value in a series of floats | ||
func interpolationFuncMax() ast.Function { | ||
return ast.Function{ | ||
ArgTypes: []ast.Type{}, | ||
ReturnType: ast.TypeFloat, | ||
Variadic: true, | ||
VariadicType: ast.TypeFloat, | ||
Callback: func(inputs []interface{}) (interface{}, error) { | ||
if len(inputs) < 2 { | ||
return nil, errors.New("max function requires at least 2 inputs") | ||
} | ||
max := inputs[0].(float64) | ||
for _, input := range inputs[1:] { | ||
max = math.Max(max, input.(float64)) | ||
} | ||
return max, nil | ||
}, | ||
} | ||
} | ||
|
||
// interpolationFuncMin returns the smallest value in a series of floats | ||
func interpolationFuncMin() ast.Function { | ||
return ast.Function{ | ||
ArgTypes: []ast.Type{}, | ||
ReturnType: ast.TypeFloat, | ||
Variadic: true, | ||
VariadicType: ast.TypeFloat, | ||
Callback: func(inputs []interface{}) (interface{}, error) { | ||
if len(inputs) < 2 { | ||
return nil, errors.New("min function requires at least 2 inputs") | ||
} | ||
min := inputs[0].(float64) | ||
for _, input := range inputs[1:] { | ||
min = math.Min(min, input.(float64)) | ||
} | ||
return min, nil | ||
}, | ||
} | ||
} |
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,61 @@ | ||
package interpolation | ||
|
||
import "testing" | ||
|
||
func TestInterpolationFuncMax(t *testing.T) { | ||
testCases := []functionTestCase{ | ||
{ | ||
description: "Should find the max value", | ||
text: `${max(0, 2)}`, | ||
expectation: "2", | ||
}, | ||
{ | ||
description: "One input should fail", | ||
text: `${max(4)}`, | ||
expectation: "", | ||
evalError: true, | ||
}, | ||
{ | ||
description: "Long list finds max", | ||
text: `${max(100, 30, 450, 1, 25)}`, | ||
expectation: "450", | ||
}, | ||
} | ||
|
||
maxTestFunc := testInterpolationFunc(keyFuncs{"max": interpolationFuncMax}) | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
maxTestFunc(t, tc) | ||
}) | ||
} | ||
} | ||
|
||
func TestInterpolationFuncMin(t *testing.T) { | ||
testCases := []functionTestCase{ | ||
{ | ||
description: "Should find the min value", | ||
text: `${min(140, 250)}`, | ||
expectation: "140", | ||
}, | ||
{ | ||
description: "One input should fail", | ||
text: `${min(4)}`, | ||
expectation: "", | ||
evalError: true, | ||
}, | ||
{ | ||
description: "Long list finds min", | ||
text: `${min(100, 30, 450, 1, 25)}`, | ||
expectation: "1", | ||
}, | ||
} | ||
|
||
minTestFunc := testInterpolationFunc(keyFuncs{"min": interpolationFuncMin}) | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
minTestFunc(t, tc) | ||
}) | ||
} | ||
} |
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