mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-11 01:09:54 +00:00
Refactor encoder and decoder creation
Use more consistent parameters vs globals Return errors instead of calling panic()
This commit is contained in:
parent
0c2d05d33f
commit
ad456c1846
@ -84,7 +84,10 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
encoder := configureEncoder(format)
|
encoder, err := configureEncoder()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
printer := yqlib.NewPrinter(encoder, printerWriter)
|
printer := yqlib.NewPrinter(encoder, printerWriter)
|
||||||
|
|
||||||
|
|||||||
@ -93,7 +93,10 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
encoder := configureEncoder(format)
|
encoder, err := configureEncoder()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
printer := yqlib.NewPrinter(encoder, printerWriter)
|
printer := yqlib.NewPrinter(encoder, printerWriter)
|
||||||
|
|
||||||
|
|||||||
36
cmd/utils.go
36
cmd/utils.go
@ -61,7 +61,12 @@ func configureDecoder(evaluateTogether bool) (yqlib.Decoder, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
switch yqlibInputFormat {
|
yqlibDecoder, err := createDecoder(yqlibInputFormat, evaluateTogether)
|
||||||
|
return yqlibDecoder, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDecoder(format yqlib.InputFormat, evaluateTogether bool) (yqlib.Decoder, error) {
|
||||||
|
switch format {
|
||||||
case yqlib.XMLInputFormat:
|
case yqlib.XMLInputFormat:
|
||||||
return yqlib.NewXMLDecoder(yqlib.ConfiguredXMLPreferences), nil
|
return yqlib.NewXMLDecoder(yqlib.ConfiguredXMLPreferences), nil
|
||||||
case yqlib.PropertiesInputFormat:
|
case yqlib.PropertiesInputFormat:
|
||||||
@ -72,11 +77,13 @@ func configureDecoder(evaluateTogether bool) (yqlib.Decoder, error) {
|
|||||||
return yqlib.NewCSVObjectDecoder(','), nil
|
return yqlib.NewCSVObjectDecoder(','), nil
|
||||||
case yqlib.TSVObjectInputFormat:
|
case yqlib.TSVObjectInputFormat:
|
||||||
return yqlib.NewCSVObjectDecoder('\t'), nil
|
return yqlib.NewCSVObjectDecoder('\t'), nil
|
||||||
}
|
case yqlib.YamlInputFormat:
|
||||||
prefs := yqlib.ConfiguredYamlPreferences
|
prefs := yqlib.ConfiguredYamlPreferences
|
||||||
prefs.EvaluateTogether = evaluateTogether
|
prefs.EvaluateTogether = evaluateTogether
|
||||||
return yqlib.NewYamlDecoder(prefs), nil
|
return yqlib.NewYamlDecoder(prefs), nil
|
||||||
}
|
}
|
||||||
|
return nil, fmt.Errorf("invalid decoder: %v", format)
|
||||||
|
}
|
||||||
|
|
||||||
func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yqlib.PrinterWriter, error) {
|
func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yqlib.PrinterWriter, error) {
|
||||||
|
|
||||||
@ -95,22 +102,31 @@ func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yq
|
|||||||
return printerWriter, nil
|
return printerWriter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder {
|
func configureEncoder() (yqlib.Encoder, error) {
|
||||||
|
yqlibOutputFormat, err := yqlib.OutputFormatFromString(outputFormat)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
yqlibEncoder, err := createEncoder(yqlibOutputFormat)
|
||||||
|
return yqlibEncoder, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func createEncoder(format yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
|
||||||
switch format {
|
switch format {
|
||||||
case yqlib.JSONOutputFormat:
|
case yqlib.JSONOutputFormat:
|
||||||
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar)
|
return yqlib.NewJSONEncoder(indent, colorsEnabled, unwrapScalar), nil
|
||||||
case yqlib.PropsOutputFormat:
|
case yqlib.PropsOutputFormat:
|
||||||
return yqlib.NewPropertiesEncoder(unwrapScalar)
|
return yqlib.NewPropertiesEncoder(unwrapScalar), nil
|
||||||
case yqlib.CSVOutputFormat:
|
case yqlib.CSVOutputFormat:
|
||||||
return yqlib.NewCsvEncoder(',')
|
return yqlib.NewCsvEncoder(','), nil
|
||||||
case yqlib.TSVOutputFormat:
|
case yqlib.TSVOutputFormat:
|
||||||
return yqlib.NewCsvEncoder('\t')
|
return yqlib.NewCsvEncoder('\t'), nil
|
||||||
case yqlib.YamlOutputFormat:
|
case yqlib.YamlOutputFormat:
|
||||||
return yqlib.NewYamlEncoder(indent, colorsEnabled, yqlib.ConfiguredYamlPreferences)
|
return yqlib.NewYamlEncoder(indent, colorsEnabled, yqlib.ConfiguredYamlPreferences), nil
|
||||||
case yqlib.XMLOutputFormat:
|
case yqlib.XMLOutputFormat:
|
||||||
return yqlib.NewXMLEncoder(indent, yqlib.ConfiguredXMLPreferences)
|
return yqlib.NewXMLEncoder(indent, yqlib.ConfiguredXMLPreferences), nil
|
||||||
}
|
}
|
||||||
panic("invalid encoder")
|
return nil, fmt.Errorf("invalid encoder: %v", format)
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is a hack to enable backwards compatibility with githubactions (which pipe /dev/null into everything)
|
// this is a hack to enable backwards compatibility with githubactions (which pipe /dev/null into everything)
|
||||||
|
|||||||
@ -98,13 +98,11 @@ type decoderPreferences struct {
|
|||||||
format InputFormat
|
format InputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
/* takes a string and decodes it back into an object */
|
func createDecoder(format InputFormat) Decoder {
|
||||||
func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
||||||
|
|
||||||
preferences := expressionNode.Operation.Preferences.(decoderPreferences)
|
|
||||||
|
|
||||||
var decoder Decoder
|
var decoder Decoder
|
||||||
switch preferences.format {
|
switch format {
|
||||||
|
case JsonInputFormat:
|
||||||
|
decoder = NewJSONDecoder()
|
||||||
case YamlInputFormat:
|
case YamlInputFormat:
|
||||||
decoder = NewYamlDecoder(ConfiguredYamlPreferences)
|
decoder = NewYamlDecoder(ConfiguredYamlPreferences)
|
||||||
case XMLInputFormat:
|
case XMLInputFormat:
|
||||||
@ -120,6 +118,15 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
|
|||||||
case UriInputFormat:
|
case UriInputFormat:
|
||||||
decoder = NewUriDecoder()
|
decoder = NewUriDecoder()
|
||||||
}
|
}
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
|
|
||||||
|
/* takes a string and decodes it back into an object */
|
||||||
|
func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
|
|
||||||
|
preferences := expressionNode.Operation.Preferences.(decoderPreferences)
|
||||||
|
|
||||||
|
decoder := createDecoder(preferences.format)
|
||||||
|
|
||||||
var results = list.New()
|
var results = list.New()
|
||||||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
||||||
|
|||||||
@ -314,7 +314,8 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
|
|||||||
var writer = bufio.NewWriter(&output)
|
var writer = bufio.NewWriter(&output)
|
||||||
// note printDocSeparators is true, it should still not print document separators
|
// note printDocSeparators is true, it should still not print document separators
|
||||||
// when outputing JSON.
|
// when outputing JSON.
|
||||||
printer := NewPrinter(NewJSONEncoder(0, false, false), NewSinglePrinterWriter(writer))
|
encoder := NewJSONEncoder(0, false, false)
|
||||||
|
printer := NewPrinter(encoder, NewSinglePrinterWriter(writer))
|
||||||
|
|
||||||
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
|
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user