diff --git a/pkg/cli/flatten_unflatten.go b/pkg/cli/flatten_unflatten.go index cd92ec6f7..8744b10b8 100644 --- a/pkg/cli/flatten_unflatten.go +++ b/pkg/cli/flatten_unflatten.go @@ -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 diff --git a/pkg/climain/mlrcli_parse.go b/pkg/climain/mlrcli_parse.go index eeecfa6dc..52e772d4b 100644 --- a/pkg/climain/mlrcli_parse.go +++ b/pkg/climain/mlrcli_parse.go @@ -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) diff --git a/pkg/terminals/repl/entry.go b/pkg/terminals/repl/entry.go index d2403ccdf..23a86941b 100644 --- a/pkg/terminals/repl/entry.go +++ b/pkg/terminals/repl/entry.go @@ -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