Introduced 'format' to encapsulate encoding and decoding formats together

This commit is contained in:
Mike Farah
2024-02-24 16:37:13 +11:00
parent 8f6d642012
commit 447bf28cd8
11 changed files with 253 additions and 250 deletions
+10 -33
View File
@@ -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")
}