mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-10 08:35:42 +00:00
Respect string Style in Lua encoder
Lua has 'single', "double" and [[ long ]] strings.
This commit is contained in:
parent
0debfc3cf9
commit
11605f6b63
@ -80,7 +80,28 @@ func (le *luaEncoder) PrintLeadingContent(writer io.Writer, content string) erro
|
||||
}
|
||||
|
||||
func (le *luaEncoder) encodeString(writer io.Writer, node *yaml.Node) error {
|
||||
return writeString(writer, "\""+le.escape.Replace(node.Value)+"\"")
|
||||
quote := "\""
|
||||
switch node.Style {
|
||||
case yaml.LiteralStyle, yaml.FoldedStyle, yaml.FlowStyle:
|
||||
for i := 0; i < 10; i++ {
|
||||
if !strings.Contains(node.Value, "]"+strings.Repeat("=", i)+"]") {
|
||||
err := writeString(writer, "["+strings.Repeat("=", i)+"[\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = writeString(writer, node.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeString(writer, "]"+strings.Repeat("=", i)+"]")
|
||||
}
|
||||
}
|
||||
case yaml.SingleQuotedStyle:
|
||||
quote = "'"
|
||||
|
||||
// falltrough to regular ol' string
|
||||
}
|
||||
return writeString(writer, quote+le.escape.Replace(node.Value)+quote)
|
||||
}
|
||||
|
||||
func (le *luaEncoder) writeIndent(writer io.Writer) error {
|
||||
|
||||
@ -47,8 +47,8 @@ numbers: [123,456]
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Scalar str",
|
||||
input: "str: |\n foo\n bar\n",
|
||||
expected: "return {\n\t[\"str\"] = \"foo\\nbar\\n\";\n};\n",
|
||||
input: "str: |\n foo\n bar\nanother: 'single'\nand: \"double\"",
|
||||
expected: "return {\n\t[\"str\"] = [[\nfoo\nbar\n]];\n\t[\"another\"] = 'single';\n\t[\"and\"] = \"double\";\n};\n",
|
||||
scenarioType: "encode",
|
||||
},
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user