Skip to content

Commit

Permalink
dump,json: Properly figure if compound or not
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 0a97f86 commit f8e5944
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
8 changes: 5 additions & 3 deletions format/json/testdata/json.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/json.gz:
$ fq -d json . /test.json
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| |
| | |.: {} /test.json (json)
0x00|7b 0a 20 20 20 20 22 61 22 3a 20 31 32 33 2c 0a|{. "a": 123,.|.: {} /test.json (json)
* |until 0x74.7 (end) (117) | |
$ fq -d json tovalue /test.json
{
"a": 123,
Expand All @@ -17,7 +18,8 @@ $ fq -d json tovalue /test.json
}
$ fq . /test.json
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| |
| | |.: {} /test.json (json)
0x00|7b 0a 20 20 20 20 22 61 22 3a 20 31 32 33 2c 0a|{. "a": 123,.|.: {} /test.json (json)
* |until 0x74.7 (end) (117) | |
$ fq .b[1] /test.json
2
$ fq . /json.gz
Expand Down Expand Up @@ -57,4 +59,4 @@ $ fq tovalue /json.gz
}
$ fq .uncompressed /json.gz
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| |
| | |.uncompressed: {} (json)
0x0|7b 22 61 22 3a 20 31 32 33 7d |{"a": 123} |.uncompressed: {} (json)
17 changes: 11 additions & 6 deletions pkg/interp/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ const (
colField = 6
)

func isCompound(v *decode.Value) bool {
switch v.V.(type) {
case decode.Struct, decode.Array:
return true
default:
return false
}
}

func dumpEx(v *decode.Value, cw *columnwriter.Writer, depth int, rootV *decode.Value, rootDepth int, addrWidth int, opts Options) error {
deco := opts.Decorator
// no error check as we write into buffering column
Expand Down Expand Up @@ -75,8 +84,6 @@ func dumpEx(v *decode.Value, cw *columnwriter.Writer, depth int, rootV *decode.V
rootIndent := strings.Repeat(" ", rootDepth)
indent := strings.Repeat(" ", depth)

isSimple := false

if depth == 0 {
switch v.V.(type) {
case decode.Struct:
Expand Down Expand Up @@ -152,8 +159,6 @@ func dumpEx(v *decode.Value, cw *columnwriter.Writer, depth int, rootV *decode.V
if v.Description != "" {
cfmt(colField, fmt.Sprintf(" (%s)", deco.Value.F(v.Description)))
}

isSimple = true
}

if opts.Verbose {
Expand Down Expand Up @@ -245,8 +250,8 @@ func dumpEx(v *decode.Value, cw *columnwriter.Writer, depth int, rootV *decode.V
// log.Printf("opts: %#+v\n", opts)
// log.Printf("depth: %#+v\n", depth)

// has length and is a simple value or a collapsed struct/array
if v.Range.Len > 0 && (isSimple || (opts.Depth != 0 && opts.Depth == depth)) {
// has length and is not compound or a collapsed struct/array (max depth)
if v.Range.Len > 0 && (!isCompound(v) || (opts.Depth != 0 && opts.Depth == depth)) {
cfmt(colAddr, "%s%s\n",
rootIndent, deco.DumpAddr.F(num.PadFormatInt(startLineByte, opts.AddrBase, true, addrWidth)))

Expand Down
2 changes: 1 addition & 1 deletion pkg/interp/testdata/value_json_array.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$ fq -i -n '"[]" | json'
json> (.) | ., tovalue, type, length?
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| |
| | |.: [0] unnamed (json)
0x0|5b 5d| |[]| |.: [0] unnamed (json)
[]
"array"
0
Expand Down
2 changes: 1 addition & 1 deletion pkg/interp/testdata/value_json_object.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$ fq -i -n '"{}" | json'
json> (.) | ., tovalue, type, length?
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| |
| | |.: {} unnamed (json)
0x0|7b 7d| |{}| |.: {} unnamed (json)
{}
"object"
0
Expand Down

0 comments on commit f8e5944

Please sign in to comment.