mirror of
https://github.com/wader/fq.git
synced 2024-09-11 12:05:39 +03:00
lint: Enable errcheck adnd revive
This commit is contained in:
parent
5bf4bc791a
commit
3ff0c9b5e0
@ -1,5 +1,7 @@
|
||||
linters:
|
||||
enable:
|
||||
- revive
|
||||
- errname
|
||||
- goimports
|
||||
- nilerr
|
||||
- forcetypeassert
|
||||
@ -33,4 +35,3 @@ linters-settings:
|
||||
# allow md5
|
||||
- G401
|
||||
- G501
|
||||
unused:
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Package all imports and registers all formats in the default registry
|
||||
//nolint:revive
|
||||
package all
|
||||
|
||||
import (
|
||||
|
@ -14,6 +14,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
OBU_SEQUENCE_HEADER = 1
|
||||
OBU_TEMPORAL_DELIMITER = 2
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/wader/fq/pkg/ranges"
|
||||
)
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
ALL = "all"
|
||||
|
||||
@ -130,6 +131,7 @@ type HevcDcrOut struct {
|
||||
}
|
||||
|
||||
// based on ffmpeg libavformat/isom.c ff_mp4_obj_type
|
||||
//nolint:revive
|
||||
const (
|
||||
MPEGObjectTypeMOV_TEXT = 0x08
|
||||
MPEGObjectTypeMPEG4 = 0x20
|
||||
@ -320,6 +322,7 @@ type ProtoBufIn struct {
|
||||
Message ProtoBufMessage
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
MPEGAudioObjectTypeMain = 1
|
||||
MPEGAudioObjectTypeLC = 2
|
||||
|
@ -23,6 +23,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
SHT_NULL = 0x0
|
||||
SHT_PROGBITS = 0x1
|
||||
@ -377,6 +378,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
|
||||
var shname string
|
||||
var typ uint64
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
DT_NULL = 0
|
||||
DT_NEEDED = 1
|
||||
|
@ -1,3 +1,4 @@
|
||||
//nolint:revive
|
||||
package ebml_matroska
|
||||
|
||||
// https://raw.githubusercontent.com/cellar-wg/matroska-specification/aa2144a58b661baf54b99bab41113d66b0f5ff62/ebml_matroska.xml
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Code below generated from ebml_matroska.xml
|
||||
//nolint:revive
|
||||
package ebml_matroska
|
||||
|
||||
import "github.com/wader/fq/format/matroska/ebml"
|
||||
|
@ -1177,8 +1177,8 @@ func init() {
|
||||
hasProgramID := d.FieldBool("has_program_id")
|
||||
if hasProgramID {
|
||||
d.FieldU16("short_program_id")
|
||||
hasUuid := d.FieldBool("has_uuid")
|
||||
if hasUuid {
|
||||
hasUUID := d.FieldBool("has_uuid")
|
||||
if hasUUID {
|
||||
d.FieldBitBufLen("uuid", 16*8)
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ var SyntaxElementNames = map[uint64]string{
|
||||
TERM: "TERM",
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
EXT_FILL = 0x0
|
||||
EXT_FILL_DATA = 0x1
|
||||
@ -61,6 +62,7 @@ var ExtensionPayloadIDNames = map[uint64]string{
|
||||
EXT_SBR_DATA_CRC: "EXT_SBR_DATA_CRC",
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
ONLY_LONG_SEQUENCE = 0x0
|
||||
LONG_START_SEQUENCE = 0x1
|
||||
|
@ -1,3 +1,4 @@
|
||||
//nolint:revive
|
||||
package mpeg
|
||||
|
||||
import (
|
||||
|
@ -21,6 +21,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
CMD_END = 0xff
|
||||
FSTA_DSP = 0x00
|
||||
|
@ -95,11 +95,11 @@ func (r *Registry) Group(name string) ([]*decode.Format, error) {
|
||||
}
|
||||
|
||||
func (r *Registry) MustGroup(name string) []*decode.Format {
|
||||
if g, err := r.Group(name); err == nil {
|
||||
g, err := r.Group(name)
|
||||
if err == nil {
|
||||
return g
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
func (r *Registry) MustAll() []*decode.Format {
|
||||
|
@ -1,3 +1,4 @@
|
||||
//nolint:revive
|
||||
package tiff
|
||||
|
||||
const (
|
||||
|
@ -24,6 +24,7 @@ var vp9FeatureIDNames = map[uint64]string{
|
||||
vp9FeatureChromaSubsampling: "Chroma Subsampling",
|
||||
}
|
||||
|
||||
//nolint:revive
|
||||
const (
|
||||
CS_UNKNOWN = 0
|
||||
CS_BT_601 = 1
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
|
||||
type ProgressFn func(approxReadBytes int64, totalSize int64)
|
||||
|
||||
type progressReaderSeeker struct {
|
||||
type ProgressReaderSeeker struct {
|
||||
rs io.ReadSeeker
|
||||
pos int64
|
||||
totalSize int64
|
||||
@ -16,12 +16,12 @@ type progressReaderSeeker struct {
|
||||
progressFn ProgressFn
|
||||
}
|
||||
|
||||
func New(rs io.ReadSeeker, precision int64, totalSize int64, fn ProgressFn) *progressReaderSeeker {
|
||||
func New(rs io.ReadSeeker, precision int64, totalSize int64, fn ProgressFn) *ProgressReaderSeeker {
|
||||
partitionSize := totalSize / precision
|
||||
if totalSize%precision != 0 {
|
||||
partitionSize++
|
||||
}
|
||||
return &progressReaderSeeker{
|
||||
return &ProgressReaderSeeker{
|
||||
rs: rs,
|
||||
totalSize: totalSize,
|
||||
partitionSize: partitionSize,
|
||||
@ -30,7 +30,7 @@ func New(rs io.ReadSeeker, precision int64, totalSize int64, fn ProgressFn) *pro
|
||||
}
|
||||
}
|
||||
|
||||
func (prs *progressReaderSeeker) Read(p []byte) (n int, err error) {
|
||||
func (prs *ProgressReaderSeeker) Read(p []byte) (n int, err error) {
|
||||
n, err = prs.rs.Read(p)
|
||||
newPos := prs.pos + int64(n)
|
||||
lastPartitionsReadCount := prs.partitionsReadCount
|
||||
@ -59,7 +59,7 @@ func (prs *progressReaderSeeker) Read(p []byte) (n int, err error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (prs *progressReaderSeeker) Seek(offset int64, whence int) (int64, error) {
|
||||
func (prs *ProgressReaderSeeker) Seek(offset int64, whence int) (int64, error) {
|
||||
pos, err := prs.rs.Seek(offset, whence)
|
||||
prs.pos = pos
|
||||
return pos, err
|
||||
|
@ -343,13 +343,13 @@ func (r *SectionBitReader) Seek(offset int64, whence int) (int64, error) {
|
||||
}
|
||||
|
||||
// TODO: smart, track index?
|
||||
type multiBitReader struct {
|
||||
type MultiBitReader struct {
|
||||
pos int64
|
||||
readers []BitReadAtSeeker
|
||||
readerEnds []int64
|
||||
}
|
||||
|
||||
func NewMultiBitReader(rs []BitReadAtSeeker) (*multiBitReader, error) {
|
||||
func NewMultiBitReader(rs []BitReadAtSeeker) (*MultiBitReader, error) {
|
||||
readerEnds := make([]int64, len(rs))
|
||||
var esSum int64
|
||||
for i, r := range rs {
|
||||
@ -360,10 +360,10 @@ func NewMultiBitReader(rs []BitReadAtSeeker) (*multiBitReader, error) {
|
||||
esSum += e
|
||||
readerEnds[i] = esSum
|
||||
}
|
||||
return &multiBitReader{readers: rs, readerEnds: readerEnds}, nil
|
||||
return &MultiBitReader{readers: rs, readerEnds: readerEnds}, nil
|
||||
}
|
||||
|
||||
func (m *multiBitReader) ReadBitsAt(p []byte, nBits int, bitOff int64) (n int, err error) {
|
||||
func (m *MultiBitReader) ReadBitsAt(p []byte, nBits int, bitOff int64) (n int, err error) {
|
||||
end := m.readerEnds[len(m.readers)-1]
|
||||
if end <= bitOff {
|
||||
return 0, io.EOF
|
||||
@ -390,13 +390,13 @@ func (m *multiBitReader) ReadBitsAt(p []byte, nBits int, bitOff int64) (n int, e
|
||||
return rBits, err
|
||||
}
|
||||
|
||||
func (m *multiBitReader) ReadBits(p []byte, nBits int) (n int, err error) {
|
||||
func (m *MultiBitReader) ReadBits(p []byte, nBits int) (n int, err error) {
|
||||
n, err = m.ReadBitsAt(p, nBits, m.pos)
|
||||
m.pos += int64(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (m *multiBitReader) SeekBits(bitOff int64, whence int) (int64, error) {
|
||||
func (m *MultiBitReader) SeekBits(bitOff int64, whence int) (int64, error) {
|
||||
var p int64
|
||||
end := m.readerEnds[len(m.readerEnds)-1]
|
||||
|
||||
@ -419,7 +419,7 @@ func (m *multiBitReader) SeekBits(bitOff int64, whence int) (int64, error) {
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (m *multiBitReader) Read(p []byte) (n int, err error) {
|
||||
func (m *MultiBitReader) Read(p []byte) (n int, err error) {
|
||||
n, err = m.ReadBitsAt(p, len(p)*8, m.pos)
|
||||
m.pos += int64(n)
|
||||
|
||||
|
@ -56,33 +56,33 @@ func Read64(buf []byte, firstBit int, nBits int) uint64 {
|
||||
}
|
||||
// done
|
||||
return n
|
||||
} else {
|
||||
b := buf[bytePos]
|
||||
}
|
||||
|
||||
if byteBitPos == 0 {
|
||||
// bitPos is byte aligned but not bitsLeft
|
||||
if bitsLeft >= 8 {
|
||||
// TODO: more cases left >= 16 etc
|
||||
n = n<<8 | uint64(b)
|
||||
bitPos += 8
|
||||
bitsLeft -= 8
|
||||
} else {
|
||||
n = n<<bitsLeft | (uint64(b) >> (8 - bitsLeft))
|
||||
// done
|
||||
return n
|
||||
}
|
||||
b := buf[bytePos]
|
||||
|
||||
if byteBitPos == 0 {
|
||||
// bitPos is byte aligned but not bitsLeft
|
||||
if bitsLeft >= 8 {
|
||||
// TODO: more cases left >= 16 etc
|
||||
n = n<<8 | uint64(b)
|
||||
bitPos += 8
|
||||
bitsLeft -= 8
|
||||
} else {
|
||||
// neither byteBitPos or bitsLeft byte aligned
|
||||
byteBitsLeft := (8 - byteBitPos) & 0x7
|
||||
if bitsLeft >= byteBitsLeft {
|
||||
n = n<<byteBitsLeft | (uint64(b) & ((1 << byteBitsLeft) - 1))
|
||||
bitPos += byteBitsLeft
|
||||
bitsLeft -= byteBitsLeft
|
||||
} else {
|
||||
n = n<<bitsLeft | (uint64(b)&((1<<byteBitsLeft)-1))>>(byteBitsLeft-bitsLeft)
|
||||
// done
|
||||
return n
|
||||
}
|
||||
n = n<<bitsLeft | (uint64(b) >> (8 - bitsLeft))
|
||||
// done
|
||||
return n
|
||||
}
|
||||
} else {
|
||||
// neither byteBitPos or bitsLeft byte aligned
|
||||
byteBitsLeft := (8 - byteBitPos) & 0x7
|
||||
if bitsLeft >= byteBitsLeft {
|
||||
n = n<<byteBitsLeft | (uint64(b) & ((1 << byteBitsLeft) - 1))
|
||||
bitPos += byteBitsLeft
|
||||
bitsLeft -= byteBitsLeft
|
||||
} else {
|
||||
n = n<<bitsLeft | (uint64(b)&((1<<byteBitsLeft)-1))>>(byteBitsLeft-bitsLeft)
|
||||
// done
|
||||
return n
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -134,37 +134,37 @@ func Write64(v uint64, nBits int, buf []byte, firstBit int) {
|
||||
}
|
||||
// done
|
||||
return
|
||||
} else {
|
||||
b := buf[bytePos]
|
||||
}
|
||||
|
||||
if byteBitPos == 0 {
|
||||
// byteBitPos is byte aligned but not bitsLeft
|
||||
if bitsLeft >= 8 {
|
||||
// TODO: more cases left >= 16 etc
|
||||
buf[bytePos] = byte(v >> (bitsLeft - 8))
|
||||
bitPos += 8
|
||||
bitsLeft -= 8
|
||||
} else {
|
||||
extraBits := 8 - bitsLeft
|
||||
buf[bytePos] = byte(v)<<extraBits | b&((1<<extraBits)-1)
|
||||
// done
|
||||
return
|
||||
}
|
||||
b := buf[bytePos]
|
||||
|
||||
if byteBitPos == 0 {
|
||||
// byteBitPos is byte aligned but not bitsLeft
|
||||
if bitsLeft >= 8 {
|
||||
// TODO: more cases left >= 16 etc
|
||||
buf[bytePos] = byte(v >> (bitsLeft - 8))
|
||||
bitPos += 8
|
||||
bitsLeft -= 8
|
||||
} else {
|
||||
// neither byteBitPos or bitsLeft byte aligned
|
||||
byteBitsLeft := (8 - byteBitPos) & 0x7
|
||||
if bitsLeft >= byteBitsLeft {
|
||||
bMask := byte((1<<byteBitPos)-1) << (8 - byteBitPos)
|
||||
buf[bytePos] = b&bMask | byte(v>>(bitsLeft-byteBitsLeft))
|
||||
bitPos += byteBitsLeft
|
||||
bitsLeft -= byteBitsLeft
|
||||
} else {
|
||||
extraBits := byteBitsLeft - bitsLeft
|
||||
bMask := byte(((1<<byteBitPos)-1)<<(8-byteBitPos) | ((1 << extraBits) - 1))
|
||||
buf[bytePos] = b&bMask | byte(v)<<extraBits
|
||||
// done
|
||||
return
|
||||
}
|
||||
extraBits := 8 - bitsLeft
|
||||
buf[bytePos] = byte(v)<<extraBits | b&((1<<extraBits)-1)
|
||||
// done
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// neither byteBitPos or bitsLeft byte aligned
|
||||
byteBitsLeft := (8 - byteBitPos) & 0x7
|
||||
if bitsLeft >= byteBitsLeft {
|
||||
bMask := byte((1<<byteBitPos)-1) << (8 - byteBitPos)
|
||||
buf[bytePos] = b&bMask | byte(v>>(bitsLeft-byteBitsLeft))
|
||||
bitPos += byteBitsLeft
|
||||
bitsLeft -= byteBitsLeft
|
||||
} else {
|
||||
extraBits := byteBitsLeft - bitsLeft
|
||||
bMask := byte(((1<<byteBitPos)-1)<<(8-byteBitPos) | ((1 << extraBits) - 1))
|
||||
buf[bytePos] = b&bMask | byte(v)<<extraBits
|
||||
// done
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ func MakeTable(poly int, bits int) Table {
|
||||
|
||||
for i := 0; i < 256; i++ {
|
||||
// note sure about -8 for > 16 bit crc
|
||||
var crc uint = uint(i << (bits - 8))
|
||||
crc := uint(i << (bits - 8))
|
||||
for j := 0; j < 8; j++ {
|
||||
if crc&(1<<(bits-1)) != 0 {
|
||||
crc = ((crc << 1) ^ uint(poly)) & mask
|
||||
|
@ -17,11 +17,11 @@ import (
|
||||
"github.com/wader/fq/pkg/ranges"
|
||||
)
|
||||
|
||||
type DecodeFormatsError struct {
|
||||
type FormatsError struct {
|
||||
Errs []FormatError
|
||||
}
|
||||
|
||||
func (de DecodeFormatsError) Error() string {
|
||||
func (de FormatsError) Error() string {
|
||||
var errs []string
|
||||
for _, err := range de.Errs {
|
||||
errs = append(errs, err.Error())
|
||||
@ -97,10 +97,7 @@ const (
|
||||
LittleEndian
|
||||
)
|
||||
|
||||
// just to get some type safety
|
||||
type Options interface{ decodeOptions() }
|
||||
|
||||
type DecodeOptions struct {
|
||||
type Options struct {
|
||||
Name string
|
||||
Description string
|
||||
IsRoot bool
|
||||
@ -110,22 +107,20 @@ type DecodeOptions struct {
|
||||
ReadBuf *[]byte
|
||||
}
|
||||
|
||||
func (DecodeOptions) decodeOptions() {}
|
||||
|
||||
// Decode try decode formats and return first success and all other decoder errors
|
||||
func Decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts DecodeOptions) (*Value, interface{}, error) {
|
||||
func Decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Options) (*Value, interface{}, error) {
|
||||
opts.IsRoot = true
|
||||
return decode(ctx, bb, formats, opts)
|
||||
}
|
||||
|
||||
func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts DecodeOptions) (*Value, interface{}, error) {
|
||||
func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Options) (*Value, interface{}, error) {
|
||||
if formats == nil {
|
||||
panic("formats is nil, failed to register format?")
|
||||
}
|
||||
|
||||
var forceOne = len(formats) == 1
|
||||
|
||||
decodeErr := DecodeFormatsError{}
|
||||
decodeErr := FormatsError{}
|
||||
|
||||
for _, f := range formats {
|
||||
d := NewDecoder(ctx, f, bb, opts)
|
||||
@ -142,7 +137,7 @@ func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Decod
|
||||
|
||||
if !rOk {
|
||||
switch panicV := r.RecoverV.(type) {
|
||||
case IOError, ValidateError, DecodeFormatsError:
|
||||
case IOError, ValidateError, FormatsError:
|
||||
panicErr, _ := panicV.(error)
|
||||
formatErr := FormatError{
|
||||
Err: panicErr,
|
||||
@ -202,7 +197,7 @@ type D struct {
|
||||
}
|
||||
|
||||
// TODO: new struct decoder?
|
||||
func NewDecoder(ctx context.Context, format *Format, bb *bitio.Buffer, opts DecodeOptions) *D {
|
||||
func NewDecoder(ctx context.Context, format *Format, bb *bitio.Buffer, opts Options) *D {
|
||||
cbb := bb.Copy()
|
||||
|
||||
name := format.RootName
|
||||
@ -855,7 +850,7 @@ func (d *D) DecodeRangeFn(firstBit int64, nBits int64, fn func(d *D)) {
|
||||
|
||||
func (d *D) Format(formats []*Format, inArg interface{}) interface{} {
|
||||
bb := d.BitBufRange(d.Pos(), d.BitsLeft())
|
||||
dv, v, err := decode(d.Ctx, bb, formats, DecodeOptions{
|
||||
dv, v, err := decode(d.Ctx, bb, formats, Options{
|
||||
ReadBuf: d.readBuf,
|
||||
IsRoot: false,
|
||||
StartOffset: d.Pos(),
|
||||
@ -887,7 +882,7 @@ func (d *D) Format(formats []*Format, inArg interface{}) interface{} {
|
||||
|
||||
func (d *D) FieldTryFormat(name string, formats []*Format, inArg interface{}) (*Value, interface{}, error) {
|
||||
bb := d.BitBufRange(d.Pos(), d.BitsLeft())
|
||||
dv, v, err := decode(d.Ctx, bb, formats, DecodeOptions{
|
||||
dv, v, err := decode(d.Ctx, bb, formats, Options{
|
||||
Name: name,
|
||||
ReadBuf: d.readBuf,
|
||||
IsRoot: false,
|
||||
@ -916,7 +911,7 @@ func (d *D) FieldFormat(name string, formats []*Format, inArg interface{}) (*Val
|
||||
|
||||
func (d *D) FieldTryFormatLen(name string, nBits int64, formats []*Format, inArg interface{}) (*Value, interface{}, error) {
|
||||
bb := d.BitBufRange(d.Pos(), nBits)
|
||||
dv, v, err := decode(d.Ctx, bb, formats, DecodeOptions{
|
||||
dv, v, err := decode(d.Ctx, bb, formats, Options{
|
||||
Name: name,
|
||||
ReadBuf: d.readBuf,
|
||||
IsRoot: false,
|
||||
@ -946,7 +941,7 @@ func (d *D) FieldFormatLen(name string, nBits int64, formats []*Format, inArg in
|
||||
// TODO: return decooder?
|
||||
func (d *D) FieldTryFormatRange(name string, firstBit int64, nBits int64, formats []*Format, inArg interface{}) (*Value, interface{}, error) {
|
||||
bb := d.BitBufRange(firstBit, nBits)
|
||||
dv, v, err := decode(d.Ctx, bb, formats, DecodeOptions{
|
||||
dv, v, err := decode(d.Ctx, bb, formats, Options{
|
||||
Name: name,
|
||||
ReadBuf: d.readBuf,
|
||||
IsRoot: false,
|
||||
@ -972,7 +967,7 @@ func (d *D) FieldFormatRange(name string, firstBit int64, nBits int64, formats [
|
||||
}
|
||||
|
||||
func (d *D) FieldTryFormatBitBuf(name string, bb *bitio.Buffer, formats []*Format, inArg interface{}) (*Value, interface{}, error) {
|
||||
dv, v, err := decode(d.Ctx, bb, formats, DecodeOptions{
|
||||
dv, v, err := decode(d.Ctx, bb, formats, Options{
|
||||
Name: name,
|
||||
ReadBuf: d.readBuf,
|
||||
IsRoot: true,
|
||||
|
@ -128,14 +128,14 @@ func (d *D) FE(nBits int, endian Endian) float64 {
|
||||
}
|
||||
|
||||
func (d *D) TryFieldUFn(name string, fn func() (uint64, DisplayFormat, string)) (uint64, error) {
|
||||
if v, err := d.TryFieldFn(name, func() (*Value, error) {
|
||||
v, err := d.TryFieldFn(name, func() (*Value, error) {
|
||||
u, fmt, d := fn()
|
||||
return &Value{V: u, DisplayFormat: fmt, Symbol: d}, nil
|
||||
}); err != nil {
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return v.V.(uint64), err
|
||||
}
|
||||
return v.V.(uint64), err
|
||||
}
|
||||
|
||||
func (d *D) FieldUFn(name string, fn func() (uint64, DisplayFormat, string)) uint64 {
|
||||
|
@ -16,7 +16,7 @@ type bufferRange struct {
|
||||
r ranges.Range
|
||||
}
|
||||
|
||||
var _ InterpValue = (*bufferObject)(nil)
|
||||
var _ Value = (*bufferObject)(nil)
|
||||
var _ ToBuffer = (*bufferObject)(nil)
|
||||
|
||||
type bufferObject struct {
|
||||
|
@ -20,7 +20,7 @@ import (
|
||||
const (
|
||||
colAddr = 0
|
||||
colHex = 2
|
||||
colAscii = 4
|
||||
colASCII = 4
|
||||
colField = 6
|
||||
)
|
||||
|
||||
@ -92,7 +92,7 @@ func dumpEx(v *decode.Value, buf []byte, cw *columnwriter.Writer, depth int, roo
|
||||
columns()
|
||||
}
|
||||
cfmt(colHex, "%s", deco.DumpHeader.F(hexHeader))
|
||||
cfmt(colAscii, "%s", deco.DumpHeader.F(asciiHeader))
|
||||
cfmt(colASCII, "%s", deco.DumpHeader.F(asciiHeader))
|
||||
if !isCompound(v) {
|
||||
cw.Flush()
|
||||
}
|
||||
@ -175,7 +175,7 @@ func dumpEx(v *decode.Value, buf []byte, cw *columnwriter.Writer, depth int, roo
|
||||
indent := strings.Repeat(" ", depth)
|
||||
|
||||
var formatErr decode.FormatError
|
||||
var decodeFormatsErr decode.DecodeFormatsError
|
||||
var decodeFormatsErr decode.FormatsError
|
||||
|
||||
switch {
|
||||
case errors.As(err, &formatErr):
|
||||
@ -191,7 +191,7 @@ func dumpEx(v *decode.Value, buf []byte, cw *columnwriter.Writer, depth int, roo
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case errors.Is(formatErr.Err, decode.DecodeFormatsError{}):
|
||||
case errors.Is(formatErr.Err, decode.FormatsError{}):
|
||||
printErrs(depth+1, formatErr.Err)
|
||||
}
|
||||
case errors.As(err, &decodeFormatsErr):
|
||||
@ -273,7 +273,7 @@ func dumpEx(v *decode.Value, buf []byte, cw *columnwriter.Writer, depth int, roo
|
||||
return err
|
||||
}
|
||||
if _, err := io.CopyBuffer(
|
||||
asciiwriter.New(cw.Columns[colAscii], opts.LineBytes, int(startLineByteOffset), asciiFn),
|
||||
asciiwriter.New(cw.Columns[colASCII], opts.LineBytes, int(startLineByteOffset), asciiFn),
|
||||
io.LimitReader(vBitBuf.Copy(), displaySizeBytes),
|
||||
buf); err != nil {
|
||||
return err
|
||||
@ -290,7 +290,7 @@ func dumpEx(v *decode.Value, buf []byte, cw *columnwriter.Writer, depth int, roo
|
||||
if lastDisplayByte == bufferLastByte && lastDisplayByte != lastLineStopByte {
|
||||
// extra "|" in as EOF markers
|
||||
cfmt(colHex, "%s\n", deco.Column)
|
||||
cfmt(colAscii, "%s\n", deco.Column)
|
||||
cfmt(colASCII, "%s\n", deco.Column)
|
||||
}
|
||||
|
||||
if stopByte != lastDisplayByte {
|
||||
|
@ -363,7 +363,7 @@ func (i *Interp) queryToString(c interface{}, a []interface{}) interface{} {
|
||||
}
|
||||
|
||||
func (i *Interp) _displayName(c interface{}, a []interface{}) interface{} {
|
||||
qo, ok := c.(InterpValue)
|
||||
qo, ok := c.(Value)
|
||||
if !ok {
|
||||
return fmt.Errorf("%v: value is not query object", c)
|
||||
}
|
||||
@ -371,7 +371,7 @@ func (i *Interp) _displayName(c interface{}, a []interface{}) interface{} {
|
||||
}
|
||||
|
||||
func (i *Interp) _extKeys(c interface{}, a []interface{}) interface{} {
|
||||
if v, ok := c.(InterpValue); ok {
|
||||
if v, ok := c.(Value); ok {
|
||||
var vs []interface{}
|
||||
for _, s := range v.ExtKeys() {
|
||||
vs = append(vs, s)
|
||||
@ -629,13 +629,13 @@ func (i *Interp) _decode(c interface{}, a []interface{}) interface{} {
|
||||
}
|
||||
|
||||
dv, _, err := decode.Decode(i.evalContext.ctx, bb, decodeFormats,
|
||||
decode.DecodeOptions{
|
||||
decode.Options{
|
||||
Description: opts.Filename,
|
||||
FormatOptions: opts.Remain,
|
||||
},
|
||||
)
|
||||
if dv == nil {
|
||||
var decodeFormatsErr decode.DecodeFormatsError
|
||||
var decodeFormatsErr decode.FormatsError
|
||||
if errors.As(err, &decodeFormatsErr) {
|
||||
var vs []interface{}
|
||||
for _, fe := range decodeFormatsErr.Errs {
|
||||
|
@ -66,12 +66,12 @@ func (ce compileError) Value() interface{} {
|
||||
"column": ce.pos.Column,
|
||||
}
|
||||
}
|
||||
func (ee compileError) Error() string {
|
||||
filename := ee.filename
|
||||
func (ce compileError) Error() string {
|
||||
filename := ce.filename
|
||||
if filename == "" {
|
||||
filename = "src"
|
||||
}
|
||||
return fmt.Sprintf("%s:%d:%d: %s: %s", filename, ee.pos.Line, ee.pos.Column, ee.what, ee.err.Error())
|
||||
return fmt.Sprintf("%s:%d:%d: %s: %s", filename, ce.pos.Line, ce.pos.Column, ce.what, ce.err.Error())
|
||||
}
|
||||
|
||||
var ErrEOF = io.EOF
|
||||
@ -169,7 +169,7 @@ func (o CtxWriter) Write(p []byte) (n int, err error) {
|
||||
return o.Writer.Write(p)
|
||||
}
|
||||
|
||||
type InterpValue interface {
|
||||
type Value interface {
|
||||
gojq.JQValue
|
||||
|
||||
DisplayName() string
|
||||
@ -313,14 +313,14 @@ func toBufferEx(v interface{}, inArray bool) (*bitio.Buffer, error) {
|
||||
if inArray {
|
||||
b := [1]byte{byte(bi.Uint64())}
|
||||
return bitio.NewBufferFromBytes(b[:], -1), nil
|
||||
} else {
|
||||
padBefore := (8 - (bi.BitLen() % 8)) % 8
|
||||
bb, err := bitio.NewBufferFromBytes(bi.Bytes(), -1).BitBufRange(int64(padBefore), int64(bi.BitLen()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bb, nil
|
||||
}
|
||||
|
||||
padBefore := (8 - (bi.BitLen() % 8)) % 8
|
||||
bb, err := bitio.NewBufferFromBytes(bi.Bytes(), -1).BitBufRange(int64(padBefore), int64(bi.BitLen()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bb, nil
|
||||
case []interface{}:
|
||||
var rr []bitio.BitReadAtSeeker
|
||||
// TODO: optimize byte array case
|
||||
|
@ -30,9 +30,8 @@ func previewValue(v *decode.Value) string {
|
||||
case bool:
|
||||
if vv {
|
||||
return "true"
|
||||
} else {
|
||||
return "false"
|
||||
}
|
||||
return "false"
|
||||
case int64:
|
||||
// TODO: DisplayFormat is weird
|
||||
return num.PadFormatInt(vv, decode.DisplayFormatToBase(v.DisplayFormat), true, 0)
|
||||
@ -44,24 +43,21 @@ func previewValue(v *decode.Value) string {
|
||||
case string:
|
||||
if len(vv) > 50 {
|
||||
return fmt.Sprintf("%q", vv[0:50]) + "..."
|
||||
} else {
|
||||
return fmt.Sprintf("%q", vv)
|
||||
}
|
||||
return fmt.Sprintf("%q", vv)
|
||||
case []byte:
|
||||
if len(vv) > 16 {
|
||||
return hex.EncodeToString(vv[0:16]) + "..."
|
||||
} else {
|
||||
return hex.EncodeToString(vv)
|
||||
}
|
||||
return hex.EncodeToString(vv)
|
||||
case *bitio.Buffer:
|
||||
vvLen := vv.Len()
|
||||
if vvLen > 16*8 {
|
||||
bs, _ := vv.BytesRange(0, 16)
|
||||
return hex.EncodeToString(bs) + "..."
|
||||
} else {
|
||||
bs, _ := vv.BytesRange(0, int(bitio.BitsByteCount(vvLen)))
|
||||
return hex.EncodeToString(bs)
|
||||
}
|
||||
bs, _ := vv.BytesRange(0, int(bitio.BitsByteCount(vvLen)))
|
||||
return hex.EncodeToString(bs)
|
||||
case nil:
|
||||
return "none"
|
||||
|
||||
|
@ -34,7 +34,7 @@ func (err notUpdateableError) Error() string {
|
||||
|
||||
// TODO: rename
|
||||
type valueIf interface {
|
||||
InterpValue
|
||||
Value
|
||||
ToBuffer
|
||||
}
|
||||
|
||||
@ -216,37 +216,37 @@ func (v decodeValue) JQValueKey(name string) interface{} {
|
||||
|
||||
// string (*bitio.Buffer)
|
||||
|
||||
var _ valueIf = bufferDecodeValue{}
|
||||
var _ valueIf = BufferDecodeValue{}
|
||||
|
||||
type bufferDecodeValue struct {
|
||||
type BufferDecodeValue struct {
|
||||
gojqextra.Base
|
||||
decodeValueBase
|
||||
*bitio.Buffer
|
||||
}
|
||||
|
||||
func NewStringBufferValueObject(dv *decode.Value, bb *bitio.Buffer) bufferDecodeValue {
|
||||
return bufferDecodeValue{
|
||||
func NewStringBufferValueObject(dv *decode.Value, bb *bitio.Buffer) BufferDecodeValue {
|
||||
return BufferDecodeValue{
|
||||
decodeValueBase: decodeValueBase{dv},
|
||||
Base: gojqextra.Base{Typ: "string"},
|
||||
Buffer: bb,
|
||||
}
|
||||
}
|
||||
|
||||
func (v bufferDecodeValue) JQValueKey(name string) interface{} {
|
||||
func (v BufferDecodeValue) JQValueKey(name string) interface{} {
|
||||
return valueUnderscoreKey(name, v.decodeValueBase.JQValueKey, v.Base.JQValueKey)
|
||||
}
|
||||
|
||||
func (v bufferDecodeValue) JQValueLength() interface{} {
|
||||
func (v BufferDecodeValue) JQValueLength() interface{} {
|
||||
return int(v.Buffer.Len()) / 8
|
||||
}
|
||||
func (v bufferDecodeValue) JQValueIndex(index int) interface{} {
|
||||
func (v BufferDecodeValue) JQValueIndex(index int) interface{} {
|
||||
if index < 0 {
|
||||
return ""
|
||||
}
|
||||
// TODO: funcIndexSlice, string outside should return "" not null
|
||||
return v.JQValueSlice(index, index+1)
|
||||
}
|
||||
func (v bufferDecodeValue) JQValueSlice(start int, end int) interface{} {
|
||||
func (v BufferDecodeValue) JQValueSlice(start int, end int) interface{} {
|
||||
bb := v.Buffer.Copy()
|
||||
if start != 0 {
|
||||
if _, err := bb.SeekAbs(int64(start) * 8); err != nil {
|
||||
@ -259,23 +259,23 @@ func (v bufferDecodeValue) JQValueSlice(start int, end int) interface{} {
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
func (v bufferDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
func (v BufferDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "string"}
|
||||
}
|
||||
func (v bufferDecodeValue) JQValueToNumber() interface{} {
|
||||
func (v BufferDecodeValue) JQValueToNumber() interface{} {
|
||||
s, ok := v.JQValueToString().(string)
|
||||
if ok {
|
||||
gojq.NormalizeNumbers(s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
func (v bufferDecodeValue) JQValueToString() interface{} {
|
||||
func (v BufferDecodeValue) JQValueToString() interface{} {
|
||||
return v.JQValueSlice(0, int(v.Buffer.Len())/8)
|
||||
}
|
||||
func (v bufferDecodeValue) JQValueToGoJQ() interface{} {
|
||||
func (v BufferDecodeValue) JQValueToGoJQ() interface{} {
|
||||
return v.JQValueToString()
|
||||
}
|
||||
func (v bufferDecodeValue) JQValueToGoJQEx(opts Options) interface{} {
|
||||
func (v BufferDecodeValue) JQValueToGoJQEx(opts Options) interface{} {
|
||||
s, err := opts.BitsFormatFn(v.Buffer.Copy())
|
||||
if err != nil {
|
||||
return err
|
||||
@ -285,66 +285,66 @@ func (v bufferDecodeValue) JQValueToGoJQEx(opts Options) interface{} {
|
||||
|
||||
// decode value array
|
||||
|
||||
var _ valueIf = arrayDecodeValue{}
|
||||
var _ valueIf = ArrayDecodeValue{}
|
||||
|
||||
type arrayDecodeValue struct {
|
||||
type ArrayDecodeValue struct {
|
||||
gojqextra.Base
|
||||
decodeValueBase
|
||||
decode.Array
|
||||
}
|
||||
|
||||
func NewArrayDecodeValue(dv *decode.Value, a decode.Array) arrayDecodeValue {
|
||||
return arrayDecodeValue{
|
||||
func NewArrayDecodeValue(dv *decode.Value, a decode.Array) ArrayDecodeValue {
|
||||
return ArrayDecodeValue{
|
||||
decodeValueBase: decodeValueBase{dv},
|
||||
Base: gojqextra.Base{Typ: "array"},
|
||||
Array: a,
|
||||
}
|
||||
}
|
||||
|
||||
func (v arrayDecodeValue) JQValueKey(name string) interface{} {
|
||||
func (v ArrayDecodeValue) JQValueKey(name string) interface{} {
|
||||
return valueUnderscoreKey(name, v.decodeValueBase.JQValueKey, v.Base.JQValueKey)
|
||||
}
|
||||
func (v arrayDecodeValue) JQValueSliceLen() interface{} { return len(v.Array) }
|
||||
func (v arrayDecodeValue) JQValueLength() interface{} { return len(v.Array) }
|
||||
func (v arrayDecodeValue) JQValueIndex(index int) interface{} {
|
||||
func (v ArrayDecodeValue) JQValueSliceLen() interface{} { return len(v.Array) }
|
||||
func (v ArrayDecodeValue) JQValueLength() interface{} { return len(v.Array) }
|
||||
func (v ArrayDecodeValue) JQValueIndex(index int) interface{} {
|
||||
// -1 outside after string, -2 outside before string
|
||||
if index < 0 {
|
||||
return nil
|
||||
}
|
||||
return makeDecodeValue(v.Array[index])
|
||||
}
|
||||
func (v arrayDecodeValue) JQValueSlice(start int, end int) interface{} {
|
||||
func (v ArrayDecodeValue) JQValueSlice(start int, end int) interface{} {
|
||||
vs := make([]interface{}, end-start)
|
||||
for i, e := range v.Array[start:end] {
|
||||
vs[i] = makeDecodeValue(e)
|
||||
}
|
||||
return vs
|
||||
}
|
||||
func (v arrayDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
func (v ArrayDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "array"}
|
||||
}
|
||||
func (v arrayDecodeValue) JQValueEach() interface{} {
|
||||
func (v ArrayDecodeValue) JQValueEach() interface{} {
|
||||
props := make([]gojq.PathValue, len(v.Array))
|
||||
for i, f := range v.Array {
|
||||
props[i] = gojq.PathValue{Path: i, Value: makeDecodeValue(f)}
|
||||
}
|
||||
return props
|
||||
}
|
||||
func (v arrayDecodeValue) JQValueKeys() interface{} {
|
||||
func (v ArrayDecodeValue) JQValueKeys() interface{} {
|
||||
vs := make([]interface{}, len(v.Array))
|
||||
for i := range v.Array {
|
||||
vs[i] = i
|
||||
}
|
||||
return vs
|
||||
}
|
||||
func (v arrayDecodeValue) JQValueHas(key interface{}) interface{} {
|
||||
func (v ArrayDecodeValue) JQValueHas(key interface{}) interface{} {
|
||||
intKey, ok := key.(int)
|
||||
if !ok {
|
||||
return gojqextra.HasKeyTypeError{L: "array", R: fmt.Sprintf("%v", key)}
|
||||
}
|
||||
return intKey >= 0 && intKey < len(v.Array)
|
||||
}
|
||||
func (v arrayDecodeValue) JQValueToGoJQ() interface{} {
|
||||
func (v ArrayDecodeValue) JQValueToGoJQ() interface{} {
|
||||
vs := make([]interface{}, len(v.Array))
|
||||
for i, f := range v.Array {
|
||||
vs[i] = makeDecodeValue(f)
|
||||
@ -354,25 +354,25 @@ func (v arrayDecodeValue) JQValueToGoJQ() interface{} {
|
||||
|
||||
// decode value struct
|
||||
|
||||
var _ valueIf = structDecodeValue{}
|
||||
var _ valueIf = StructDecodeValue{}
|
||||
|
||||
type structDecodeValue struct {
|
||||
type StructDecodeValue struct {
|
||||
gojqextra.Base
|
||||
decodeValueBase
|
||||
decode.Struct
|
||||
}
|
||||
|
||||
func NewStructDecodeValue(dv *decode.Value, s decode.Struct) structDecodeValue {
|
||||
return structDecodeValue{
|
||||
func NewStructDecodeValue(dv *decode.Value, s decode.Struct) StructDecodeValue {
|
||||
return StructDecodeValue{
|
||||
decodeValueBase: decodeValueBase{dv},
|
||||
Base: gojqextra.Base{Typ: "object"},
|
||||
Struct: s,
|
||||
}
|
||||
}
|
||||
|
||||
func (v structDecodeValue) JQValueLength() interface{} { return len(v.Struct) }
|
||||
func (v structDecodeValue) JQValueSliceLen() interface{} { return len(v.Struct) }
|
||||
func (v structDecodeValue) JQValueKey(name string) interface{} {
|
||||
func (v StructDecodeValue) JQValueLength() interface{} { return len(v.Struct) }
|
||||
func (v StructDecodeValue) JQValueSliceLen() interface{} { return len(v.Struct) }
|
||||
func (v StructDecodeValue) JQValueKey(name string) interface{} {
|
||||
if strings.HasPrefix(name, "_") {
|
||||
return v.decodeValueBase.JQValueKey(name)
|
||||
}
|
||||
@ -384,24 +384,24 @@ func (v structDecodeValue) JQValueKey(name string) interface{} {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (v structDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
func (v StructDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
|
||||
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "object"}
|
||||
}
|
||||
func (v structDecodeValue) JQValueEach() interface{} {
|
||||
func (v StructDecodeValue) JQValueEach() interface{} {
|
||||
props := make([]gojq.PathValue, len(v.Struct))
|
||||
for i, f := range v.Struct {
|
||||
props[i] = gojq.PathValue{Path: f.Name, Value: makeDecodeValue(f)}
|
||||
}
|
||||
return props
|
||||
}
|
||||
func (v structDecodeValue) JQValueKeys() interface{} {
|
||||
func (v StructDecodeValue) JQValueKeys() interface{} {
|
||||
vs := make([]interface{}, len(v.Struct))
|
||||
for i, f := range v.Struct {
|
||||
vs[i] = f.Name
|
||||
}
|
||||
return vs
|
||||
}
|
||||
func (v structDecodeValue) JQValueHas(key interface{}) interface{} {
|
||||
func (v StructDecodeValue) JQValueHas(key interface{}) interface{} {
|
||||
stringKey, ok := key.(string)
|
||||
if !ok {
|
||||
return gojqextra.HasKeyTypeError{L: "object", R: fmt.Sprintf("%v", key)}
|
||||
@ -413,7 +413,7 @@ func (v structDecodeValue) JQValueHas(key interface{}) interface{} {
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (v structDecodeValue) JQValueToGoJQ() interface{} {
|
||||
func (v StructDecodeValue) JQValueToGoJQ() interface{} {
|
||||
vm := make(map[string]interface{}, len(v.Struct))
|
||||
for _, f := range v.Struct {
|
||||
vm[f.Name] = makeDecodeValue(f)
|
||||
|
Loading…
Reference in New Issue
Block a user