Restore --quote-all flag for CSV output

This commit is contained in:
John Kerl 2022-08-22 09:17:37 -04:00
parent a28554216a
commit b27dcddfe3
4 changed files with 19 additions and 8 deletions

View file

@ -519,11 +519,6 @@ var LegacyFlagSection = FlagSection{
parser: NoOpParse1,
},
{
name: "--quote-all",
help: "Ignored as of version 6. Types are inferred/retained through the processing flow now.",
parser: NoOpParse1,
},
{
name: "--quote-none",
help: "Ignored as of version 6. Types are inferred/retained through the processing flow now.",
@ -2157,6 +2152,15 @@ var CSVTSVOnlyFlagSection = FlagSection{
*pargi += 1
},
},
{
name: "--quote-all",
help: "Force double-quoting of CSV fields.",
parser: func(args []string, argc int, pargi *int, options *TOptions) {
options.WriterOptions.CSVQuoteAll = true
*pargi += 1
},
},
},
}

View file

@ -105,6 +105,8 @@ type TWriterOptions struct {
JSONOutputMultiline bool // --jvstack
// Not using miller/types enum to avoid package cycle
CSVQuoteAll bool // --quote-all
// When we read things like
//
// x:a=1,x:b=2

View file

@ -18,6 +18,8 @@ type RecordWriterCSV struct {
lastJoinedHeader *string
// Only write one blank line for schema changes / blank input lines
justWroteEmptyLine bool
// For double-quote around all fields
quoteAll bool
}
func NewRecordWriterCSV(writerOptions *cli.TWriterOptions) (*RecordWriterCSV, error) {
@ -32,6 +34,7 @@ func NewRecordWriterCSV(writerOptions *cli.TWriterOptions) (*RecordWriterCSV, er
csvWriter: nil, // will be set on first Write() wherein we have the output stream
lastJoinedHeader: nil,
justWroteEmptyLine: false,
quoteAll: writerOptions.CSVQuoteAll,
}, nil
}
@ -81,7 +84,7 @@ func (writer *RecordWriterCSV) Write(
i++
}
//////writer.csvWriter.Write(fields)
writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, true)
writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, true, writer.quoteAll)
}
fields := make([]string, outrec.FieldCount)
@ -90,6 +93,6 @@ func (writer *RecordWriterCSV) Write(
fields[i] = pe.Value.String()
i++
}
writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, false)
writer.WriteCSVRecordMaybeColorized(fields, bufferedOutputStream, outputIsStdout, false, writer.quoteAll)
writer.justWroteEmptyLine = false
}

View file

@ -58,6 +58,7 @@ func (writer *RecordWriterCSV) WriteCSVRecordMaybeColorized(
bufferedOutputStream *bufio.Writer,
outputIsStdout bool,
isKey bool,
quoteAll bool,
) error {
comma := writer.csvWriter.Comma
@ -82,7 +83,8 @@ func (writer *RecordWriterCSV) WriteCSVRecordMaybeColorized(
// If we don't have to have a quoted field then just
// write out the field and continue to the next field.
if !fieldNeedsQuotes(field, comma) {
needsQuotes := quoteAll || fieldNeedsQuotes(field, comma)
if !needsQuotes {
if _, err := bufferedOutputStream.WriteString(prefix); err != nil {
return err
}