Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't trim all space characters in SequenceNode.blockStyleString #361

Merged
merged 2 commits into from
Mar 21, 2023
Merged
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
3 changes: 2 additions & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,8 +1570,9 @@ func (n *SequenceNode) blockStyleString() string {
diffLength := len(splittedValues[0]) - len(trimmedFirstValue)
if len(splittedValues) > 1 && value.Type() == StringType || value.Type() == LiteralType {
// If multi-line string, the space characters for indent have already been added, so delete them.
prefix := space + " "
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prefix := space + " "
firstContentLineSpaceNum := len(splittedValues[1]) - len(strings.TrimLeft(splittedValues[1], " "))
prefix := strings.Repeat(" ", firstContentLineSpaceNum)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt it would be better to calculate using the number of spaces in the first line

Copy link
Contributor Author

@martin-sucha martin-sucha Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does not work if the first line is empty. I have added a test case with the first line empty. I've also discovered a separate issue #366.

for i := 1; i < len(splittedValues); i++ {
splittedValues[i] = strings.TrimLeft(splittedValues[i], " ")
splittedValues[i] = strings.TrimPrefix(splittedValues[i], prefix)
}
}
newValues := []string{trimmedFirstValue}
Expand Down
41 changes: 41 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/goccy/go-yaml/parser"
"math"
"reflect"
"strconv"
Expand Down Expand Up @@ -1399,6 +1400,46 @@ func Example_MarshalYAML() {
// field: 13
}

func TestIssue356(t *testing.T) {
tests := map[string]struct {
in string
}{
"content on first line": {
in: `args:
- |

key:
nest1: something
nest2:
nest2a: b
`,
},
"empty first line": {
in: `args:
- |

key:
nest1: something
nest2:
nest2a: b
`,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
f, err := parser.ParseBytes([]byte(test.in), 0)
if err != nil {
t.Fatalf("parse: %v", err)
}
got := f.String()
if test.in != got {
t.Fatalf("failed to encode.\nexpected:\n%s\nbut got:\n%s\n", test.in, got)
}
})
}
}

func TestMarshalIndentWithMultipleText(t *testing.T) {
t.Run("depth1", func(t *testing.T) {
b, err := yaml.MarshalWithOptions(map[string]interface{}{
Expand Down