mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-18 00:45:47 +00:00
Be smarter about auto-unflatten (#1584)
This commit is contained in:
parent
6520bf4758
commit
71d9388bff
3 changed files with 22 additions and 3 deletions
|
|
@ -52,6 +52,10 @@ package cli
|
|||
// * If input is non-JSON and output is JSON:
|
||||
// o Default is to auto-unflatten at output.
|
||||
// o There is a --no-auto-unflatten for those who want it.
|
||||
//
|
||||
// * Overrriding these: if the last verb the user has explicitly provided is
|
||||
// flatten, don't undo that by putting an unflatten right after.
|
||||
//
|
||||
// ================================================================
|
||||
|
||||
func DecideFinalFlatten(writerOptions *TWriterOptions) bool {
|
||||
|
|
@ -64,7 +68,22 @@ func DecideFinalFlatten(writerOptions *TWriterOptions) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func DecideFinalUnflatten(options *TOptions) bool {
|
||||
func DecideFinalUnflatten(
|
||||
options *TOptions,
|
||||
verbSequences [][]string,
|
||||
) bool {
|
||||
|
||||
numVerbs := len(verbSequences)
|
||||
if numVerbs > 0 {
|
||||
lastVerbSequence := verbSequences[numVerbs-1]
|
||||
if len(lastVerbSequence) > 0 {
|
||||
lastVerbName := lastVerbSequence[0]
|
||||
if lastVerbName == "flatten" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ifmt := options.ReaderOptions.InputFileFormat
|
||||
ofmt := options.WriterOptions.OutputFileFormat
|
||||
|
||||
|
|
|
|||
|
|
@ -376,7 +376,7 @@ func parseCommandLinePassTwo(
|
|||
recordTransformers = append(recordTransformers, transformer)
|
||||
}
|
||||
|
||||
if cli.DecideFinalUnflatten(options) {
|
||||
if cli.DecideFinalUnflatten(options, verbSequences) {
|
||||
// E.g. req.method=GET,req.path=/api/check becomes
|
||||
// '{"req": {"method": "GET", "path": "/api/check"}}'
|
||||
transformer, err := transformers.NewTransformerUnflatten(options.WriterOptions.FLATSEP, options, nil)
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ func ReplMain(args []string) int {
|
|||
// --auto-flatten is on by default. But if input and output formats are both JSON,
|
||||
// then we don't need to actually do anything. See also mlrcli_parse.go.
|
||||
options.WriterOptions.AutoFlatten = cli.DecideFinalFlatten(&options.WriterOptions)
|
||||
options.WriterOptions.AutoUnflatten = cli.DecideFinalUnflatten(options)
|
||||
options.WriterOptions.AutoUnflatten = cli.DecideFinalUnflatten(options, [][]string{})
|
||||
|
||||
recordOutputFileName := "(stdout)"
|
||||
recordOutputStream := os.Stdout
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue