Skip to content

Commit a7d54ff

Browse files
authored
Merge pull request #677 from wader/binary-bits-format
interp: Make binary also respect bits_format
2 parents fa5a89c + b2c0e5f commit a7d54ff

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

pkg/interp/binary.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,30 @@ func (b Binary) JQValueToNumber() any {
397397
func (b Binary) JQValueToString() any {
398398
return b.JQValueToGoJQ()
399399
}
400+
func (b Binary) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
401+
br, err := b.toReader()
402+
if err != nil {
403+
return err
404+
}
405+
406+
brC, err := bitio.CloneReaderAtSeeker(br)
407+
if err != nil {
408+
return err
409+
}
410+
411+
opts, err := optsFn()
412+
if err != nil {
413+
return err
414+
}
415+
416+
s, err := opts.BitsFormatFn(brC)
417+
if err != nil {
418+
return err
419+
}
420+
421+
return s
422+
}
423+
400424
func (b Binary) JQValueToGoJQ() any {
401425
buf, err := b.toBytesBuffer(b.r)
402426
if err != nil {

pkg/interp/decode.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func makeDecodeValueOut(dv *decode.Value, kind decodeValueKind, out any) any {
383383
},
384384
},
385385
decodeValueBase: decodeValueBase{dv: dv},
386-
bitsFormat: true,
386+
isRaw: true,
387387
}
388388
case bool:
389389
return decodeValue{
@@ -599,7 +599,7 @@ var _ DecodeValue = decodeValue{}
599599
type decodeValue struct {
600600
gojq.JQValue
601601
decodeValueBase
602-
bitsFormat bool
602+
isRaw bool
603603
}
604604

605605
func (v decodeValue) JQValueKey(name string) any {
@@ -609,34 +609,17 @@ func (v decodeValue) JQValueHas(key any) any {
609609
return valueHas(key, v.decodeValueBase.JQValueKey, v.JQValue.JQValueHas)
610610
}
611611
func (v decodeValue) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
612-
if !v.bitsFormat {
612+
if !v.isRaw {
613613
return v.JQValueToGoJQ()
614614
}
615615

616616
bv, err := v.decodeValueBase.ToBinary()
617617
if err != nil {
618618
return err
619619
}
620-
br, err := bv.toReader()
621-
if err != nil {
622-
return err
623-
}
624-
625-
brC, err := bitio.CloneReaderAtSeeker(br)
626-
if err != nil {
627-
return err
628-
}
629620

630-
opts, err := optsFn()
631-
if err != nil {
632-
return err
633-
}
621+
return bv.JQValueToGoJQEx(optsFn)
634622

635-
s, err := opts.BitsFormatFn(brC)
636-
if err != nil {
637-
return err
638-
}
639-
return s
640623
}
641624

642625
// decode value array

pkg/interp/testdata/binary.fqtest

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ $ fq -d mp3 '.frames[0].audio_data | ("", "md5", "hex", "base64", "snippet") as
4141
"0000000000"
4242
"AAAAAAA="
4343
"<5>AAAAAAA="
44+
$ fq -o bits_format=hex -n '[1,2,3] | tobytes'
45+
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
46+
0x0|01 02 03| |...| |.: raw bits 0x0-0x2.7 (3)
47+
$ fq -o bits_format=hex -V -n '[1,2,3] | tobytes'
48+
"010203"
4449
$ fq -d mp3 -i . test.mp3
4550
mp3> [1, 2, 3] | tobytes
4651
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|

0 commit comments

Comments
 (0)