Implement basic Lua output support

Ref #1700

Basic but working serialization to Lua tables.
This commit is contained in:
Kim Alvefur 2023-07-30 20:22:24 +02:00
parent 9b4082919b
commit 07b53fca7d
3 changed files with 128 additions and 1 deletions

View File

@ -197,6 +197,8 @@ func createEncoder(format yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
return yqlib.NewTomlEncoder(), nil return yqlib.NewTomlEncoder(), nil
case yqlib.ShellVariablesOutputFormat: case yqlib.ShellVariablesOutputFormat:
return yqlib.NewShellVariablesEncoder(), nil return yqlib.NewShellVariablesEncoder(), nil
case yqlib.LuaOutputFormat:
return yqlib.NewLuaEncoder(), nil
} }
return nil, fmt.Errorf("invalid encoder: %v", format) return nil, fmt.Errorf("invalid encoder: %v", format)
} }

122
pkg/yqlib/encoder_lua.go Normal file
View File

@ -0,0 +1,122 @@
package yqlib
import (
"io"
"strings"
yaml "gopkg.in/yaml.v3"
)
type luaEncoder struct {
docPrefix string
docSuffix string
escape *strings.Replacer
}
func (le *luaEncoder) CanHandleAliases() bool {
return false
}
func NewLuaEncoder() Encoder {
escape := strings.NewReplacer("\n", "\\n", "\"", "\\\"", "\\", "\\\\")
return &luaEncoder{"return ", ";\n", escape}
}
func (le *luaEncoder) PrintDocumentSeparator(writer io.Writer) error {
return nil
}
func (le *luaEncoder) PrintLeadingContent(writer io.Writer, content string) error {
return nil
}
func (le *luaEncoder) encodeString(writer io.Writer, node *yaml.Node) error {
return writeString(writer, "\""+le.escape.Replace(node.Value)+"\"")
}
func (le *luaEncoder) encodeArray(writer io.Writer, node *yaml.Node) error {
err := writeString(writer, "{")
if err != nil {
return err
}
for _, child := range node.Content {
err := le.Encode(writer, child)
if err != nil {
return err
}
err = writeString(writer, ",")
if err != nil {
return err
}
}
return writeString(writer, "}")
}
func (le *luaEncoder) encodeMap(writer io.Writer, node *yaml.Node) error {
err := writeString(writer, "{")
if err != nil {
return err
}
for i, child := range node.Content {
if (i % 2) == 1 {
// value
err = le.Encode(writer, child)
if err != nil {
return err
}
err = writeString(writer, ";")
if err != nil {
return err
}
} else {
// key
err := writeString(writer, "[")
if err != nil {
return err
}
err = le.encodeAny(writer, child)
if err != nil {
return err
}
err = writeString(writer, "]=")
if err != nil {
return err
}
}
}
return writeString(writer, "}")
}
func (le *luaEncoder) encodeAny(writer io.Writer, node *yaml.Node) error {
switch node.Kind {
case yaml.SequenceNode:
return le.encodeArray(writer, node)
case yaml.MappingNode:
return le.encodeMap(writer, node)
case yaml.ScalarNode:
switch node.Tag {
case "!!str":
return le.encodeString(writer, node)
case "!!null":
return writeString(writer, "nil")
default:
return writeString(writer, node.Value)
}
case yaml.DocumentNode:
err := writeString(writer, le.docPrefix)
if err != nil {
return err
}
err = le.encodeAny(writer, node.Content[0])
if err != nil {
return err
}
return writeString(writer, le.docSuffix)
default:
return writeString(writer, "nil --[[ encoder NYI -- "+node.ShortTag()+" ]]")
}
}
func (le *luaEncoder) Encode(writer io.Writer, node *yaml.Node) error {
return le.encodeAny(writer, node)
}

View File

@ -33,6 +33,7 @@ const (
ShOutputFormat ShOutputFormat
TomlOutputFormat TomlOutputFormat
ShellVariablesOutputFormat ShellVariablesOutputFormat
LuaOutputFormat
) )
func OutputFormatFromString(format string) (PrinterOutputFormat, error) { func OutputFormatFromString(format string) (PrinterOutputFormat, error) {
@ -53,8 +54,10 @@ func OutputFormatFromString(format string) (PrinterOutputFormat, error) {
return TomlOutputFormat, nil return TomlOutputFormat, nil
case "shell", "s", "sh": case "shell", "s", "sh":
return ShellVariablesOutputFormat, nil return ShellVariablesOutputFormat, nil
case "lua", "l":
return LuaOutputFormat, nil
default: default:
return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml|toml|shell]", format) return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml|toml|shell|lua]", format)
} }
} }