Skip to content

Commit

Permalink
Merge pull request #98 from k1LoW/ast-empty-style
Browse files Browse the repository at this point in the history
Fix empty ( slice, map, struct ) style
  • Loading branch information
goccy authored Mar 10, 2020
2 parents 6bf031d + f92ff94 commit 58a7502
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
8 changes: 4 additions & 4 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 2 additions & 10 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
26 changes: 26 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 58a7502

Please sign in to comment.