Skip to content

Commit

Permalink
Merge pull request #483 from vmware-tanzu/fix-floats
Browse files Browse the repository at this point in the history
Parse floats correctly and fix mistake from go-yaml/yaml#171
  • Loading branch information
cppforlife authored Sep 9, 2021
2 parents c83f348 + 7699046 commit dafff19
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/yamlmeta/internal/yaml.v2/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,13 @@ var unmarshalTests = []struct {
M{"ñoño": "very yes 🟔"},
},

// YAML Float regex shouldn't match this
// This *is* in fact a float number, per the spec. #171 was a mistake.
{
"a: 123456e1\n",
M{"a": "123456e1"},
M{"a": 123456e1},
}, {
"a: 123456E1\n",
M{"a": "123456E1"},
M{"a": 123456E1},
},
// yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/yamlmeta/internal/yaml.v2/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func resolvableTag(tag string) bool {
return false
}

var yamlStyleFloat = regexp.MustCompile(`^[-+]?[0-9]*\.?[0-9]+([eE][-+][0-9]+)?$`)
var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`)

func resolve(tag string, in string) (rtag string, out interface{}) {
if !resolvableTag(tag) {
Expand Down
24 changes: 23 additions & 1 deletion pkg/yamlmeta/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package yamlmeta_test

import (
"fmt"
"github.com/k14s/difflib"
"os"
"strings"
"testing"

"github.com/k14s/difflib"
"github.com/k14s/ytt/pkg/filepos"
"github.com/k14s/ytt/pkg/yamlmeta"
)
Expand Down Expand Up @@ -179,6 +179,28 @@ func TestParserRootValue(t *testing.T) {
Position: filepos.NewUnknownPosition(),
},
},
{Description: "float", Data: "2000.1",
Expected: &yamlmeta.DocumentSet{
Items: []*yamlmeta.Document{
&yamlmeta.Document{
Value: 2000.1,
Position: filepos.NewPosition(1),
},
},
Position: filepos.NewUnknownPosition(),
},
},
{Description: "float (exponent)", Data: "9e3",
Expected: &yamlmeta.DocumentSet{
Items: []*yamlmeta.Document{
&yamlmeta.Document{
Value: 9000.0,
Position: filepos.NewPosition(1),
},
},
Position: filepos.NewUnknownPosition(),
},
},
{Description: "array", Data: "- 1",
Expected: &yamlmeta.DocumentSet{
Items: []*yamlmeta.Document{
Expand Down

0 comments on commit dafff19

Please sign in to comment.