From 3ff0c9b5e0f6a5e5eec0e4c1d066c662f0838ff6 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Mon, 27 Sep 2021 11:01:14 +0200 Subject: [PATCH] lint: Enable errcheck adnd revive --- .golangci.yml | 3 +- format/all/all.go | 1 + format/av1/av1_obu.go | 1 + format/default.go | 3 + format/elf/elf.go | 2 + .../matroska/ebml_matroska/ebml_matroska.go | 1 + .../ebml_matroska/ebml_matroska_gen.go | 1 + format/mp4/boxes.go | 4 +- format/mpeg/aac_frame.go | 2 + format/mpeg/mpeg_es.go | 1 + format/mpeg/mpeg_spu.go | 1 + format/registry/registry.go | 6 +- format/tiff/tags.go | 1 + format/vpx/vp9_frame.go | 1 + .../progressreaderseeker.go | 10 +- pkg/bitio/bitio.go | 14 +-- pkg/bitio/rw64.go | 108 +++++++++--------- pkg/crc/crc.go | 2 +- pkg/decode/decode.go | 31 +++-- pkg/decode/numbers.go | 8 +- pkg/interp/buffer.go | 2 +- pkg/interp/dump.go | 12 +- pkg/interp/funcs.go | 8 +- pkg/interp/interp.go | 22 ++-- pkg/interp/preview.go | 14 +-- pkg/interp/value.go | 80 ++++++------- 26 files changed, 173 insertions(+), 166 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 7ac977d2e..4b953998a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,7 @@ linters: enable: + - revive + - errname - goimports - nilerr - forcetypeassert @@ -33,4 +35,3 @@ linters-settings: # allow md5 - G401 - G501 - unused: diff --git a/format/all/all.go b/format/all/all.go index 7f60a33b2..1c845339e 100644 --- a/format/all/all.go +++ b/format/all/all.go @@ -1,4 +1,5 @@ // Package all imports and registers all formats in the default registry +//nolint:revive package all import ( diff --git a/format/av1/av1_obu.go b/format/av1/av1_obu.go index 27e610bc6..b8b31904d 100644 --- a/format/av1/av1_obu.go +++ b/format/av1/av1_obu.go @@ -14,6 +14,7 @@ func init() { }) } +//nolint:revive const ( OBU_SEQUENCE_HEADER = 1 OBU_TEMPORAL_DELIMITER = 2 diff --git a/format/default.go b/format/default.go index 4ca01ed47..6f259e06f 100644 --- a/format/default.go +++ b/format/default.go @@ -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 diff --git a/format/elf/elf.go b/format/elf/elf.go index 370b88055..f32bd5110 100644 --- a/format/elf/elf.go +++ b/format/elf/elf.go @@ -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 diff --git a/format/matroska/ebml_matroska/ebml_matroska.go b/format/matroska/ebml_matroska/ebml_matroska.go index 42d541bfe..afbc2781b 100644 --- a/format/matroska/ebml_matroska/ebml_matroska.go +++ b/format/matroska/ebml_matroska/ebml_matroska.go @@ -1,3 +1,4 @@ +//nolint:revive package ebml_matroska // https://raw.githubusercontent.com/cellar-wg/matroska-specification/aa2144a58b661baf54b99bab41113d66b0f5ff62/ebml_matroska.xml diff --git a/format/matroska/ebml_matroska/ebml_matroska_gen.go b/format/matroska/ebml_matroska/ebml_matroska_gen.go index 78da6f496..5ce8dbf5c 100644 --- a/format/matroska/ebml_matroska/ebml_matroska_gen.go +++ b/format/matroska/ebml_matroska/ebml_matroska_gen.go @@ -1,4 +1,5 @@ // Code below generated from ebml_matroska.xml +//nolint:revive package ebml_matroska import "github.com/wader/fq/format/matroska/ebml" diff --git a/format/mp4/boxes.go b/format/mp4/boxes.go index 149a3f437..a37597f51 100644 --- a/format/mp4/boxes.go +++ b/format/mp4/boxes.go @@ -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) } } diff --git a/format/mpeg/aac_frame.go b/format/mpeg/aac_frame.go index 90b87d047..326d4ce63 100644 --- a/format/mpeg/aac_frame.go +++ b/format/mpeg/aac_frame.go @@ -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 diff --git a/format/mpeg/mpeg_es.go b/format/mpeg/mpeg_es.go index 4a856f301..66d1666fe 100644 --- a/format/mpeg/mpeg_es.go +++ b/format/mpeg/mpeg_es.go @@ -1,3 +1,4 @@ +//nolint:revive package mpeg import ( diff --git a/format/mpeg/mpeg_spu.go b/format/mpeg/mpeg_spu.go index ccc0a5420..d8d1dace8 100644 --- a/format/mpeg/mpeg_spu.go +++ b/format/mpeg/mpeg_spu.go @@ -21,6 +21,7 @@ func init() { }) } +//nolint:revive const ( CMD_END = 0xff FSTA_DSP = 0x00 diff --git a/format/registry/registry.go b/format/registry/registry.go index 731d48dbe..1c076f280 100644 --- a/format/registry/registry.go +++ b/format/registry/registry.go @@ -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 { diff --git a/format/tiff/tags.go b/format/tiff/tags.go index 7399425e6..a3e62cd81 100644 --- a/format/tiff/tags.go +++ b/format/tiff/tags.go @@ -1,3 +1,4 @@ +//nolint:revive package tiff const ( diff --git a/format/vpx/vp9_frame.go b/format/vpx/vp9_frame.go index a7f141259..7c06ed02a 100644 --- a/format/vpx/vp9_frame.go +++ b/format/vpx/vp9_frame.go @@ -24,6 +24,7 @@ var vp9FeatureIDNames = map[uint64]string{ vp9FeatureChromaSubsampling: "Chroma Subsampling", } +//nolint:revive const ( CS_UNKNOWN = 0 CS_BT_601 = 1 diff --git a/internal/progressreadseeker/progressreaderseeker.go b/internal/progressreadseeker/progressreaderseeker.go index 801b35c16..50b9704b1 100644 --- a/internal/progressreadseeker/progressreaderseeker.go +++ b/internal/progressreadseeker/progressreaderseeker.go @@ -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 diff --git a/pkg/bitio/bitio.go b/pkg/bitio/bitio.go index 50bf04947..73bdeae4e 100644 --- a/pkg/bitio/bitio.go +++ b/pkg/bitio/bitio.go @@ -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) diff --git a/pkg/bitio/rw64.go b/pkg/bitio/rw64.go index 22e07c5f4..ddaa8a4cd 100644 --- a/pkg/bitio/rw64.go +++ b/pkg/bitio/rw64.go @@ -56,33 +56,33 @@ func Read64(buf []byte, firstBit int, nBits int) uint64 { } // 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 { + n = n<> (8 - bitsLeft)) + // 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<> (8 - bitsLeft)) - // done - return n - } + // neither byteBitPos or bitsLeft byte aligned + byteBitsLeft := (8 - byteBitPos) & 0x7 + if bitsLeft >= byteBitsLeft { + n = n<= byteBitsLeft { - n = n<>(byteBitsLeft-bitsLeft) - // done - return n - } + n = n<>(byteBitsLeft-bitsLeft) + // done + return n } } } @@ -134,37 +134,37 @@ func Write64(v uint64, nBits int, buf []byte, firstBit int) { } // 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 { + extraBits := 8 - bitsLeft + buf[bytePos] = byte(v)<= 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)<= byteBitsLeft { + bMask := byte((1<>(bitsLeft-byteBitsLeft)) + bitPos += byteBitsLeft + bitsLeft -= byteBitsLeft } else { - // neither byteBitPos or bitsLeft byte aligned - byteBitsLeft := (8 - byteBitPos) & 0x7 - if bitsLeft >= byteBitsLeft { - bMask := byte((1<>(bitsLeft-byteBitsLeft)) - bitPos += byteBitsLeft - bitsLeft -= byteBitsLeft - } else { - extraBits := byteBitsLeft - bitsLeft - bMask := byte(((1< 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 diff --git a/pkg/decode/decode.go b/pkg/decode/decode.go index a4b81c7fc..1073697d1 100644 --- a/pkg/decode/decode.go +++ b/pkg/decode/decode.go @@ -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, diff --git a/pkg/decode/numbers.go b/pkg/decode/numbers.go index 3ce21e259..98ebaf188 100644 --- a/pkg/decode/numbers.go +++ b/pkg/decode/numbers.go @@ -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 { diff --git a/pkg/interp/buffer.go b/pkg/interp/buffer.go index 1ddd460ec..3f0e8690a 100644 --- a/pkg/interp/buffer.go +++ b/pkg/interp/buffer.go @@ -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 { diff --git a/pkg/interp/dump.go b/pkg/interp/dump.go index 6496e7bf5..06753c9b0 100644 --- a/pkg/interp/dump.go +++ b/pkg/interp/dump.go @@ -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 { diff --git a/pkg/interp/funcs.go b/pkg/interp/funcs.go index 52d5e5645..93cb22e68 100644 --- a/pkg/interp/funcs.go +++ b/pkg/interp/funcs.go @@ -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 { diff --git a/pkg/interp/interp.go b/pkg/interp/interp.go index d1875ae73..499f1e314 100644 --- a/pkg/interp/interp.go +++ b/pkg/interp/interp.go @@ -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 diff --git a/pkg/interp/preview.go b/pkg/interp/preview.go index 3f4769488..59987912a 100644 --- a/pkg/interp/preview.go +++ b/pkg/interp/preview.go @@ -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" diff --git a/pkg/interp/value.go b/pkg/interp/value.go index 18deecedf..4fe92804e 100644 --- a/pkg/interp/value.go +++ b/pkg/interp/value.go @@ -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)