Skip to content

Commit 8f6d642

Browse files
committed
Refactoring encoding configuration
1 parent 1d371b7 commit 8f6d642

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

pkg/yqlib/encoder_base64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (e *base64Encoder) PrintLeadingContent(_ io.Writer, _ string) error {
2828

2929
func (e *base64Encoder) Encode(writer io.Writer, node *CandidateNode) error {
3030
if node.guessTagFromCustomType() != "!!str" {
31-
return fmt.Errorf("cannot encode %v as base64, can only operate on strings. Please first pipe through another encoding operator to convert the value to a string", node.Tag)
31+
return fmt.Errorf("cannot encode %v as base64, can only operate on strings", node.Tag)
3232
}
3333
_, err := writer.Write([]byte(e.encoding.EncodeToString([]byte(node.Value))))
3434
return err

pkg/yqlib/operator_encoder_decoder.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
1818
prefs.ColorsEnabled = false
1919
prefs.UnwrapScalar = false
2020
return NewJSONEncoder(prefs)
21-
case PropsOutputFormat:
22-
return NewPropertiesEncoder(ConfiguredPropertiesPreferences)
23-
case CSVOutputFormat:
24-
return NewCsvEncoder(ConfiguredCsvPreferences)
25-
case TSVOutputFormat:
26-
return NewCsvEncoder(ConfiguredTsvPreferences)
2721
case YamlOutputFormat:
2822
var prefs = ConfiguredYamlPreferences.Copy()
2923
prefs.Indent = indent
@@ -33,14 +27,8 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
3327
var xmlPrefs = ConfiguredXMLPreferences.Copy()
3428
xmlPrefs.Indent = indent
3529
return NewXMLEncoder(xmlPrefs)
36-
case Base64OutputFormat:
37-
return NewBase64Encoder()
38-
case UriOutputFormat:
39-
return NewUriEncoder()
40-
case ShOutputFormat:
41-
return NewShEncoder()
4230
}
43-
panic("invalid encoder")
31+
return format.EncoderFactory()
4432
}
4533

4634
func encodeToString(candidate *CandidateNode, prefs encoderPreferences) (string, error) {

pkg/yqlib/printer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}, func() Encoder
3333
var TSVOutputFormat = &PrinterOutputFormat{"tsv", []string{"t"}, func() Encoder { return NewCsvEncoder(ConfiguredTsvPreferences) }}
3434
var XMLOutputFormat = &PrinterOutputFormat{"xml", []string{"x"}, func() Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) }}
3535

36-
var Base64OutputFormat = &PrinterOutputFormat{}
37-
var UriOutputFormat = &PrinterOutputFormat{}
38-
var ShOutputFormat = &PrinterOutputFormat{}
36+
var Base64OutputFormat = &PrinterOutputFormat{"base64", []string{}, func() Encoder { return NewBase64Encoder() }}
37+
var UriOutputFormat = &PrinterOutputFormat{"uri", []string{}, func() Encoder { return NewUriEncoder() }}
38+
var ShOutputFormat = &PrinterOutputFormat{"", nil, func() Encoder { return NewShEncoder() }}
3939

4040
var TomlOutputFormat = &PrinterOutputFormat{"toml", []string{}, func() Encoder { return NewTomlEncoder() }}
4141
var ShellVariablesOutputFormat = &PrinterOutputFormat{"shell", []string{"s", "sh"}, func() Encoder { return NewShellVariablesEncoder() }}

0 commit comments

Comments
 (0)