fixed leading content logic

This commit is contained in:
Mike Farah 2022-10-28 13:50:22 +11:00
parent b87c1503f4
commit e918bec740
5 changed files with 15 additions and 7 deletions

View File

@ -75,7 +75,7 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
return err return err
} }
decoder, err := configureDecoder() decoder, err := configureDecoder(true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -97,7 +97,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
printer := yqlib.NewPrinter(encoder, printerWriter) printer := yqlib.NewPrinter(encoder, printerWriter)
decoder, err := configureDecoder() decoder, err := configureDecoder(false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -56,7 +56,7 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
return expression, args, nil return expression, args, nil
} }
func configureDecoder() (yqlib.Decoder, error) { func configureDecoder(evaluateTogether bool) (yqlib.Decoder, error) {
yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat) yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,8 +73,9 @@ func configureDecoder() (yqlib.Decoder, error) {
case yqlib.TSVObjectInputFormat: case yqlib.TSVObjectInputFormat:
return yqlib.NewCSVObjectDecoder('\t'), nil return yqlib.NewCSVObjectDecoder('\t'), nil
} }
prefs := yqlib.ConfiguredYamlPreferences
return yqlib.NewYamlDecoder(yqlib.ConfiguredYamlPreferences), nil prefs.EvaluateTogether = evaluateTogether
return yqlib.NewYamlDecoder(prefs), nil
} }
func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yqlib.PrinterWriter, error) { func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yqlib.PrinterWriter, error) {

View File

@ -16,10 +16,11 @@ type yamlDecoder struct {
prefs YamlPreferences prefs YamlPreferences
leadingContent string leadingContent string
readAnything bool readAnything bool
firstFile bool
} }
func NewYamlDecoder(prefs YamlPreferences) Decoder { 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) { 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 readerToUse := reader
leadingContent := "" leadingContent := ""
var err error 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)) readerToUse, leadingContent, err = dec.processReadStream(bufio.NewReader(reader))
if err != nil { if err != nil {
return err return err
@ -67,6 +71,7 @@ func (dec *yamlDecoder) Init(reader io.Reader) error {
dec.leadingContent = leadingContent dec.leadingContent = leadingContent
dec.readAnything = false dec.readAnything = false
dec.decoder = *yaml.NewDecoder(readerToUse) dec.decoder = *yaml.NewDecoder(readerToUse)
dec.firstFile = false
return nil return nil
} }

View File

@ -4,6 +4,7 @@ type YamlPreferences struct {
LeadingContentPreProcessing bool LeadingContentPreProcessing bool
PrintDocSeparators bool PrintDocSeparators bool
UnwrapScalar bool UnwrapScalar bool
EvaluateTogether bool
} }
func NewDefaultYamlPreferences() YamlPreferences { func NewDefaultYamlPreferences() YamlPreferences {
@ -11,6 +12,7 @@ func NewDefaultYamlPreferences() YamlPreferences {
LeadingContentPreProcessing: true, LeadingContentPreProcessing: true,
PrintDocSeparators: true, PrintDocSeparators: true,
UnwrapScalar: true, UnwrapScalar: true,
EvaluateTogether: false,
} }
} }