mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
Refactoring encoding configuration
This commit is contained in:
parent
55f6a3a49d
commit
1d371b712f
30
cmd/utils.go
30
cmd/utils.go
@ -180,38 +180,22 @@ func configureEncoder() (yqlib.Encoder, error) {
|
||||
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
|
||||
yqlib.ConfiguredXMLPreferences.Indent = indent
|
||||
yqlib.ConfiguredYamlPreferences.Indent = indent
|
||||
yqlib.ConfiguredJsonPreferences.Indent = indent
|
||||
yqlib.ConfiguredJSONPreferences.Indent = indent
|
||||
|
||||
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar
|
||||
yqlib.ConfiguredPropertiesPreferences.UnwrapScalar = unwrapScalar
|
||||
yqlib.ConfiguredJsonPreferences.UnwrapScalar = unwrapScalar
|
||||
yqlib.ConfiguredJSONPreferences.UnwrapScalar = unwrapScalar
|
||||
|
||||
yqlib.ConfiguredYamlPreferences.ColorsEnabled = colorsEnabled
|
||||
yqlib.ConfiguredJsonPreferences.ColorsEnabled = colorsEnabled
|
||||
yqlib.ConfiguredJSONPreferences.ColorsEnabled = colorsEnabled
|
||||
|
||||
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
|
||||
|
||||
switch format {
|
||||
case yqlib.JSONOutputFormat:
|
||||
return yqlib.NewJSONEncoder(yqlib.ConfiguredJsonPreferences), nil
|
||||
case yqlib.PropsOutputFormat:
|
||||
return yqlib.NewPropertiesEncoder(yqlib.ConfiguredPropertiesPreferences), nil
|
||||
case yqlib.CSVOutputFormat:
|
||||
return yqlib.NewCsvEncoder(yqlib.ConfiguredCsvPreferences), nil
|
||||
case yqlib.TSVOutputFormat:
|
||||
return yqlib.NewCsvEncoder(yqlib.ConfiguredTsvPreferences), nil
|
||||
case yqlib.YamlOutputFormat:
|
||||
return yqlib.NewYamlEncoder(yqlib.ConfiguredYamlPreferences), nil
|
||||
case yqlib.XMLOutputFormat:
|
||||
return yqlib.NewXMLEncoder(yqlib.ConfiguredXMLPreferences), nil
|
||||
case yqlib.TomlOutputFormat:
|
||||
return yqlib.NewTomlEncoder(), nil
|
||||
case yqlib.ShellVariablesOutputFormat:
|
||||
return yqlib.NewShellVariablesEncoder(), nil
|
||||
case yqlib.LuaOutputFormat:
|
||||
return yqlib.NewLuaEncoder(yqlib.ConfiguredLuaPreferences), nil
|
||||
}
|
||||
encoder := format.EncoderFactory()
|
||||
if encoder == nil {
|
||||
return nil, fmt.Errorf("invalid encoder: %v", format)
|
||||
}
|
||||
return encoder, nil
|
||||
}
|
||||
|
||||
// this is a hack to enable backwards compatibility with githubactions (which pipe /dev/null into everything)
|
||||
|
@ -16,7 +16,7 @@ func yamlToJSON(t *testing.T, sampleYaml string, indent int) string {
|
||||
var output bytes.Buffer
|
||||
writer := bufio.NewWriter(&output)
|
||||
|
||||
prefs := ConfiguredJsonPreferences.Copy()
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = indent
|
||||
prefs.UnwrapScalar = false
|
||||
var jsonEncoder = NewJSONEncoder(prefs)
|
||||
|
@ -71,7 +71,7 @@ func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) {
|
||||
encoder := NewYamlEncoder(ConfiguredYamlPreferences)
|
||||
|
||||
if s.scenarioType == "shebang-json" {
|
||||
encoder = NewJSONEncoder(ConfiguredJsonPreferences)
|
||||
encoder = NewJSONEncoder(ConfiguredJSONPreferences)
|
||||
}
|
||||
|
||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), encoder)))
|
||||
|
@ -22,4 +22,4 @@ func (p *JsonPreferences) Copy() JsonPreferences {
|
||||
}
|
||||
}
|
||||
|
||||
var ConfiguredJsonPreferences = NewDefaultJsonPreferences()
|
||||
var ConfiguredJSONPreferences = NewDefaultJsonPreferences()
|
||||
|
@ -328,7 +328,7 @@ func documentRoundtripNdJsonScenario(w *bufio.Writer, s formatScenario, indent i
|
||||
}
|
||||
|
||||
writeOrPanic(w, "will output\n")
|
||||
prefs := ConfiguredJsonPreferences.Copy()
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = indent
|
||||
prefs.UnwrapScalar = false
|
||||
|
||||
@ -386,7 +386,7 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
|
||||
}
|
||||
|
||||
func testJSONScenario(t *testing.T, s formatScenario) {
|
||||
prefs := ConfiguredJsonPreferences.Copy()
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = s.indent
|
||||
prefs.UnwrapScalar = false
|
||||
switch s.scenarioType {
|
||||
@ -488,7 +488,7 @@ func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=json -I=%v '%v' sample.yml\n```\n", s.indent, expression))
|
||||
}
|
||||
writeOrPanic(w, "will output\n")
|
||||
prefs := ConfiguredJsonPreferences.Copy()
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = s.indent
|
||||
prefs.UnwrapScalar = false
|
||||
|
||||
|
@ -13,7 +13,7 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
|
||||
|
||||
switch format {
|
||||
case JSONOutputFormat:
|
||||
prefs := ConfiguredJsonPreferences.Copy()
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = indent
|
||||
prefs.ColorsEnabled = false
|
||||
prefs.UnwrapScalar = false
|
||||
|
@ -34,7 +34,7 @@ type expressionScenario struct {
|
||||
func TestMain(m *testing.M) {
|
||||
logging.SetLevel(logging.ERROR, "")
|
||||
ConfiguredYamlPreferences.ColorsEnabled = false
|
||||
ConfiguredJsonPreferences.ColorsEnabled = false
|
||||
ConfiguredJSONPreferences.ColorsEnabled = false
|
||||
Now = func() time.Time {
|
||||
return time.Date(2021, time.May, 19, 1, 2, 3, 4, time.UTC)
|
||||
}
|
||||
|
@ -18,26 +18,29 @@ type Printer interface {
|
||||
SetNulSepOutput(nulSepOutput bool)
|
||||
}
|
||||
|
||||
type EncoderFactoryFunction func() Encoder
|
||||
|
||||
type PrinterOutputFormat struct {
|
||||
FormalName string
|
||||
Names []string
|
||||
EncoderFactory EncoderFactoryFunction
|
||||
}
|
||||
|
||||
var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}}
|
||||
var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}}
|
||||
var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}}
|
||||
var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}}
|
||||
var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}}
|
||||
var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}}
|
||||
var YamlOutputFormat = &PrinterOutputFormat{"yaml", []string{"y", "yml"}, func() Encoder { return NewYamlEncoder(ConfiguredYamlPreferences) }}
|
||||
var JSONOutputFormat = &PrinterOutputFormat{"json", []string{"j"}, func() Encoder { return NewJSONEncoder(ConfiguredJSONPreferences) }}
|
||||
var PropsOutputFormat = &PrinterOutputFormat{"props", []string{"p", "properties"}, func() Encoder { return NewPropertiesEncoder(ConfiguredPropertiesPreferences) }}
|
||||
var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}, func() Encoder { return NewCsvEncoder(ConfiguredCsvPreferences) }}
|
||||
var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}, func() Encoder { return NewCsvEncoder(ConfiguredTsvPreferences) }}
|
||||
var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}, func() Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) }}
|
||||
|
||||
var Base64OutputFormat = &PrinterOutputFormat{}
|
||||
var UriOutputFormat = &PrinterOutputFormat{}
|
||||
var ShOutputFormat = &PrinterOutputFormat{}
|
||||
|
||||
var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}}
|
||||
var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}}
|
||||
var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}, func() Encoder { return NewTomlEncoder() }}
|
||||
var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}, func() Encoder { return NewShellVariablesEncoder() }}
|
||||
|
||||
var LuaOutputFormat = &PrinterOutputFormat{"lua", []string{"l"}}
|
||||
var LuaOutputFormat = &PrinterOutputFormat{"lua", []string{"l"}, func() Encoder { return NewLuaEncoder(ConfiguredLuaPreferences) }}
|
||||
|
||||
var Formats = []*PrinterOutputFormat{
|
||||
YamlOutputFormat,
|
||||
@ -66,6 +69,10 @@ func (f *PrinterOutputFormat) MatchesName(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *PrinterOutputFormat) GetConfiguredEncoder() Encoder {
|
||||
return f.EncoderFactory()
|
||||
}
|
||||
|
||||
func OutputFormatFromString(format string) (*PrinterOutputFormat, error) {
|
||||
for _, printerFormat := range Formats {
|
||||
if printerFormat.MatchesName(format) {
|
||||
|
@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
|
||||
var writer = bufio.NewWriter(&output)
|
||||
// note printDocSeparators is true, it should still not print document separators
|
||||
// when outputting JSON.
|
||||
prefs := ConfiguredJsonPreferences.Copy()
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = 0
|
||||
encoder := NewJSONEncoder(prefs)
|
||||
if encoder == nil {
|
||||
@ -368,7 +368,7 @@ func TestPrinterNulSeparatorWithJson(t *testing.T) {
|
||||
var writer = bufio.NewWriter(&output)
|
||||
// note printDocSeparators is true, it should still not print document separators
|
||||
// when outputting JSON.
|
||||
prefs := ConfiguredJsonPreferences.Copy()
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = 0
|
||||
encoder := NewJSONEncoder(prefs)
|
||||
if encoder == nil {
|
||||
|
Loading…
Reference in New Issue
Block a user