Skip to content

Commit

Permalink
Quote strings with special characters in flow mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpeshansky committed Nov 23, 2021
1 parent 562004d commit 450c46a
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 @@ -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
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

0 comments on commit 450c46a

Please sign in to comment.