Hook up settings for Lua output

This commit is contained in:
Kim Alvefur 2023-07-31 08:25:40 +02:00
parent cabb73b12c
commit c4de13e874
4 changed files with 24 additions and 3 deletions

View File

@ -78,6 +78,10 @@ yq -P sample.json
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", yqlib.ConfiguredXMLPreferences.SkipProcInst, "skip over process instructions (e.g. <?xml version=\"1\"?>)") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", yqlib.ConfiguredXMLPreferences.SkipProcInst, "skip over process instructions (e.g. <?xml version=\"1\"?>)")
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. <!DOCTYPE thing cat>)") rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. <!DOCTYPE thing cat>)")
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredLuaPreferences.DocPrefix, "lua-prefix", yqlib.ConfiguredLuaPreferences.DocPrefix, "prefix")
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredLuaPreferences.DocSuffix, "lua-suffix", yqlib.ConfiguredLuaPreferences.DocSuffix, "suffix")
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredLuaPreferences.UnquotedKeys, "lua-unquoted-keys", yqlib.ConfiguredLuaPreferences.UnquotedKeys, "output unquoted string keys (e.g. {foo=\"bar\"})")
rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.") rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.")
rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)") rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)")

View File

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

View File

@ -18,7 +18,7 @@ func (le *luaEncoder) CanHandleAliases() bool {
return false return false
} }
func NewLuaEncoder() Encoder { func NewLuaEncoder(prefs LuaPreferences) Encoder {
escape := strings.NewReplacer( escape := strings.NewReplacer(
"\000", "\\000", "\000", "\\000",
"\001", "\\001", "\001", "\\001",
@ -57,7 +57,7 @@ func NewLuaEncoder() Encoder {
"\\", "\\\\", "\\", "\\\\",
"\177", "\\127", "\177", "\\127",
) )
return &luaEncoder{"return ", ";\n", false, escape} return &luaEncoder{prefs.DocPrefix, prefs.DocSuffix, prefs.UnquotedKeys, escape}
} }
func (le *luaEncoder) PrintDocumentSeparator(writer io.Writer) error { func (le *luaEncoder) PrintDocumentSeparator(writer io.Writer) error {

17
pkg/yqlib/lua.go Normal file
View File

@ -0,0 +1,17 @@
package yqlib
type LuaPreferences struct {
DocPrefix string
DocSuffix string
UnquotedKeys bool
}
func NewDefaultLuaPreferences() LuaPreferences {
return LuaPreferences{
DocPrefix: "return ",
DocSuffix: ";\n",
UnquotedKeys: false,
}
}
var ConfiguredLuaPreferences = NewDefaultLuaPreferences()