Skip to content

Commit

Permalink
Respect string Style in Lua encoder
Browse files Browse the repository at this point in the history
Lua has 'single', "double" and [[ long ]] strings.
  • Loading branch information
Zash committed Aug 4, 2023
1 parent 9c8ce70 commit f5715ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 22 additions & 1 deletion pkg/yqlib/encoder_lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
{
Expand Down

0 comments on commit f5715ee

Please sign in to comment.