Skip to content

Commit

Permalink
fq: Use go 1.20 and cleanup
Browse files Browse the repository at this point in the history
Also rename *ex packages to *x
  • Loading branch information
wader committed Apr 1, 2024
1 parent 4835102 commit bf7fa07
Show file tree
Hide file tree
Showing 56 changed files with 245 additions and 311 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ apk add -X http://dl-cdn.alpinelinux.org/alpine/edge/testing fq

### Build from source

Make sure you have [go](https://go.dev) 1.18 or later installed.
Make sure you have [go](https://go.dev) 1.20 or later installed.

To install directly from git repository (no clone needed):
```sh
Expand Down
4 changes: 2 additions & 2 deletions format/cbor/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"

"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
Expand Down Expand Up @@ -117,7 +117,7 @@ func decodeCBORValue(d *decode.D) any {
}},
majorTypeNegativeInt: {s: scalar.Uint{Sym: "negative_int"}, d: func(d *decode.D, shortCount uint64, count uint64) any {
n := new(big.Int)
n.SetUint64(count).Neg(n).Sub(n, mathex.BigIntOne)
n.SetUint64(count).Neg(n).Sub(n, mathx.BigIntOne)
d.FieldValueBigInt("value", n)
return nil
}},
Expand Down
10 changes: 5 additions & 5 deletions format/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"io"

"github.com/wader/fq/format"
"github.com/wader/fq/internal/gojqex"
"github.com/wader/fq/internal/gojqx"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
Expand Down Expand Up @@ -83,19 +83,19 @@ func toCSV(_ *interp.Interp, c []any, opts ToCSVOpts) any {
w.Comma = rune(opts.Comma[0])
}
for _, row := range c {
rs, ok := gojqex.Cast[[]any](row)
rs, ok := gojqx.Cast[[]any](row)
if !ok {
return fmt.Errorf("expected row to be an array, got %s", gojqex.TypeErrorPreview(row))
return fmt.Errorf("expected row to be an array, got %s", gojqx.TypeErrorPreview(row))
}
vs, ok := gojqex.NormalizeToStrings(rs).([]any)
vs, ok := gojqx.NormalizeToStrings(rs).([]any)
if !ok {
panic("not array")
}
var ss []string
for _, v := range vs {
s, ok := v.(string)
if !ok {
return fmt.Errorf("expected row record to be scalars, got %s", gojqex.TypeErrorPreview(v))
return fmt.Errorf("expected row record to be scalars, got %s", gojqx.TypeErrorPreview(v))
}
ss = append(ss, s)
}
Expand Down
4 changes: 2 additions & 2 deletions format/fit/fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package fit
import (
"embed"
"fmt"
"sort"
"slices"

"github.com/wader/fq/format"
"github.com/wader/fq/format/fit/mappers"
Expand Down Expand Up @@ -344,7 +344,7 @@ func fitDecodeDataMessage(d *decode.D, drc *dataRecordContext, lmfd localFieldDe
keys[i] = int(k)
i++
}
sort.Ints(keys)
slices.Sort(keys)

isDevDep := isDevMap[drc.localMessageType]

Expand Down
4 changes: 2 additions & 2 deletions format/flac/flac.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"

"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
Expand Down Expand Up @@ -65,7 +65,7 @@ func flacDecode(d *decode.D) any {

samplesInFrame := ffo.Samples
if streamTotalSamples > 0 {
samplesInFrame = mathex.Min(streamTotalSamples-streamDecodedSamples, ffo.Samples)
samplesInFrame = mathx.Min(streamTotalSamples-streamDecodedSamples, ffo.Samples)
}
frameStreamSamplesBuf := ffo.SamplesBuf[0 : samplesInFrame*uint64(ffo.Channels*ffo.BitsPerSample/8)]
framesNDecodedSamples += ffo.Samples
Expand Down
4 changes: 2 additions & 2 deletions format/flac/flac_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math/bits"

"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/checksum"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
Expand Down Expand Up @@ -478,7 +478,7 @@ func frameDecode(d *decode.D) any {
for j := 0; j < count; j++ {
high := d.Unary(0)
low := d.U(riceParameter)
samples[n] = mathex.ZigZag[uint64, int64](high<<riceParameter | low)
samples[n] = mathx.ZigZag[uint64, int64](high<<riceParameter | low)
n++
}
samplesStop := d.Pos()
Expand Down
4 changes: 2 additions & 2 deletions format/icc/icc_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package icc

import (
"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
)
Expand Down Expand Up @@ -166,7 +166,7 @@ func iccProfileDecode(d *decode.D) any {
// was. instead add alignment after if offset+size does not align and to be sure clamp it if outside buffer.
alignStart := int64(offset) + int64(size)
alignBytes := (4 - (int64(offset)+int64(size))%4) % 4
alignBytes = mathex.Min(d.Len()/8-alignStart, alignBytes)
alignBytes = mathx.Min(d.Len()/8-alignStart, alignBytes)
if alignBytes != 0 {
d.RangeFn(alignStart*8, alignBytes*8, func(d *decode.D) {
d.FieldRawLen("alignment", d.BitsLeft())
Expand Down
4 changes: 2 additions & 2 deletions format/inet/ipv6_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net"

"github.com/wader/fq/format"
"github.com/wader/fq/internal/bitioex"
"github.com/wader/fq/internal/bitiox"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
Expand Down Expand Up @@ -105,7 +105,7 @@ var hopByHopTypeNames = scalar.UintMapSymStr{

var mapUToIPv6Sym = scalar.BitBufFn(func(s scalar.BitBuf) (scalar.BitBuf, error) {
b := &bytes.Buffer{}
if _, err := bitioex.CopyBits(b, s.Actual); err != nil {
if _, err := bitiox.CopyBits(b, s.Actual); err != nil {
return s, err
}
s.Sym = net.IP(b.Bytes()).String()
Expand Down
4 changes: 2 additions & 2 deletions format/leveldb/leveldb_log_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package leveldb
// - MANIFEST-*

import (
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/scalar"
)
Expand Down Expand Up @@ -49,7 +49,7 @@ func readBlockSequence(rro recordReadOptions, d *decode.D) {

d.FieldArray("blocks", func(d *decode.D) {
for d.BitsLeft() >= headerSize {
d.LimitedFn(mathex.Min(blockSize, d.BitsLeft()), func(d *decode.D) {
d.LimitedFn(mathx.Min(blockSize, d.BitsLeft()), func(d *decode.D) {
d.FieldStruct("block", bind(readLogBlock, rro))
})
}
Expand Down
6 changes: 3 additions & 3 deletions format/mp4/mp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package mp4
// TODO: better probe, find first 2 boxes, should be free,ftyp or mdat?

import (
"cmp"
"embed"
"fmt"

"github.com/wader/fq/format"
"github.com/wader/fq/internal/cmpex"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -240,10 +240,10 @@ func mp4Tracks(d *decode.D, ctx *decodeContext) {

// sort by id then order in file
slices.SortStableFunc(tracksCollected, func(a, b *trackCollected) int {
if r := cmpex.Compare(a.track.id, b.track.id); r != 0 {
if r := cmp.Compare(a.track.id, b.track.id); r != 0 {
return r
}
return cmpex.Compare(a.order, b.order)
return cmp.Compare(a.order, b.order)
})

d.FieldArray("tracks", func(d *decode.D) {
Expand Down
4 changes: 2 additions & 2 deletions format/mpeg/avc_nalu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package mpeg

import (
"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
Expand Down Expand Up @@ -50,7 +50,7 @@ func uEV(d *decode.D) uint64 { return expGolomb(d) }

func sEV(d *decode.D) int64 {
v := expGolomb(d) + 1
return mathex.ZigZag[uint64, int64](v) - -int64(v&1)
return mathx.ZigZag[uint64, int64](v) - -int64(v&1)
}

const (
Expand Down
6 changes: 3 additions & 3 deletions format/protobuf/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"embed"

"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
Expand Down Expand Up @@ -70,7 +70,7 @@ func protobufDecodeField(d *decode.D, pbm *format.ProtoBufMessage) {

switch pbf.Type {
case format.ProtoBufTypeInt32, format.ProtoBufTypeInt64:
v := mathex.ZigZag[uint64, int64](value)
v := mathx.ZigZag[uint64, int64](value)
d.FieldValueSint("value", v)
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
Expand All @@ -82,7 +82,7 @@ func protobufDecodeField(d *decode.D, pbm *format.ProtoBufMessage) {
}
case format.ProtoBufTypeSInt32, format.ProtoBufTypeSInt64:
// TODO: correct? 32 different?
v := mathex.TwosComplement(64, value)
v := mathx.TwosComplement(64, value)
d.FieldValueSint("value", v)
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
Expand Down
20 changes: 10 additions & 10 deletions format/text/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package text
import (
"net/url"

"github.com/wader/fq/internal/gojqex"
"github.com/wader/fq/internal/gojqx"
"github.com/wader/fq/pkg/interp"
)

Expand Down Expand Up @@ -56,23 +56,23 @@ func init() {
toURLValues := func(c map[string]any) url.Values {
qv := url.Values{}
for k, v := range c {
if va, ok := gojqex.Cast[[]any](v); ok {
if va, ok := gojqx.Cast[[]any](v); ok {
var ss []string
for _, s := range va {
if s, ok := gojqex.Cast[string](s); ok {
if s, ok := gojqx.Cast[string](s); ok {
ss = append(ss, s)
}
}
qv[k] = ss
} else if vs, ok := gojqex.Cast[string](v); ok {
} else if vs, ok := gojqx.Cast[string](v); ok {
qv[k] = []string{vs}
}
}
return qv
}
interp.RegisterFunc0("to_urlquery", func(_ *interp.Interp, c map[string]any) any {
// TODO: nicer
c, ok := gojqex.NormalizeToStrings(c).(map[string]any)
c, ok := gojqx.NormalizeToStrings(c).(map[string]any)
if !ok {
panic("not map")
}
Expand Down Expand Up @@ -118,20 +118,20 @@ func init() {
})
interp.RegisterFunc0("to_url", func(_ *interp.Interp, c map[string]any) any {
// TODO: nicer
c, ok := gojqex.NormalizeToStrings(c).(map[string]any)
c, ok := gojqx.NormalizeToStrings(c).(map[string]any)
if !ok {
panic("not map")
}

str := func(v any) string { s, _ := gojqex.Cast[string](v); return s }
str := func(v any) string { s, _ := gojqx.Cast[string](v); return s }
u := url.URL{
Scheme: str(c["scheme"]),
Host: str(c["host"]),
Path: str(c["path"]),
Fragment: str(c["fragment"]),
}

if um, ok := gojqex.Cast[map[string]any](c["user"]); ok {
if um, ok := gojqx.Cast[map[string]any](c["user"]); ok {
username, password := str(um["username"]), str(um["password"])
if username != "" {
if password == "" {
Expand All @@ -141,10 +141,10 @@ func init() {
}
}
}
if s, ok := gojqex.Cast[string](c["rawquery"]); ok {
if s, ok := gojqx.Cast[string](c["rawquery"]); ok {
u.RawQuery = s
}
if qm, ok := gojqex.Cast[map[string]any](c["query"]); ok {
if qm, ok := gojqx.Cast[map[string]any](c["query"]); ok {
u.RawQuery = toURLValues(qm).Encode()
}

Expand Down
8 changes: 4 additions & 4 deletions format/toml/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/BurntSushi/toml"
"github.com/wader/fq/format"
"github.com/wader/fq/internal/gojqex"
"github.com/wader/fq/internal/gojqx"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
Expand Down Expand Up @@ -69,7 +69,7 @@ func decodeTOML(d *decode.D) any {
d.Fatalf("%s", err)
}
var s scalar.Any
s.Actual = gojqex.Normalize(r)
s.Actual = gojqx.Normalize(r)

// TODO: better way to handle that an empty file is valid toml and parsed as an object
switch v := s.Actual.(type) {
Expand All @@ -90,11 +90,11 @@ func decodeTOML(d *decode.D) any {

func toTOML(_ *interp.Interp, c any) any {
if c == nil {
return gojqex.FuncTypeError{Name: "to_toml", V: c}
return gojqx.FuncTypeError{Name: "to_toml", V: c}
}

b := &bytes.Buffer{}
if err := toml.NewEncoder(b).Encode(gojqex.Normalize(c)); err != nil {
if err := toml.NewEncoder(b).Encode(gojqx.Normalize(c)); err != nil {
return err
}
return b.String()
Expand Down
Loading

0 comments on commit bf7fa07

Please sign in to comment.