Refactoring encoding configuration

This commit is contained in:
Mike Farah 2024-02-24 16:07:15 +11:00
parent 1d371b712f
commit 8f6d642012
3 changed files with 5 additions and 17 deletions

View File

@ -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

View File

@ -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
@ -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) {

View File

@ -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() }}