mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #8590 from hashicorp/b-template-floats
providers/template: template_file supports floating point math
This commit is contained in:
commit
1b5d7cd2c6
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/hil"
|
"github.com/hashicorp/hil"
|
||||||
@ -116,9 +117,20 @@ func execute(s string, vars map[string]interface{}) (string, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("unexpected type for variable %q: %T", k, v)
|
return "", fmt.Errorf("unexpected type for variable %q: %T", k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store the defaults (string and value)
|
||||||
|
var val interface{} = s
|
||||||
|
typ := ast.TypeString
|
||||||
|
|
||||||
|
// If we can parse a float, then use that
|
||||||
|
if v, err := strconv.ParseFloat(s, 64); err == nil {
|
||||||
|
val = v
|
||||||
|
typ = ast.TypeFloat
|
||||||
|
}
|
||||||
|
|
||||||
varmap[k] = ast.Variable{
|
varmap[k] = ast.Variable{
|
||||||
Value: s,
|
Value: val,
|
||||||
Type: ast.TypeString,
|
Type: typ,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +26,8 @@ func TestTemplateRendering(t *testing.T) {
|
|||||||
{`{a="foo"}`, `$${a}`, `foo`},
|
{`{a="foo"}`, `$${a}`, `foo`},
|
||||||
{`{a="hello"}`, `$${replace(a, "ello", "i")}`, `hi`},
|
{`{a="hello"}`, `$${replace(a, "ello", "i")}`, `hi`},
|
||||||
{`{}`, `${1+2+3}`, `6`},
|
{`{}`, `${1+2+3}`, `6`},
|
||||||
|
{`{a=1, b=2}`, `$${a+b}`, `3`},
|
||||||
|
{`{a=0.1, b=0.2}`, `$${0+((a+b)*10)}`, `3`},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range cases {
|
for _, tt := range cases {
|
||||||
|
Loading…
Reference in New Issue
Block a user