Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be smarter about auto-unflatten #1584

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading