Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,9 @@ func (e *Encoder) encodeBool(v bool) *ast.BoolNode {
}

func (e *Encoder) encodeSlice(ctx context.Context, value reflect.Value) (*ast.SequenceNode, error) {
if e.indentSequence {
e.column += e.indentNum
defer func() { e.column -= e.indentNum }()
}
column := e.column
sequence := ast.Sequence(token.New("-", "-", e.pos(column)), e.isFlowStyle)
sequence := ast.Sequence(token.New("-", "-", e.pos(e.column)), e.isFlowStyle)
for i := 0; i < value.Len(); i++ {
node, err := e.encodeValue(ctx, value.Index(i), column)
node, err := e.encodeValue(ctx, value.Index(i), e.column)
if err != nil {
return nil, err
}
Expand All @@ -621,14 +616,9 @@ func (e *Encoder) encodeSlice(ctx context.Context, value reflect.Value) (*ast.Se
}

func (e *Encoder) encodeArray(ctx context.Context, value reflect.Value) (*ast.SequenceNode, error) {
if e.indentSequence {
e.column += e.indentNum
defer func() { e.column -= e.indentNum }()
}
column := e.column
sequence := ast.Sequence(token.New("-", "-", e.pos(column)), e.isFlowStyle)
sequence := ast.Sequence(token.New("-", "-", e.pos(e.column)), e.isFlowStyle)
for i := 0; i < value.Len(); i++ {
node, err := e.encodeValue(ctx, value.Index(i), column)
node, err := e.encodeValue(ctx, value.Index(i), e.column)
if err != nil {
return nil, err
}
Expand All @@ -650,6 +640,9 @@ func (e *Encoder) encodeMapItem(ctx context.Context, item MapItem, column int) (
if e.isTagAndMapNode(value) {
value.AddColumn(e.indentNum)
}
if e.isSequenceNode(value) && e.indentSequence {
value.AddColumn(e.indentNum)
}
return ast.MappingValue(
token.New("", "", e.pos(column)),
e.encodeString(k.Interface().(string), column),
Expand Down Expand Up @@ -679,6 +672,11 @@ func (e *Encoder) isTagAndMapNode(node ast.Node) bool {
return ok && e.isMapNode(tn.Value)
}

func (e *Encoder) isSequenceNode(node ast.Node) bool {
_, ok := node.(*ast.SequenceNode)
return ok
}

func (e *Encoder) encodeMap(ctx context.Context, value reflect.Value, column int) (ast.Node, error) {
node := ast.Mapping(token.New("", "", e.pos(column)), e.isFlowStyle)
keys := make([]interface{}, len(value.MapKeys()))
Expand All @@ -701,6 +699,9 @@ func (e *Encoder) encodeMap(ctx context.Context, value reflect.Value, column int
if e.isTagAndMapNode(encoded) {
encoded.AddColumn(e.indentNum)
}
if e.isSequenceNode(encoded) && e.indentSequence {
encoded.AddColumn(e.indentNum)
}
keyText := fmt.Sprint(key)
vRef := e.toPointer(v)

Expand Down Expand Up @@ -913,6 +914,9 @@ func (e *Encoder) encodeStruct(ctx context.Context, value reflect.Value, column
if e.isMapNode(encoded) {
encoded.AddColumn(e.indentNum)
}
if e.isSequenceNode(encoded) && e.indentSequence {
encoded.AddColumn(e.indentNum)
}
var key ast.MapKeyNode = e.encodeString(sf.RenderName, column)
switch {
case encoded.Type() == ast.AliasType:
Expand Down
29 changes: 29 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,35 @@ func TestEncoder(t *testing.T) {
yaml.UseSingleQuote(true),
},
},
// Sequence indentation
{
"[]" + "\n",
[]string{},
[]yaml.EncodeOption{
yaml.IndentSequence(false),
},
},
{
"[]" + "\n",
[]string{},
[]yaml.EncodeOption{
yaml.IndentSequence(true),
},
},
{
"- a\n- b\n- c" + "\n",
[]string{"a", "b", "c"},
[]yaml.EncodeOption{
yaml.IndentSequence(false),
},
},
{
"- a\n- b\n- c" + "\n",
[]string{"a", "b", "c"},
[]yaml.EncodeOption{
yaml.IndentSequence(true),
},
},
}
for _, test := range tests {
t.Run(test.source, func(t *testing.T) {
Expand Down
Loading