Skip to content

Commit 64318c4

Browse files
committed
Latest YAML v4 handles tags differently.
setting bools, ints and floats causes rendering to fail tests. it’s automatically inferred.
1 parent 539f170 commit 64318c4

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

datamodel/high/node_builder.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package high
55

66
import (
77
"fmt"
8-
"math"
98
"reflect"
109
"sort"
1110
"strconv"
@@ -328,12 +327,11 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *nodes.NodeEntry) *ya
328327
precision := -1
329328
if entry.StringValue != "" && strings.Contains(entry.StringValue, ".") {
330329
precision = len(strings.Split(fmt.Sprint(entry.StringValue), ".")[1])
331-
val := strconv.FormatFloat(value.(float64), 'f', precision, 64)
332-
valueNode = utils.CreateFloatNode(val)
333-
} else {
334-
val := strconv.FormatFloat(value.(float64), 'f', precision, 64)
335-
valueNode = utils.CreateIntNode(val)
336330
}
331+
val := strconv.FormatFloat(value.(float64), 'f', precision, 64)
332+
// Always create float node for float64 values, even if they don't contain decimal points
333+
// This handles cases like negative zero (-0.0) which formats as "-0" but should remain float
334+
valueNode = utils.CreateFloatNode(val)
337335
valueNode.Line = line
338336
case reflect.Slice:
339337
var rawNode yaml.Node
@@ -510,11 +508,9 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *nodes.NodeEntry) *ya
510508
if *b != 0 || entry.RenderZero {
511509
formatFloat := strconv.FormatFloat(*b, 'f', -1, 64)
512510

513-
if *b == math.Trunc(*b) {
514-
valueNode = utils.CreateIntNode(formatFloat)
515-
} else {
516-
valueNode = utils.CreateFloatNode(formatFloat)
517-
}
511+
// Always create float node for float64 values, even if they're whole numbers
512+
// This handles cases like negative zero (-0.0) and ensures type consistency
513+
valueNode = utils.CreateFloatNode(formatFloat)
518514

519515
valueNode.Line = line
520516
}

datamodel/low/v2/responses_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestResponses_Build_WrongType(t *testing.T) {
6565

6666
func TestResponses_Hash(t *testing.T) {
6767
// Clear any cached hashes to ensure clean test
68-
index.ClearHashCache()
68+
low.ClearHashCache()
6969

7070
yml := `default:
7171
description: I am a potato

utils/nodes.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func CreateStringNode(str string) *yaml.Node {
5252
func CreateBoolNode(str string) *yaml.Node {
5353
n := &yaml.Node{
5454
Kind: yaml.ScalarNode,
55-
Tag: "!!bool",
5655
Value: str,
5756
}
5857
return n
@@ -61,7 +60,6 @@ func CreateBoolNode(str string) *yaml.Node {
6160
func CreateIntNode(str string) *yaml.Node {
6261
n := &yaml.Node{
6362
Kind: yaml.ScalarNode,
64-
Tag: "!!int",
6563
Value: str,
6664
}
6765
return n
@@ -79,7 +77,6 @@ func CreateEmptyScalarNode() *yaml.Node {
7977
func CreateFloatNode(str string) *yaml.Node {
8078
n := &yaml.Node{
8179
Kind: yaml.ScalarNode,
82-
Tag: "!!float",
8380
Value: str,
8481
}
8582
return n

utils/nodes_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestCreateBoolNode(t *testing.T) {
1313
b := CreateBoolNode("true")
14-
assert.Equal(t, "!!bool", b.Tag)
14+
assert.Equal(t, "", b.Tag) // No explicit tag - let YAML infer type
1515
assert.Equal(t, "true", b.Value)
1616
}
1717

@@ -35,13 +35,13 @@ func TestCreateEmptyScalarNode(t *testing.T) {
3535

3636
func TestCreateFloatNode(t *testing.T) {
3737
f := CreateFloatNode("3.14")
38-
assert.Equal(t, "!!float", f.Tag)
38+
assert.Equal(t, "", f.Tag) // No explicit tag - let YAML infer type
3939
assert.Equal(t, "3.14", f.Value)
4040
}
4141

4242
func TestCreateIntNode(t *testing.T) {
4343
i := CreateIntNode("42")
44-
assert.Equal(t, "!!int", i.Tag)
44+
assert.Equal(t, "", i.Tag) // No explicit tag - let YAML infer type
4545
assert.Equal(t, "42", i.Value)
4646
}
4747

@@ -57,6 +57,6 @@ func TestCreateRefNode(t *testing.T) {
5757

5858
func TestCreateYamlNode(t *testing.T) {
5959
y := CreateYamlNode("foo")
60-
assert.Equal(t, "!!str", y.Tag)
60+
assert.Equal(t, "!!str", y.Tag) // Encode() sets appropriate tag
6161
assert.Equal(t, "foo", y.Value)
6262
}

0 commit comments

Comments
 (0)