Skip to content

Commit

Permalink
Merge pull request #270 from igorpeshansky/fix-flow-mode-quotes
Browse files Browse the repository at this point in the history
Quote strings with special characters in flow mode.
  • Loading branch information
goccy committed Oct 26, 2022
2 parents 8607d4f + 450c46a commit bc437e1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
20 changes: 11 additions & 9 deletions encode.go
Expand Up @@ -411,6 +411,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 @@ -629,19 +632,18 @@ func (e *Encoder) encodeStruct(ctx context.Context, value reflect.Value, column
// omit encoding
continue
}
value, err := e.encodeValue(ctx, fieldValue, column)
ve := e
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)
}
}
var key ast.MapKeyNode = 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

0 comments on commit bc437e1

Please sign in to comment.