From e918bec740efeba88d96ae551dca3dbe45a19b21 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 28 Oct 2022 13:50:22 +1100 Subject: [PATCH] fixed leading content logic --- cmd/evaluate_all_command.go | 2 +- cmd/evalute_sequence_command.go | 2 +- cmd/utils.go | 7 ++++--- pkg/yqlib/decoder_yaml.go | 9 +++++++-- pkg/yqlib/yaml.go | 2 ++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index fc761fdc..63b68871 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -75,7 +75,7 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) { return err } - decoder, err := configureDecoder() + decoder, err := configureDecoder(true) if err != nil { return err } diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index 3a352c18..53108165 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -97,7 +97,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) { printer := yqlib.NewPrinter(encoder, printerWriter) - decoder, err := configureDecoder() + decoder, err := configureDecoder(false) if err != nil { return err } diff --git a/cmd/utils.go b/cmd/utils.go index 0d303ff5..c6f5b2ab 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -56,7 +56,7 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) { return expression, args, nil } -func configureDecoder() (yqlib.Decoder, error) { +func configureDecoder(evaluateTogether bool) (yqlib.Decoder, error) { yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat) if err != nil { return nil, err @@ -73,8 +73,9 @@ func configureDecoder() (yqlib.Decoder, error) { case yqlib.TSVObjectInputFormat: return yqlib.NewCSVObjectDecoder('\t'), nil } - - return yqlib.NewYamlDecoder(yqlib.ConfiguredYamlPreferences), nil + prefs := yqlib.ConfiguredYamlPreferences + prefs.EvaluateTogether = evaluateTogether + return yqlib.NewYamlDecoder(prefs), nil } func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yqlib.PrinterWriter, error) { diff --git a/pkg/yqlib/decoder_yaml.go b/pkg/yqlib/decoder_yaml.go index d5442d77..207ec01e 100644 --- a/pkg/yqlib/decoder_yaml.go +++ b/pkg/yqlib/decoder_yaml.go @@ -16,10 +16,11 @@ type yamlDecoder struct { prefs YamlPreferences leadingContent string readAnything bool + firstFile bool } func NewYamlDecoder(prefs YamlPreferences) Decoder { - return &yamlDecoder{prefs: prefs} + return &yamlDecoder{prefs: prefs, firstFile: true} } func (dec *yamlDecoder) processReadStream(reader *bufio.Reader) (io.Reader, string, error) { @@ -58,7 +59,10 @@ func (dec *yamlDecoder) Init(reader io.Reader) error { readerToUse := reader leadingContent := "" var err error - if dec.prefs.LeadingContentPreProcessing { + // if we 'evaluating together' - we only process the leading content + // of the first file - this ensures comments from subsequent files are + // merged together correctly. + if dec.prefs.LeadingContentPreProcessing && (!dec.prefs.EvaluateTogether || dec.firstFile) { readerToUse, leadingContent, err = dec.processReadStream(bufio.NewReader(reader)) if err != nil { return err @@ -67,6 +71,7 @@ func (dec *yamlDecoder) Init(reader io.Reader) error { dec.leadingContent = leadingContent dec.readAnything = false dec.decoder = *yaml.NewDecoder(readerToUse) + dec.firstFile = false return nil } diff --git a/pkg/yqlib/yaml.go b/pkg/yqlib/yaml.go index 068e1ead..d7c8863a 100644 --- a/pkg/yqlib/yaml.go +++ b/pkg/yqlib/yaml.go @@ -4,6 +4,7 @@ type YamlPreferences struct { LeadingContentPreProcessing bool PrintDocSeparators bool UnwrapScalar bool + EvaluateTogether bool } func NewDefaultYamlPreferences() YamlPreferences { @@ -11,6 +12,7 @@ func NewDefaultYamlPreferences() YamlPreferences { LeadingContentPreProcessing: true, PrintDocSeparators: true, UnwrapScalar: true, + EvaluateTogether: false, } }