2020-06-08 03:29:51 +03:00
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
2021-10-20 02:28:26 +03:00
|
|
|
"bytes"
|
2020-06-08 03:29:51 +03:00
|
|
|
"errors"
|
2021-08-12 00:51:00 +03:00
|
|
|
"fmt"
|
2020-06-08 03:29:51 +03:00
|
|
|
"io"
|
2021-12-25 18:29:33 +03:00
|
|
|
"io/ioutil"
|
2020-06-08 03:29:51 +03:00
|
|
|
"math/big"
|
|
|
|
"strings"
|
2021-11-29 16:26:11 +03:00
|
|
|
"time"
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2022-01-24 23:21:48 +03:00
|
|
|
"github.com/wader/fq/internal/bitioextra"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/internal/gojqextra"
|
2021-11-16 13:41:14 +03:00
|
|
|
"github.com/wader/fq/internal/ioextra"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/gojq"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
func init() {
|
|
|
|
functionRegisterFns = append(functionRegisterFns, func(i *Interp) []Function {
|
|
|
|
return []Function{
|
2021-12-25 18:29:33 +03:00
|
|
|
{"_registry", 0, 0, i._registry, nil},
|
|
|
|
{"_tovalue", 1, 1, i._toValue, nil},
|
2021-11-16 13:41:14 +03:00
|
|
|
{"_decode", 2, 2, i._decode, nil},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:51:00 +03:00
|
|
|
type expectedExtkeyError struct {
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err expectedExtkeyError) Error() string {
|
|
|
|
return "expected a extkey but got: " + err.Key
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
// TODO: redo/rename
|
|
|
|
// used by _isDecodeValue
|
2021-09-23 19:35:04 +03:00
|
|
|
type DecodeValue interface {
|
2021-09-27 12:01:14 +03:00
|
|
|
Value
|
2022-02-08 20:44:48 +03:00
|
|
|
ToBinary
|
2021-09-23 19:35:04 +03:00
|
|
|
|
|
|
|
DecodeValue() *decode.Value
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-25 18:29:33 +03:00
|
|
|
func (i *Interp) _registry(c interface{}, a []interface{}) interface{} {
|
|
|
|
uniqueFormats := map[string]decode.Format{}
|
|
|
|
|
|
|
|
groups := map[string]interface{}{}
|
|
|
|
formats := map[string]interface{}{}
|
|
|
|
|
|
|
|
for fsName := range i.registry.Groups {
|
|
|
|
var group []interface{}
|
|
|
|
|
|
|
|
for _, f := range i.registry.MustGroup(fsName) {
|
|
|
|
group = append(group, f.Name)
|
|
|
|
if _, ok := uniqueFormats[f.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
uniqueFormats[f.Name] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
groups[fsName] = group
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range uniqueFormats {
|
|
|
|
vf := map[string]interface{}{
|
|
|
|
"name": f.Name,
|
|
|
|
"description": f.Description,
|
|
|
|
"probe_order": f.ProbeOrder,
|
|
|
|
"root_name": f.RootName,
|
|
|
|
"root_array": f.RootArray,
|
2022-01-10 20:25:36 +03:00
|
|
|
"to_repr": f.ToRepr,
|
2021-12-25 18:29:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var dependenciesVs []interface{}
|
|
|
|
for _, d := range f.Dependencies {
|
|
|
|
var dNamesVs []interface{}
|
|
|
|
for _, n := range d.Names {
|
|
|
|
dNamesVs = append(dNamesVs, n)
|
|
|
|
}
|
|
|
|
dependenciesVs = append(dependenciesVs, dNamesVs)
|
|
|
|
}
|
|
|
|
if len(dependenciesVs) > 0 {
|
|
|
|
vf["dependencies"] = dependenciesVs
|
|
|
|
}
|
|
|
|
var groupsVs []interface{}
|
|
|
|
for _, n := range f.Groups {
|
|
|
|
groupsVs = append(groupsVs, n)
|
|
|
|
}
|
|
|
|
if len(groupsVs) > 0 {
|
|
|
|
vf["groups"] = groupsVs
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Files != nil {
|
|
|
|
files := map[string]interface{}{}
|
|
|
|
|
|
|
|
entries, err := f.Files.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range entries {
|
|
|
|
f, err := f.Files.Open(e.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
files[e.Name()] = string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
vf["files"] = files
|
|
|
|
}
|
|
|
|
|
|
|
|
formats[f.Name] = vf
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
"groups": groups,
|
|
|
|
"formats": formats,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
func (i *Interp) _toValue(c interface{}, a []interface{}) interface{} {
|
|
|
|
v, _ := toValue(
|
|
|
|
func() Options { return i.Options(a[0]) },
|
|
|
|
c,
|
|
|
|
)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) _decode(c interface{}, a []interface{}) interface{} {
|
|
|
|
var opts struct {
|
|
|
|
Filename string `mapstructure:"filename"`
|
2021-11-16 15:03:45 +03:00
|
|
|
Force bool `mapstructure:"force"`
|
2021-11-16 13:41:14 +03:00
|
|
|
Progress string `mapstructure:"_progress"`
|
|
|
|
Remain map[string]interface{} `mapstructure:",remain"`
|
|
|
|
}
|
|
|
|
_ = mapstructure.Decode(a[1], &opts)
|
|
|
|
|
|
|
|
// TODO: progress hack
|
|
|
|
// would be nice to move all progress code into decode but it might be
|
|
|
|
// tricky to keep track of absolute positions in the underlaying readers
|
|
|
|
// when it uses BitBuf slices, maybe only in Pos()?
|
|
|
|
if bbf, ok := c.(*openFile); ok {
|
|
|
|
opts.Filename = bbf.filename
|
|
|
|
|
|
|
|
if opts.Progress != "" {
|
|
|
|
evalProgress := func(c interface{}) {
|
|
|
|
// {approx_read_bytes: 123, total_size: 123} | opts.Progress
|
|
|
|
_, _ = i.EvalFuncValues(
|
2022-02-24 14:36:48 +03:00
|
|
|
i.evalInstance.ctx,
|
2021-11-16 13:41:14 +03:00
|
|
|
c,
|
|
|
|
opts.Progress,
|
|
|
|
nil,
|
2022-02-24 14:36:48 +03:00
|
|
|
EvalOpts{output: ioextra.DiscardCtxWriter{Ctx: i.evalInstance.ctx}},
|
2021-11-16 13:41:14 +03:00
|
|
|
)
|
|
|
|
}
|
2021-11-29 16:26:11 +03:00
|
|
|
lastProgress := time.Now()
|
2021-11-16 13:41:14 +03:00
|
|
|
bbf.progressFn = func(approxReadBytes, totalSize int64) {
|
2021-11-29 16:26:11 +03:00
|
|
|
// make sure to not call too often as it's quite expensive
|
|
|
|
n := time.Now()
|
|
|
|
if n.Sub(lastProgress) < 200*time.Millisecond {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lastProgress = n
|
2021-11-16 13:41:14 +03:00
|
|
|
evalProgress(
|
|
|
|
map[string]interface{}{
|
|
|
|
"approx_read_bytes": approxReadBytes,
|
|
|
|
"total_size": totalSize,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// when done decoding, tell progress function were done and disable it
|
|
|
|
defer func() {
|
|
|
|
bbf.progressFn = nil
|
|
|
|
evalProgress(nil)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
bv, err := toBinary(c)
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
formatName, err := toString(a[0])
|
|
|
|
if err != nil {
|
2021-11-22 15:28:18 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
decodeFormat, err := i.registry.Group(formatName)
|
|
|
|
if err != nil {
|
2021-11-22 15:28:18 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-24 14:36:48 +03:00
|
|
|
dv, _, err := decode.Decode(i.evalInstance.ctx, bv.br, decodeFormat,
|
2021-11-16 13:41:14 +03:00
|
|
|
decode.Options{
|
|
|
|
IsRoot: true,
|
|
|
|
FillGaps: true,
|
2021-11-16 15:03:45 +03:00
|
|
|
Force: opts.Force,
|
2021-11-16 13:41:14 +03:00
|
|
|
Range: bv.r,
|
|
|
|
Description: opts.Filename,
|
|
|
|
FormatOptions: opts.Remain,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if dv == nil {
|
|
|
|
var decodeFormatsErr decode.FormatsError
|
|
|
|
if errors.As(err, &decodeFormatsErr) {
|
|
|
|
var vs []interface{}
|
|
|
|
for _, fe := range decodeFormatsErr.Errs {
|
|
|
|
vs = append(vs, fe.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueError{vs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueError{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return makeDecodeValue(dv)
|
|
|
|
}
|
|
|
|
|
2021-09-28 00:06:46 +03:00
|
|
|
func valueKey(name string, a, b func(name string) interface{}) interface{} {
|
2021-08-12 00:51:00 +03:00
|
|
|
if strings.HasPrefix(name, "_") {
|
|
|
|
return a(name)
|
|
|
|
}
|
|
|
|
return b(name)
|
|
|
|
}
|
2021-09-28 00:06:46 +03:00
|
|
|
func valueHas(key interface{}, a func(name string) interface{}, b func(key interface{}) interface{}) interface{} {
|
|
|
|
stringKey, ok := key.(string)
|
|
|
|
if ok && strings.HasPrefix(stringKey, "_") {
|
|
|
|
if err, ok := a(stringKey).(error); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return b(key)
|
|
|
|
}
|
2021-08-12 00:51:00 +03:00
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
// optsFn is a function as toValue is used by tovalue/0 so needs to be fast
|
|
|
|
func toValue(optsFn func() Options, v interface{}) (interface{}, bool) {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case JQValueEx:
|
|
|
|
return v.JQValueToGoJQEx(optsFn), true
|
|
|
|
case gojq.JQValue:
|
|
|
|
return v.JQValueToGoJQ(), true
|
|
|
|
case nil, bool, float64, int, string, *big.Int, map[string]interface{}, []interface{}:
|
|
|
|
return v, true
|
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:44:48 +03:00
|
|
|
func makeDecodeValue(dv *decode.Value) interface{} {
|
2020-06-08 03:29:51 +03:00
|
|
|
switch vv := dv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *decode.Compound:
|
2021-11-03 19:19:33 +03:00
|
|
|
if vv.IsArray {
|
|
|
|
return NewArrayDecodeValue(dv, vv)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
return NewStructDecodeValue(dv, vv)
|
2021-12-03 02:06:11 +03:00
|
|
|
case *scalar.S:
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := vv.Value().(type) {
|
2022-01-24 23:21:48 +03:00
|
|
|
case bitio.ReaderAtSeeker:
|
2021-11-21 23:27:44 +03:00
|
|
|
// is lazy so that in situations where the decode value is only used to
|
2022-02-08 20:44:48 +03:00
|
|
|
// create another binary we don't have to read and create a string, ex:
|
2021-11-21 23:27:44 +03:00
|
|
|
// .unknown0 | tobytes[1:] | ...
|
2021-11-03 19:19:33 +03:00
|
|
|
return decodeValue{
|
2021-11-22 03:23:59 +03:00
|
|
|
JQValue: &gojqextra.Lazy{
|
2021-11-29 16:50:53 +03:00
|
|
|
Type: "string",
|
|
|
|
IsScalar: true,
|
2021-11-22 03:23:59 +03:00
|
|
|
Fn: func() (gojq.JQValue, error) {
|
2021-11-21 23:27:44 +03:00
|
|
|
buf := &bytes.Buffer{}
|
2022-01-24 23:21:48 +03:00
|
|
|
vvC, err := bitioextra.Clone(vv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := bitioextra.CopyBits(buf, vvC); err != nil {
|
2021-11-21 23:27:44 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-22 03:23:59 +03:00
|
|
|
return gojqextra.String([]rune(buf.String())), nil
|
2021-11-21 23:27:44 +03:00
|
|
|
},
|
|
|
|
},
|
2021-11-03 19:19:33 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
bitsFormat: true,
|
|
|
|
}
|
|
|
|
case bool:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Boolean(vv),
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case int:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Number{V: vv},
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case int64:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Number{V: big.NewInt(vv)},
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case uint64:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Number{V: new(big.Int).SetUint64(vv)},
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Number{V: vv},
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.String(vv),
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Array(vv),
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case map[string]interface{}:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Object(vv),
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Null{},
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
2022-01-14 03:58:42 +03:00
|
|
|
case *big.Int:
|
|
|
|
return decodeValue{
|
|
|
|
JQValue: gojqextra.Number{V: vv},
|
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
default:
|
2021-12-03 02:06:11 +03:00
|
|
|
panic(fmt.Sprintf("unreachable vv %#+v", vv))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
default:
|
2021-12-03 02:06:11 +03:00
|
|
|
panic(fmt.Sprintf("unreachable dv %#+v", dv))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type decodeValueBase struct {
|
|
|
|
dv *decode.Value
|
|
|
|
}
|
|
|
|
|
2021-09-23 19:35:04 +03:00
|
|
|
func (dvb decodeValueBase) DecodeValue() *decode.Value {
|
|
|
|
return dvb.dv
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func (dvb decodeValueBase) Display(w io.Writer, opts Options) error { return dump(dvb.dv, w, opts) }
|
2022-02-08 20:44:48 +03:00
|
|
|
func (dvb decodeValueBase) ToBinary() (Binary, error) {
|
|
|
|
return Binary{br: dvb.dv.RootReader, r: dvb.dv.InnerRange(), unit: 8}, nil
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-12-05 03:15:59 +03:00
|
|
|
func (decodeValueBase) ExtType() string { return "decode_value" }
|
2020-06-08 03:29:51 +03:00
|
|
|
func (dvb decodeValueBase) ExtKeys() []string {
|
|
|
|
kv := []string{
|
|
|
|
"_start",
|
|
|
|
"_stop",
|
|
|
|
"_len",
|
|
|
|
"_name",
|
2021-09-28 00:11:18 +03:00
|
|
|
"_root",
|
|
|
|
"_buffer_root",
|
|
|
|
"_format_root",
|
|
|
|
"_parent",
|
2021-11-03 19:19:33 +03:00
|
|
|
"_actual",
|
|
|
|
"_sym",
|
2020-06-08 03:29:51 +03:00
|
|
|
"_description",
|
|
|
|
"_path",
|
|
|
|
"_bits",
|
|
|
|
"_bytes",
|
|
|
|
"_unknown",
|
2021-12-10 20:18:48 +03:00
|
|
|
"_index", // TODO: only if parent is array?
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-03 02:06:11 +03:00
|
|
|
if _, ok := dvb.dv.V.(*decode.Compound); ok {
|
2021-11-03 19:19:33 +03:00
|
|
|
kv = append(kv,
|
|
|
|
"_error",
|
|
|
|
"_format",
|
|
|
|
)
|
2021-12-10 20:18:48 +03:00
|
|
|
|
|
|
|
if dvb.dv.Index != -1 {
|
|
|
|
kv = append(kv, "_index")
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return kv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dvb decodeValueBase) JQValueKey(name string) interface{} {
|
2021-08-12 00:51:00 +03:00
|
|
|
dv := dvb.dv
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "_start":
|
|
|
|
return big.NewInt(dv.Range.Start)
|
|
|
|
case "_stop":
|
|
|
|
return big.NewInt(dv.Range.Stop())
|
|
|
|
case "_len":
|
|
|
|
return big.NewInt(dv.Range.Len)
|
|
|
|
case "_name":
|
|
|
|
return dv.Name
|
2021-09-28 00:11:18 +03:00
|
|
|
case "_root":
|
|
|
|
return makeDecodeValue(dv.Root())
|
|
|
|
case "_buffer_root":
|
|
|
|
// TODO: rename?
|
|
|
|
return makeDecodeValue(dv.BufferRoot())
|
|
|
|
case "_format_root":
|
|
|
|
// TODO: rename?
|
|
|
|
return makeDecodeValue(dv.FormatRoot())
|
|
|
|
case "_parent":
|
|
|
|
if dv.Parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return makeDecodeValue(dv.Parent)
|
2021-11-03 19:19:33 +03:00
|
|
|
case "_actual":
|
|
|
|
switch vv := dv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *scalar.S:
|
2021-11-03 19:19:33 +03:00
|
|
|
jv, ok := gojqextra.ToGoJQValue(vv.Actual)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can't convert actual value jq value %#+v", vv.Actual)
|
|
|
|
}
|
|
|
|
return jv
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case "_sym":
|
|
|
|
switch vv := dv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *scalar.S:
|
2021-11-03 19:19:33 +03:00
|
|
|
jv, ok := gojqextra.ToGoJQValue(vv.Sym)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can't convert sym value jq value %#+v", vv.Actual)
|
|
|
|
}
|
|
|
|
return jv
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-12 00:51:00 +03:00
|
|
|
case "_description":
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := dv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *decode.Compound:
|
2021-11-19 13:44:23 +03:00
|
|
|
if vv.Description == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
return vv.Description
|
2021-12-03 02:06:11 +03:00
|
|
|
case *scalar.S:
|
2021-11-19 13:44:23 +03:00
|
|
|
if vv.Description == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
return vv.Description
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-12 00:51:00 +03:00
|
|
|
case "_path":
|
|
|
|
return valuePath(dv)
|
|
|
|
case "_error":
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := dv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *decode.Compound:
|
2021-11-03 19:19:33 +03:00
|
|
|
var formatErr decode.FormatError
|
|
|
|
if errors.As(vv.Err, &formatErr) {
|
|
|
|
return formatErr.Value()
|
2021-08-12 00:51:00 +03:00
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
return vv.Err
|
|
|
|
default:
|
|
|
|
return nil
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-08-12 00:51:00 +03:00
|
|
|
case "_bits":
|
2022-02-08 20:44:48 +03:00
|
|
|
return Binary{
|
|
|
|
br: dv.RootReader,
|
2021-10-05 23:26:05 +03:00
|
|
|
r: dv.Range,
|
|
|
|
unit: 1,
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
case "_bytes":
|
2022-02-08 20:44:48 +03:00
|
|
|
return Binary{
|
|
|
|
br: dv.RootReader,
|
2021-10-05 23:26:05 +03:00
|
|
|
r: dv.Range,
|
|
|
|
unit: 8,
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
case "_format":
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := dv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *decode.Compound:
|
2021-11-03 19:19:33 +03:00
|
|
|
if vv.Format != nil {
|
|
|
|
return vv.Format.Name
|
|
|
|
}
|
|
|
|
return nil
|
2021-12-03 02:06:11 +03:00
|
|
|
case *scalar.S:
|
2021-11-03 19:19:33 +03:00
|
|
|
// TODO: hack, Scalar interface?
|
|
|
|
switch vv.Actual.(type) {
|
|
|
|
case map[string]interface{}, []interface{}:
|
|
|
|
return "json"
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
2021-08-12 00:51:00 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case "_unknown":
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := dv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *scalar.S:
|
2021-11-03 19:19:33 +03:00
|
|
|
return vv.Unknown
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2021-12-10 20:18:48 +03:00
|
|
|
case "_index":
|
|
|
|
if dv.Index != -1 {
|
|
|
|
return dv.Index
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-08-12 00:51:00 +03:00
|
|
|
|
|
|
|
return expectedExtkeyError{Key: name}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 19:35:04 +03:00
|
|
|
var _ DecodeValue = decodeValue{}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-08-12 02:35:40 +03:00
|
|
|
type decodeValue struct {
|
2021-08-12 00:51:00 +03:00
|
|
|
gojq.JQValue
|
|
|
|
decodeValueBase
|
2021-10-20 02:28:26 +03:00
|
|
|
bitsFormat bool
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 02:35:40 +03:00
|
|
|
func (v decodeValue) JQValueKey(name string) interface{} {
|
2021-09-28 00:06:46 +03:00
|
|
|
return valueKey(name, v.decodeValueBase.JQValueKey, v.JQValue.JQValueKey)
|
|
|
|
}
|
|
|
|
func (v decodeValue) JQValueHas(key interface{}) interface{} {
|
|
|
|
return valueHas(key, v.decodeValueBase.JQValueKey, v.JQValue.JQValueHas)
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
2021-10-20 02:28:26 +03:00
|
|
|
func (v decodeValue) JQValueToGoJQEx(optsFn func() Options) interface{} {
|
|
|
|
if !v.bitsFormat {
|
|
|
|
return v.JQValueToGoJQ()
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
bv, err := v.decodeValueBase.ToBinary()
|
2021-10-20 02:28:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-02-08 20:44:48 +03:00
|
|
|
br, err := bv.toReader()
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
brC, err := bitioextra.Clone(br)
|
2021-10-20 02:28:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-10-20 02:28:26 +03:00
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
s, err := optsFn().BitsFormatFn(brC)
|
2021-08-12 02:35:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode value array
|
|
|
|
|
2021-09-23 19:35:04 +03:00
|
|
|
var _ DecodeValue = ArrayDecodeValue{}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-27 12:01:14 +03:00
|
|
|
type ArrayDecodeValue struct {
|
2021-08-12 00:51:00 +03:00
|
|
|
gojqextra.Base
|
|
|
|
decodeValueBase
|
2021-12-03 02:06:11 +03:00
|
|
|
*decode.Compound
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-03 02:06:11 +03:00
|
|
|
func NewArrayDecodeValue(dv *decode.Value, c *decode.Compound) ArrayDecodeValue {
|
2021-09-27 12:01:14 +03:00
|
|
|
return ArrayDecodeValue{
|
2021-08-12 00:51:00 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
Base: gojqextra.Base{Typ: "array"},
|
2021-12-03 02:06:11 +03:00
|
|
|
Compound: c,
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueKey(name string) interface{} {
|
2021-09-28 00:06:46 +03:00
|
|
|
return valueKey(name, v.decodeValueBase.JQValueKey, v.Base.JQValueKey)
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
2021-12-03 02:06:11 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueSliceLen() interface{} { return len(v.Compound.Children) }
|
|
|
|
func (v ArrayDecodeValue) JQValueLength() interface{} { return len(v.Compound.Children) }
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueIndex(index int) interface{} {
|
2020-06-08 03:29:51 +03:00
|
|
|
// -1 outside after string, -2 outside before string
|
|
|
|
if index < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-03 02:06:11 +03:00
|
|
|
return makeDecodeValue((v.Compound.Children)[index])
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueSlice(start int, end int) interface{} {
|
2020-06-08 03:29:51 +03:00
|
|
|
vs := make([]interface{}, end-start)
|
2021-12-03 02:06:11 +03:00
|
|
|
for i, e := range (v.Compound.Children)[start:end] {
|
2021-08-12 02:35:40 +03:00
|
|
|
vs[i] = makeDecodeValue(e)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueEach() interface{} {
|
2021-12-03 02:06:11 +03:00
|
|
|
props := make([]gojq.PathValue, len(v.Compound.Children))
|
|
|
|
for i, f := range v.Compound.Children {
|
2021-08-12 02:35:40 +03:00
|
|
|
props[i] = gojq.PathValue{Path: i, Value: makeDecodeValue(f)}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return props
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueKeys() interface{} {
|
2021-12-03 02:06:11 +03:00
|
|
|
vs := make([]interface{}, len(v.Compound.Children))
|
|
|
|
for i := range v.Compound.Children {
|
2020-06-08 03:29:51 +03:00
|
|
|
vs[i] = i
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueHas(key interface{}) interface{} {
|
2021-09-28 00:06:46 +03:00
|
|
|
return valueHas(
|
|
|
|
key,
|
|
|
|
v.decodeValueBase.JQValueKey,
|
|
|
|
func(key interface{}) interface{} {
|
|
|
|
intKey, ok := key.(int)
|
|
|
|
if !ok {
|
|
|
|
return gojqextra.HasKeyTypeError{L: "array", R: fmt.Sprintf("%v", key)}
|
|
|
|
}
|
2021-12-03 02:06:11 +03:00
|
|
|
return intKey >= 0 && intKey < len(v.Compound.Children)
|
2021-09-28 00:06:46 +03:00
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueToGoJQ() interface{} {
|
2021-12-03 02:06:11 +03:00
|
|
|
vs := make([]interface{}, len(v.Compound.Children))
|
|
|
|
for i, f := range v.Compound.Children {
|
2021-08-12 02:35:40 +03:00
|
|
|
vs[i] = makeDecodeValue(f)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode value struct
|
|
|
|
|
2021-09-23 19:35:04 +03:00
|
|
|
var _ DecodeValue = StructDecodeValue{}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-27 12:01:14 +03:00
|
|
|
type StructDecodeValue struct {
|
2021-08-12 00:51:00 +03:00
|
|
|
gojqextra.Base
|
|
|
|
decodeValueBase
|
2021-12-03 02:06:11 +03:00
|
|
|
*decode.Compound
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-03 02:06:11 +03:00
|
|
|
func NewStructDecodeValue(dv *decode.Value, c *decode.Compound) StructDecodeValue {
|
2021-09-27 12:01:14 +03:00
|
|
|
return StructDecodeValue{
|
2021-08-12 00:51:00 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv},
|
|
|
|
Base: gojqextra.Base{Typ: "object"},
|
2021-12-03 02:06:11 +03:00
|
|
|
Compound: c,
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 02:06:11 +03:00
|
|
|
func (v StructDecodeValue) JQValueLength() interface{} { return len(v.Compound.Children) }
|
|
|
|
func (v StructDecodeValue) JQValueSliceLen() interface{} { return len(v.Compound.Children) }
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v StructDecodeValue) JQValueKey(name string) interface{} {
|
2021-08-12 00:51:00 +03:00
|
|
|
if strings.HasPrefix(name, "_") {
|
|
|
|
return v.decodeValueBase.JQValueKey(name)
|
|
|
|
}
|
|
|
|
|
2021-12-03 02:06:11 +03:00
|
|
|
for _, f := range v.Compound.Children {
|
2020-06-08 03:29:51 +03:00
|
|
|
if f.Name == name {
|
2021-08-12 02:35:40 +03:00
|
|
|
return makeDecodeValue(f)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v StructDecodeValue) JQValueEach() interface{} {
|
2021-12-03 02:06:11 +03:00
|
|
|
props := make([]gojq.PathValue, len(v.Compound.Children))
|
|
|
|
for i, f := range v.Compound.Children {
|
2021-08-12 02:35:40 +03:00
|
|
|
props[i] = gojq.PathValue{Path: f.Name, Value: makeDecodeValue(f)}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return props
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v StructDecodeValue) JQValueKeys() interface{} {
|
2021-12-03 02:06:11 +03:00
|
|
|
vs := make([]interface{}, len(v.Compound.Children))
|
|
|
|
for i, f := range v.Compound.Children {
|
2020-06-08 03:29:51 +03:00
|
|
|
vs[i] = f.Name
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v StructDecodeValue) JQValueHas(key interface{}) interface{} {
|
2021-09-28 00:06:46 +03:00
|
|
|
return valueHas(
|
|
|
|
key,
|
|
|
|
v.decodeValueBase.JQValueKey,
|
|
|
|
func(key interface{}) interface{} {
|
|
|
|
stringKey, ok := key.(string)
|
|
|
|
if !ok {
|
|
|
|
return gojqextra.HasKeyTypeError{L: "object", R: fmt.Sprintf("%v", key)}
|
|
|
|
}
|
2021-12-03 02:06:11 +03:00
|
|
|
for _, f := range v.Compound.Children {
|
2021-09-28 00:06:46 +03:00
|
|
|
if f.Name == stringKey {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (v StructDecodeValue) JQValueToGoJQ() interface{} {
|
2021-12-03 02:06:11 +03:00
|
|
|
vm := make(map[string]interface{}, len(v.Compound.Children))
|
|
|
|
for _, f := range v.Compound.Children {
|
2021-08-12 02:35:40 +03:00
|
|
|
vm[f.Name] = makeDecodeValue(f)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return vm
|
|
|
|
}
|