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

Quote strings with special characters in flow mode. #270

Merged
merged 1 commit into from Oct 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions encode.go
Expand Up @@ -405,6 +405,9 @@ func (e *Encoder) isNeedQuoted(v string) bool {
if e.useLiteralStyleIfMultiline && strings.ContainsAny(v, "\n\r") {
return false
}
if e.isFlowStyle && strings.ContainsAny(v, `]},'"`) {
return true
}
if token.IsNeedQuoted(v) {
return true
}
Expand Down Expand Up @@ -623,19 +626,18 @@ func (e *Encoder) encodeStruct(ctx context.Context, value reflect.Value, column
// omit encoding
continue
}
value, err := e.encodeValue(ctx, fieldValue, column)
ve := e
Copy link
Owner

@goccy goccy Jan 11, 2022

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apologies, I completely missed this. This is to ensure that the value is encoded with flow style even f the base encoder doesn't set it, without modifying the base encoder. Should I add a comment to that effect?

Copy link
Owner

Choose a reason for hiding this comment

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

I see ! Thank you for the reply.

if !e.isFlowStyle && structField.IsFlow {
ve = &Encoder{}
*ve = *e
ve.isFlowStyle = true
}
value, err := ve.encodeValue(ctx, fieldValue, column)
if err != nil {
return nil, errors.Wrapf(err, "failed to encode value")
}
if m, ok := value.(*ast.MappingNode); ok {
if !e.isFlowStyle && structField.IsFlow {
m.SetIsFlowStyle(true)
}
if _, ok := value.(*ast.MappingNode); ok {
value.AddColumn(e.indent)
} else if s, ok := value.(*ast.SequenceNode); ok {
if !e.isFlowStyle && structField.IsFlow {
s.SetIsFlowStyle(true)
}
}
key := e.encodeString(structField.RenderName, column)
switch {
Expand Down
82 changes: 82 additions & 0 deletions encode_test.go
Expand Up @@ -548,6 +548,88 @@ func TestEncoder(t *testing.T) {
}{struct{ B, D string }{"c", "e"}},
nil,
},
// Quoting in flow mode
{
`a: [b, "c,d", e]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", "c,d", "e"}},
[]yaml.EncodeOption{
yaml.UseSingleQuote(false),
},
},
{
`a: [b, "c]", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", "c]", "d"}},
[]yaml.EncodeOption{
yaml.UseSingleQuote(false),
},
},
{
`a: [b, "c}", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", "c}", "d"}},
[]yaml.EncodeOption{
yaml.UseSingleQuote(false),
},
},
{
`a: [b, "c\"", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", `c"`, "d"}},
[]yaml.EncodeOption{
yaml.UseSingleQuote(false),
},
},
{
`a: [b, "c'", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", "c'", "d"}},
[]yaml.EncodeOption{
yaml.UseSingleQuote(false),
},
},
// No quoting in non-flow mode
{
"a:\n- b\n- c,d\n- e\n",
struct {
A []string `yaml:"a"`
}{[]string{"b", "c,d", "e"}},
nil,
},
{
`a: [b, "c]", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", "c]", "d"}},
nil,
},
{
`a: [b, "c}", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", "c}", "d"}},
nil,
},
{
`a: [b, "c\"", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", `c"`, "d"}},
nil,
},
{
`a: [b, "c'", d]` + "\n",
struct {
A []string `yaml:"a,flow"`
}{[]string{"b", "c'", "d"}},
nil,
},

// Multi bytes
{
Expand Down