Repository: goccy/go-yaml
Pull request #98 (2020-03-09 13:10:07)

Fix empty ( slice, map, struct ) style
Thank you for your [comment](https://github.com/goccy/go-yaml/pull/97#issuecomment-596406482) !

Fix empty ( slice, map, struct ) encoding style.



Patch:
diff --git a/ast/ast.go b/ast/ast.go
index df51140..a16c5ef 100644
--- a/ast/ast.go
+++ b/ast/ast.go
@@ -681,7 +681,7 @@ func (n *MappingNode) blockStyleString() string {
 
 // String mapping values to text
 func (n *MappingNode) String() string {
-	if n.IsFlowStyle {
+	if n.IsFlowStyle || len(n.Values) == 0 {
 		return n.flowStyleString()
 	}
 	return n.blockStyleString()
@@ -730,9 +730,9 @@ func (n *MappingValueNode) String() string {
 		return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
 	} else if keyIndentLevel < valueIndentLevel {
 		return fmt.Sprintf("%s%s:\n%s", space, n.Key.String(), n.Value.String())
-	} else if m, ok := n.Value.(*MappingNode); ok && m.IsFlowStyle {
+	} else if m, ok := n.Value.(*MappingNode); ok && (m.IsFlowStyle || len(m.Values) == 0) {
 		return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
-	} else if s, ok := n.Value.(*SequenceNode); ok && s.IsFlowStyle {
+	} else if s, ok := n.Value.(*SequenceNode); ok && (s.IsFlowStyle || len(s.Values) == 0) {
 		return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
 	} else if _, ok := n.Value.(*AnchorNode); ok {
 		return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
@@ -833,7 +833,7 @@ func (n *SequenceNode) blockStyleString() string {
 
 // String sequence to text
 func (n *SequenceNode) String() string {
-	if n.IsFlowStyle {
+	if n.IsFlowStyle || len(n.Values) == 0 {
 		return n.flowStyleString()
 	}
 	return n.blockStyleString()
diff --git a/encode.go b/encode.go
index ca93249..6c46188 100644
--- a/encode.go
+++ b/encode.go
@@ -254,11 +254,7 @@ func (e *Encoder) encodeBool(v bool) ast.Node {
 }
 
 func (e *Encoder) encodeSlice(value reflect.Value) (ast.Node, error) {
-	isFlowStyle := e.isFlowStyle
-	if value.Len() == 0 {
-		isFlowStyle = true
-	}
-	sequence := ast.Sequence(token.New("-", "-", e.pos(e.column)), isFlowStyle)
+	sequence := ast.Sequence(token.New("-", "-", e.pos(e.column)), e.isFlowStyle)
 	for i := 0; i < value.Len(); i++ {
 		node, err := e.encodeValue(value.Index(i), e.column)
 		if err != nil {
@@ -299,11 +295,7 @@ func (e *Encoder) encodeMapSlice(value MapSlice, column int) (ast.Node, error) {
 }
 
 func (e *Encoder) encodeMap(value reflect.Value, column int) ast.Node {
-	isFlowStyle := e.isFlowStyle
-	if value.Len() == 0 {
-		isFlowStyle = true
-	}
-	node := ast.Mapping(token.New("", "", e.pos(column)), isFlowStyle)
+	node := ast.Mapping(token.New("", "", e.pos(column)), e.isFlowStyle)
 	keys := []string{}
 	for _, k := range value.MapKeys() {
 		keys = append(keys, k.Interface().(string))


Test patch:
diff --git a/encode_test.go b/encode_test.go
index 23ea618..c1e18b4 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -317,6 +317,32 @@ func TestEncoder(t *testing.T) {
 			}{0, 0},
 		},
 
+		{
+			"a:\n  y: \"\"\n",
+			struct {
+				A *struct {
+					X string `yaml:"x,omitempty"`
+					Y string
+				}
+			}{&struct {
+				X string `yaml:"x,omitempty"`
+				Y string
+			}{}},
+		},
+
+		{
+			"a: {}\n",
+			struct {
+				A *struct {
+					X string `yaml:"x,omitempty"`
+					Y string `yaml:"y,omitempty"`
+				}
+			}{&struct {
+				X string `yaml:"x,omitempty"`
+				Y string `yaml:"y,omitempty"`
+			}{}},
+		},
+
 		{
 			"a: {x: 1}\n",
 			struct {