Skip to content

Commit 1bdf1df

Browse files
authored
Merge pull request #647 from wader/tovalue-deep
interp: Make tovalue output behave as jq value
2 parents 6194b87 + ee66fec commit 1bdf1df

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

internal/gojqex/totype.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ func IsNull(x any) bool {
2424
}
2525

2626
func ToGoJQValue(v any) (any, bool) {
27+
return ToGoJQValueFn(v, func(v any) (any, bool) {
28+
switch v := v.(type) {
29+
case gojq.JQValue:
30+
return v.JQValueToGoJQ(), true
31+
default:
32+
return nil, false
33+
}
34+
})
35+
}
36+
37+
func ToGoJQValueFn(v any, valueFn func(v any) (any, bool)) (any, bool) {
2738
switch vv := v.(type) {
2839
case nil:
2940
return vv, true
@@ -51,25 +62,16 @@ func ToGoJQValue(v any) (any, bool) {
5162
if vv >= math.MinInt && vv <= math.MaxInt {
5263
return int(vv), true
5364
}
54-
return vv, true
55-
} else if vv.IsUint64() {
56-
vv := vv.Uint64()
57-
if vv <= math.MaxInt {
58-
return int(vv), true
59-
}
60-
return vv, true
6165
}
6266
return vv, true
6367
case string:
6468
return vv, true
6569
case []byte:
6670
return string(vv), true
67-
case gojq.JQValue:
68-
return ToGoJQValue(vv.JQValueToGoJQ())
6971
case []any:
7072
vvs := make([]any, len(vv))
7173
for i, v := range vv {
72-
v, ok := ToGoJQValue(v)
74+
v, ok := ToGoJQValueFn(v, valueFn)
7375
if !ok {
7476
return nil, false
7577
}
@@ -79,14 +81,17 @@ func ToGoJQValue(v any) (any, bool) {
7981
case map[string]any:
8082
vvs := make(map[string]any, len(vv))
8183
for k, v := range vv {
82-
v, ok := ToGoJQValue(v)
84+
v, ok := ToGoJQValueFn(v, valueFn)
8385
if !ok {
8486
return nil, false
8587
}
8688
vvs[k] = v
8789
}
8890
return vvs, true
8991
default:
92+
if nv, ok := valueFn(vv); ok {
93+
return ToGoJQValueFn(nv, valueFn)
94+
}
9095
return nil, false
9196
}
9297
}

pkg/interp/decode.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,23 @@ func valueHas(key any, a func(name string) any, b func(key any) any) any {
305305
return b(key)
306306
}
307307

308-
// optsFn is a function as toValue is used by tovalue/0 so needs to be fast
308+
// TODO: make more efficient somehow? shallow values but might be hard
309+
// when things like tovalue.key should behave like a jq value and not a decode value etc
309310
func toValue(optsFn func() Options, v any) any {
310-
switch v := v.(type) {
311-
case JQValueEx:
312-
if optsFn == nil {
313-
return v.JQValueToGoJQ()
311+
nv, _ := gojqex.ToGoJQValueFn(v, func(v any) (any, bool) {
312+
switch v := v.(type) {
313+
case JQValueEx:
314+
if optsFn == nil {
315+
return v.JQValueToGoJQ(), true
316+
}
317+
return v.JQValueToGoJQEx(optsFn), true
318+
case gojq.JQValue:
319+
return v.JQValueToGoJQ(), true
320+
default:
321+
return v, true
314322
}
315-
return v.JQValueToGoJQEx(optsFn)
316-
case gojq.JQValue:
317-
return v.JQValueToGoJQ()
318-
default:
319-
return v
320-
}
323+
})
324+
return nv
321325
}
322326

323327
type decodeValueKind int

pkg/interp/interp.jq

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def display($opts):
1616
( . as $c
1717
| options($opts) as $opts
1818
| try _todisplay catch $c
19-
| if ($opts.value_output | not) and _can_display then _display($opts)
19+
| if $opts.value_output then tovalue end
20+
| if _can_display then _display($opts)
2021
else
2122
( if _is_string and $opts.raw_string then print
2223
else _print_color_json($opts)

pkg/interp/testdata/tovalue.fqtest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# TODO: use test format
2+
$ fq -i . test.mp3
3+
mp3> .headers[0] | tovalue.header.magic
4+
"ID3"
5+
mp3> ^D
26
$ fq -i
37
null> "aaa" | mp3_frame | .gap0 | tovalue, tovalue({sizebase: 2})
48
"aaa"

pkg/interp/testdata/value_output.fqtest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ ID3
2020
"unused": 0
2121
}
2222
35
23+
$ fq -o bits_format=base64 -V '.frames[0].audio_data' test.mp3
24+
"AAAAAAA="
25+
$ fq -o bits_format=base64 -Vr '.frames[0].audio_data' test.mp3
26+
AAAAAAA=

0 commit comments

Comments
 (0)