mirror of
https://github.com/wader/fq.git
synced 2024-08-16 15:30:36 +03:00
interp: Make binary also respect bits_format
This commit is contained in:
parent
4335c30075
commit
b2c0e5fcd8
@ -397,6 +397,30 @@ func (b Binary) JQValueToNumber() any {
|
||||
func (b Binary) JQValueToString() any {
|
||||
return b.JQValueToGoJQ()
|
||||
}
|
||||
func (b Binary) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
|
||||
br, err := b.toReader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
brC, err := bitio.CloneReaderAtSeeker(br)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts, err := optsFn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s, err := opts.BitsFormatFn(brC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (b Binary) JQValueToGoJQ() any {
|
||||
buf, err := b.toBytesBuffer(b.r)
|
||||
if err != nil {
|
||||
|
@ -383,7 +383,7 @@ func makeDecodeValueOut(dv *decode.Value, kind decodeValueKind, out any) any {
|
||||
},
|
||||
},
|
||||
decodeValueBase: decodeValueBase{dv: dv},
|
||||
bitsFormat: true,
|
||||
isRaw: true,
|
||||
}
|
||||
case bool:
|
||||
return decodeValue{
|
||||
@ -599,7 +599,7 @@ var _ DecodeValue = decodeValue{}
|
||||
type decodeValue struct {
|
||||
gojq.JQValue
|
||||
decodeValueBase
|
||||
bitsFormat bool
|
||||
isRaw bool
|
||||
}
|
||||
|
||||
func (v decodeValue) JQValueKey(name string) any {
|
||||
@ -609,7 +609,7 @@ func (v decodeValue) JQValueHas(key any) any {
|
||||
return valueHas(key, v.decodeValueBase.JQValueKey, v.JQValue.JQValueHas)
|
||||
}
|
||||
func (v decodeValue) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
|
||||
if !v.bitsFormat {
|
||||
if !v.isRaw {
|
||||
return v.JQValueToGoJQ()
|
||||
}
|
||||
|
||||
@ -617,26 +617,9 @@ func (v decodeValue) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
br, err := bv.toReader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
brC, err := bitio.CloneReaderAtSeeker(br)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bv.JQValueToGoJQEx(optsFn)
|
||||
|
||||
opts, err := optsFn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s, err := opts.BitsFormatFn(brC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// decode value array
|
||||
|
5
pkg/interp/testdata/binary.fqtest
vendored
5
pkg/interp/testdata/binary.fqtest
vendored
@ -41,6 +41,11 @@ $ fq -d mp3 '.frames[0].audio_data | ("", "md5", "hex", "base64", "snippet") as
|
||||
"0000000000"
|
||||
"AAAAAAA="
|
||||
"<5>AAAAAAA="
|
||||
$ fq -o bits_format=hex -n '[1,2,3] | tobytes'
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
0x0|01 02 03| |...| |.: raw bits 0x0-0x2.7 (3)
|
||||
$ fq -o bits_format=hex -V -n '[1,2,3] | tobytes'
|
||||
"010203"
|
||||
$ fq -d mp3 -i . test.mp3
|
||||
mp3> [1, 2, 3] | tobytes
|
||||
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
||||
|
Loading…
Reference in New Issue
Block a user