diff --git a/pkg/yqlib/encoder_lua.go b/pkg/yqlib/encoder_lua.go index b5816b57..8506348d 100644 --- a/pkg/yqlib/encoder_lua.go +++ b/pkg/yqlib/encoder_lua.go @@ -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 { diff --git a/pkg/yqlib/lua_test.go b/pkg/yqlib/lua_test.go index df5b98ee..6c8b9916 100644 --- a/pkg/yqlib/lua_test.go +++ b/pkg/yqlib/lua_test.go @@ -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", }, {