1
1
mirror of https://github.com/wader/fq.git synced 2024-12-29 00:22:38 +03:00

dump,json: Properly figure if compound or not

This commit is contained in:
Mattias Wadman 2021-09-06 11:06:06 +02:00
parent 0a97f86f32
commit f8e5944f91
4 changed files with 18 additions and 11 deletions

View File

@ -2,7 +2,8 @@
/json.gz: /json.gz:
$ fq -d json . /test.json $ fq -d json . /test.json
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| | |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 $ fq -d json tovalue /test.json
{ {
"a": 123, "a": 123,
@ -17,7 +18,8 @@ $ fq -d json tovalue /test.json
} }
$ fq . /test.json $ fq . /test.json
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| | |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 $ fq .b[1] /test.json
2 2
$ fq . /json.gz $ fq . /json.gz
@ -57,4 +59,4 @@ $ fq tovalue /json.gz
} }
$ fq .uncompressed /json.gz $ fq .uncompressed /json.gz
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| | |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)

View File

@ -24,6 +24,15 @@ const (
colField = 6 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 { func dumpEx(v *decode.Value, cw *columnwriter.Writer, depth int, rootV *decode.Value, rootDepth int, addrWidth int, opts Options) error {
deco := opts.Decorator deco := opts.Decorator
// no error check as we write into buffering column // no error check as we write into buffering column
@ -75,8 +84,6 @@ func dumpEx(v *decode.Value, cw *columnwriter.Writer, depth int, rootV *decode.V
rootIndent := strings.Repeat(" ", rootDepth) rootIndent := strings.Repeat(" ", rootDepth)
indent := strings.Repeat(" ", depth) indent := strings.Repeat(" ", depth)
isSimple := false
if depth == 0 { if depth == 0 {
switch v.V.(type) { switch v.V.(type) {
case decode.Struct: case decode.Struct:
@ -152,8 +159,6 @@ func dumpEx(v *decode.Value, cw *columnwriter.Writer, depth int, rootV *decode.V
if v.Description != "" { if v.Description != "" {
cfmt(colField, fmt.Sprintf(" (%s)", deco.Value.F(v.Description))) cfmt(colField, fmt.Sprintf(" (%s)", deco.Value.F(v.Description)))
} }
isSimple = true
} }
if opts.Verbose { if opts.Verbose {
@ -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("opts: %#+v\n", opts)
// log.Printf("depth: %#+v\n", depth) // log.Printf("depth: %#+v\n", depth)
// has length and is a simple value or a collapsed struct/array // has length and is not compound or a collapsed struct/array (max depth)
if v.Range.Len > 0 && (isSimple || (opts.Depth != 0 && opts.Depth == depth)) { if v.Range.Len > 0 && (!isCompound(v) || (opts.Depth != 0 && opts.Depth == depth)) {
cfmt(colAddr, "%s%s\n", cfmt(colAddr, "%s%s\n",
rootIndent, deco.DumpAddr.F(num.PadFormatInt(startLineByte, opts.AddrBase, true, addrWidth))) rootIndent, deco.DumpAddr.F(num.PadFormatInt(startLineByte, opts.AddrBase, true, addrWidth)))

View File

@ -2,7 +2,7 @@
$ fq -i -n '"[]" | json' $ fq -i -n '"[]" | json'
json> (.) | ., tovalue, type, length? json> (.) | ., tovalue, type, length?
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| | |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" "array"
0 0

View File

@ -2,7 +2,7 @@
$ fq -i -n '"{}" | json' $ fq -i -n '"{}" | json'
json> (.) | ., tovalue, type, length? json> (.) | ., tovalue, type, length?
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| | |00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f| |
| | |.: {} unnamed (json) 0x0|7b 7d| |{}| |.: {} unnamed (json)
{} {}
"object" "object"
0 0