Skip to content

Commit

Permalink
Support sequence indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Rey committed Jun 29, 2021
1 parent b003782 commit 4768d50
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
17 changes: 13 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Encoder struct {
writer io.Writer
opts []EncodeOption
indent int
indentSequence bool
isFlowStyle bool
isJSONStyle bool
useJSONMarshaler bool
Expand Down Expand Up @@ -369,9 +370,13 @@ func (e *Encoder) encodeBool(v bool) ast.Node {
}

func (e *Encoder) encodeSlice(ctx context.Context, value reflect.Value) (ast.Node, error) {
sequence := ast.Sequence(token.New("-", "-", e.pos(e.column)), e.isFlowStyle)
column := e.column
if e.indentSequence {
column += e.indent
}
sequence := ast.Sequence(token.New("-", "-", e.pos(column)), e.isFlowStyle)
for i := 0; i < value.Len(); i++ {
node, err := e.encodeValue(ctx, value.Index(i), e.column)
node, err := e.encodeValue(ctx, value.Index(i), column)
if err != nil {
return nil, errors.Wrapf(err, "failed to encode value for slice")
}
Expand All @@ -381,9 +386,13 @@ func (e *Encoder) encodeSlice(ctx context.Context, value reflect.Value) (ast.Nod
}

func (e *Encoder) encodeArray(ctx context.Context, value reflect.Value) (ast.Node, error) {
sequence := ast.Sequence(token.New("-", "-", e.pos(e.column)), e.isFlowStyle)
column := e.column
if e.indentSequence {
column += e.indent
}
sequence := ast.Sequence(token.New("-", "-", e.pos(column)), e.isFlowStyle)
for i := 0; i < value.Len(); i++ {
node, err := e.encodeValue(ctx, value.Index(i), e.column)
node, err := e.encodeValue(ctx, value.Index(i), column)
if err != nil {
return nil, errors.Wrapf(err, "failed to encode value for array")
}
Expand Down
29 changes: 29 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,25 @@ func TestEncoder(t *testing.T) {
map[string][]string{"v": {"A", "B"}},
nil,
},
{
"v:\n - A\n - B\n",
map[string][]string{"v": {"A", "B"}},
[]yaml.EncodeOption{
yaml.IndentSequence(true),
},
},
{
"v:\n- A\n- B\n",
map[string][2]string{"v": {"A", "B"}},
nil,
},
{
"v:\n - A\n - B\n",
map[string][2]string{"v": {"A", "B"}},
[]yaml.EncodeOption{
yaml.IndentSequence(true),
},
},
{
"a: -\n",
map[string]string{"a": "-"},
Expand Down Expand Up @@ -207,6 +221,21 @@ func TestEncoder(t *testing.T) {
},
nil,
},
{
"v:\n - A\n - 1\n - B:\n - 2\n - 3\n",
map[string]interface{}{
"v": []interface{}{
"A",
1,
map[string][]int{
"B": {2, 3},
},
},
},
[]yaml.EncodeOption{
yaml.IndentSequence(true),
},
},
{
"a:\n b: c\n",
map[string]interface{}{
Expand Down
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func Indent(spaces int) EncodeOption {
}
}

// IndentSequence causes sequence values to be indented the same value as Indent
func IndentSequence(indent bool) EncodeOption {
return func(e *Encoder) error {
e.indentSequence = indent
return nil
}
}

// Flow encoding by flow style
func Flow(isFlowStyle bool) EncodeOption {
return func(e *Encoder) error {
Expand Down

0 comments on commit 4768d50

Please sign in to comment.