mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-01 14:14:52 +08:00
Introduced 'format' to encapsulate encoding and decoding formats together
This commit is contained in:
@@ -9,21 +9,21 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
|
||||
func configureEncoder(format *Format, indent int) Encoder {
|
||||
|
||||
switch format {
|
||||
case JSONOutputFormat:
|
||||
case JSONFormat:
|
||||
prefs := ConfiguredJSONPreferences.Copy()
|
||||
prefs.Indent = indent
|
||||
prefs.ColorsEnabled = false
|
||||
prefs.UnwrapScalar = false
|
||||
return NewJSONEncoder(prefs)
|
||||
case YamlOutputFormat:
|
||||
case YamlFormat:
|
||||
var prefs = ConfiguredYamlPreferences.Copy()
|
||||
prefs.Indent = indent
|
||||
prefs.ColorsEnabled = false
|
||||
return NewYamlEncoder(prefs)
|
||||
case XMLOutputFormat:
|
||||
case XMLFormat:
|
||||
var xmlPrefs = ConfiguredXMLPreferences.Copy()
|
||||
xmlPrefs.Indent = indent
|
||||
return NewXMLEncoder(xmlPrefs)
|
||||
@@ -46,7 +46,7 @@ func encodeToString(candidate *CandidateNode, prefs encoderPreferences) (string,
|
||||
}
|
||||
|
||||
type encoderPreferences struct {
|
||||
format *PrinterOutputFormat
|
||||
format *Format
|
||||
indent int
|
||||
}
|
||||
|
||||
@@ -81,9 +81,9 @@ func encodeOperator(_ *dataTreeNavigator, context Context, expressionNode *Expre
|
||||
}
|
||||
|
||||
// dont print a newline when printing json on a single line.
|
||||
if (preferences.format == JSONOutputFormat && preferences.indent == 0) ||
|
||||
preferences.format == CSVOutputFormat ||
|
||||
preferences.format == TSVOutputFormat {
|
||||
if (preferences.format == JSONFormat && preferences.indent == 0) ||
|
||||
preferences.format == CSVFormat ||
|
||||
preferences.format == TSVFormat {
|
||||
stringValue = chomper.ReplaceAllString(stringValue, "")
|
||||
}
|
||||
|
||||
@@ -93,30 +93,7 @@ func encodeOperator(_ *dataTreeNavigator, context Context, expressionNode *Expre
|
||||
}
|
||||
|
||||
type decoderPreferences struct {
|
||||
format InputFormat
|
||||
}
|
||||
|
||||
func createDecoder(format InputFormat) Decoder {
|
||||
var decoder Decoder
|
||||
switch format {
|
||||
case JsonInputFormat:
|
||||
decoder = NewJSONDecoder()
|
||||
case YamlInputFormat:
|
||||
decoder = NewYamlDecoder(ConfiguredYamlPreferences)
|
||||
case XMLInputFormat:
|
||||
decoder = NewXMLDecoder(ConfiguredXMLPreferences)
|
||||
case Base64InputFormat:
|
||||
decoder = NewBase64Decoder()
|
||||
case PropertiesInputFormat:
|
||||
decoder = NewPropertiesDecoder()
|
||||
case CSVObjectInputFormat:
|
||||
decoder = NewCSVObjectDecoder(ConfiguredCsvPreferences)
|
||||
case TSVObjectInputFormat:
|
||||
decoder = NewCSVObjectDecoder(ConfiguredTsvPreferences)
|
||||
case UriInputFormat:
|
||||
decoder = NewUriDecoder()
|
||||
}
|
||||
return decoder
|
||||
format *Format
|
||||
}
|
||||
|
||||
/* takes a string and decodes it back into an object */
|
||||
@@ -124,7 +101,7 @@ func decodeOperator(_ *dataTreeNavigator, context Context, expressionNode *Expre
|
||||
|
||||
preferences := expressionNode.Operation.Preferences.(decoderPreferences)
|
||||
|
||||
decoder := createDecoder(preferences.format)
|
||||
decoder := preferences.format.DecoderFactory()
|
||||
if decoder == nil {
|
||||
return Context{}, errors.New("no support for input format")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user