Skip to content

Commit

Permalink
Be smarter about auto-unflatten (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Jun 9, 2024
1 parent 6520bf4 commit 71d9388
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
21 changes: 20 additions & 1 deletion pkg/cli/flatten_unflatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkg/climain/mlrcli_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/terminals/repl/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 71d9388

Please sign in to comment.