Skip to content

Commit

Permalink
Refactoring encoding configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 24, 2024
1 parent 1d371b7 commit 8f6d642
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/yqlib/encoder_base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (e *base64Encoder) PrintLeadingContent(_ io.Writer, _ string) error {

func (e *base64Encoder) Encode(writer io.Writer, node *CandidateNode) error {
if node.guessTagFromCustomType() != "!!str" {
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)
return fmt.Errorf("cannot encode %v as base64, can only operate on strings", node.Tag)
}
_, err := writer.Write([]byte(e.encoding.EncodeToString([]byte(node.Value))))
return err
Expand Down
14 changes: 1 addition & 13 deletions pkg/yqlib/operator_encoder_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
prefs.ColorsEnabled = false
prefs.UnwrapScalar = false
return NewJSONEncoder(prefs)
case PropsOutputFormat:
return NewPropertiesEncoder(ConfiguredPropertiesPreferences)
case CSVOutputFormat:
return NewCsvEncoder(ConfiguredCsvPreferences)
case TSVOutputFormat:
return NewCsvEncoder(ConfiguredTsvPreferences)
case YamlOutputFormat:
var prefs = ConfiguredYamlPreferences.Copy()
prefs.Indent = indent
Expand All @@ -33,14 +27,8 @@ func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
var xmlPrefs = ConfiguredXMLPreferences.Copy()
xmlPrefs.Indent = indent
return NewXMLEncoder(xmlPrefs)
case Base64OutputFormat:
return NewBase64Encoder()
case UriOutputFormat:
return NewUriEncoder()
case ShOutputFormat:
return NewShEncoder()
}
panic("invalid encoder")
return format.EncoderFactory()
}

func encodeToString(candidate *CandidateNode, prefs encoderPreferences) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/yqlib/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var CSVOutputFormat = &PrinterOutputFormat{"csv", []string{"c"}, func() Encoder
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 Base64OutputFormat = &PrinterOutputFormat{"base64", []string{}, func() Encoder { return NewBase64Encoder() }}
var UriOutputFormat = &PrinterOutputFormat{"uri", []string{}, func() Encoder { return NewUriEncoder() }}
var ShOutputFormat = &PrinterOutputFormat{"", nil, func() Encoder { return NewShEncoder() }}

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

0 comments on commit 8f6d642

Please sign in to comment.