Be smarter about auto-unflatten (#1584)

This commit is contained in:
John Kerl 2024-06-08 20:58:26 -04:00 committed by GitHub
parent 6520bf4758
commit 71d9388bff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View file

@ -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

View file

@ -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)

View file

@ -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