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"
|
|
|
|
"math/big"
|
2021-12-09 19:15:21 +03:00
|
|
|
"reflect"
|
2021-11-29 16:26:11 +03:00
|
|
|
"time"
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-12-09 19:15:21 +03:00
|
|
|
"github.com/mitchellh/copystructure"
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/bitioex"
|
|
|
|
"github.com/wader/fq/internal/gojqex"
|
|
|
|
"github.com/wader/fq/internal/ioex"
|
2022-06-20 21:27:41 +03:00
|
|
|
"github.com/wader/fq/internal/mapstruct"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2023-10-07 18:16:21 +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() {
|
2022-07-16 19:39:57 +03:00
|
|
|
RegisterFunc0("_registry", (*Interp)._registry)
|
|
|
|
RegisterFunc1("_tovalue", (*Interp)._toValue)
|
|
|
|
RegisterFunc2("_decode", (*Interp)._decode)
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
func (i *Interp) _registry(c any) any {
|
2023-03-29 01:36:55 +03:00
|
|
|
uniqueFormats := map[string]*decode.Format{}
|
2021-12-25 18:29:33 +03:00
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
groups := map[string]any{}
|
|
|
|
formats := map[string]any{}
|
2021-12-25 18:29:33 +03:00
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
for _, g := range i.Registry.Groups() {
|
2022-05-20 16:10:41 +03:00
|
|
|
var group []any
|
2021-12-25 18:29:33 +03:00
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
for _, f := range g.Formats {
|
2021-12-25 18:29:33 +03:00
|
|
|
group = append(group, f.Name)
|
|
|
|
if _, ok := uniqueFormats[f.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
uniqueFormats[f.Name] = f
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
groups[g.Name] = group
|
2021-12-25 18:29:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range uniqueFormats {
|
2022-05-20 16:10:41 +03:00
|
|
|
vf := map[string]any{
|
2022-11-20 21:22:16 +03:00
|
|
|
"name": f.Name,
|
|
|
|
"description": f.Description,
|
|
|
|
"probe_order": f.ProbeOrder,
|
|
|
|
"root_name": f.RootName,
|
|
|
|
"root_array": f.RootArray,
|
|
|
|
"skip_decode_function": f.SkipDecodeFunction,
|
2021-12-25 18:29:33 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
var dependenciesVs []any
|
2021-12-25 18:29:33 +03:00
|
|
|
for _, d := range f.Dependencies {
|
2022-05-20 16:10:41 +03:00
|
|
|
var dNamesVs []any
|
2023-03-29 01:36:55 +03:00
|
|
|
for _, g := range d.Groups {
|
|
|
|
dNamesVs = append(dNamesVs, g.Name)
|
2021-12-25 18:29:33 +03:00
|
|
|
}
|
|
|
|
dependenciesVs = append(dependenciesVs, dNamesVs)
|
|
|
|
}
|
|
|
|
if len(dependenciesVs) > 0 {
|
|
|
|
vf["dependencies"] = dependenciesVs
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
var groupsVs []any
|
2023-03-29 01:36:55 +03:00
|
|
|
for _, g := range f.Groups {
|
|
|
|
groupsVs = append(groupsVs, g.Name)
|
2021-12-25 18:29:33 +03:00
|
|
|
}
|
|
|
|
if len(groupsVs) > 0 {
|
|
|
|
vf["groups"] = groupsVs
|
|
|
|
}
|
2023-02-18 23:10:48 +03:00
|
|
|
if f.DefaultInArg != nil {
|
2022-05-20 16:10:41 +03:00
|
|
|
doc := map[string]any{}
|
2023-02-18 23:10:48 +03:00
|
|
|
st := reflect.TypeOf(f.DefaultInArg)
|
2021-12-09 19:15:21 +03:00
|
|
|
for i := 0; i < st.NumField(); i++ {
|
|
|
|
f := st.Field(i)
|
|
|
|
if v, ok := f.Tag.Lookup("doc"); ok {
|
2022-06-20 21:27:41 +03:00
|
|
|
doc[mapstruct.CamelToSnake(f.Name)] = v
|
2021-12-09 19:15:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vf["decode_in_arg_doc"] = doc
|
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
args, err := mapstruct.ToMap(f.DefaultInArg)
|
2021-12-09 19:15:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter out internal field without documentation
|
|
|
|
for k := range args {
|
|
|
|
if _, ok := doc[k]; !ok {
|
|
|
|
delete(args, k)
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 16:27:51 +03:00
|
|
|
vf["decode_in_arg"] = gojqex.Normalize(args)
|
2021-12-09 19:15:21 +03:00
|
|
|
}
|
2021-12-25 18:29:33 +03:00
|
|
|
|
2021-12-09 19:15:21 +03:00
|
|
|
if f.Functions != nil {
|
2022-05-20 16:10:41 +03:00
|
|
|
var ss []any
|
2021-12-09 19:15:21 +03:00
|
|
|
for _, f := range f.Functions {
|
|
|
|
ss = append(ss, f)
|
|
|
|
}
|
|
|
|
vf["functions"] = ss
|
|
|
|
}
|
|
|
|
|
2021-12-25 18:29:33 +03:00
|
|
|
formats[f.Name] = vf
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
var files []any
|
|
|
|
for _, fs := range i.Registry.FSs {
|
|
|
|
ventries := []any{}
|
|
|
|
|
|
|
|
entries, err := fs.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range entries {
|
|
|
|
f, err := fs.Open(e.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-05 01:20:14 +03:00
|
|
|
b, err := io.ReadAll(f)
|
2022-07-16 19:39:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ventries = append(ventries, map[string]any{
|
|
|
|
"name": e.Name(),
|
|
|
|
"data": string(b),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
files = append(files, ventries)
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
return map[string]any{
|
2021-12-25 18:29:33 +03:00
|
|
|
"groups": groups,
|
|
|
|
"formats": formats,
|
2022-07-16 19:39:57 +03:00
|
|
|
"files": files,
|
2021-12-25 18:29:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 13:20:01 +03:00
|
|
|
func (i *Interp) _toValue(c any, om map[string]any) any {
|
2023-05-15 18:23:59 +03:00
|
|
|
opts, err := OptionsFromValue(om)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := toValue(func() (*Options, error) { return opts, nil }, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2022-06-20 21:27:41 +03:00
|
|
|
type decodeOpts struct {
|
|
|
|
Force bool
|
|
|
|
Progress string
|
|
|
|
Remain map[string]any `mapstruct:",remain"`
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
func (i *Interp) _decode(c any, format string, opts decodeOpts) any {
|
2021-12-09 19:15:21 +03:00
|
|
|
var filename string
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
// TODO: progress hack
|
|
|
|
// would be nice to move all progress code into decode but it might be
|
2023-05-14 16:44:39 +03:00
|
|
|
// tricky to keep track of absolute positions in the underlying readers
|
2021-11-16 13:41:14 +03:00
|
|
|
// when it uses BitBuf slices, maybe only in Pos()?
|
|
|
|
if bbf, ok := c.(*openFile); ok {
|
2021-12-09 19:15:21 +03:00
|
|
|
filename = bbf.filename
|
2021-11-16 13:41:14 +03:00
|
|
|
|
|
|
|
if opts.Progress != "" {
|
2022-05-20 16:10:41 +03:00
|
|
|
evalProgress := func(c any) {
|
2021-11-16 13:41:14 +03:00
|
|
|
// {approx_read_bytes: 123, total_size: 123} | opts.Progress
|
|
|
|
_, _ = i.EvalFuncValues(
|
2022-07-16 19:39:57 +03:00
|
|
|
i.EvalInstance.Ctx,
|
2021-11-16 13:41:14 +03:00
|
|
|
c,
|
|
|
|
opts.Progress,
|
|
|
|
nil,
|
2022-08-12 16:27:51 +03:00
|
|
|
EvalOpts{output: ioex.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(
|
2022-05-20 16:10:41 +03:00
|
|
|
map[string]any{
|
2021-11-16 13:41:14 +03:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
formatName, err := toString(format)
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
2021-11-22 15:28:18 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
2023-03-29 01:36:55 +03:00
|
|
|
decodeGroup, err := i.Registry.Group(formatName)
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
2021-11-22 15:28:18 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
dv, formatOut, err := decode.Decode(i.EvalInstance.Ctx, bv.br, decodeGroup,
|
2021-11-16 13:41:14 +03:00
|
|
|
decode.Options{
|
2021-12-09 19:15:21 +03:00
|
|
|
IsRoot: true,
|
|
|
|
FillGaps: true,
|
|
|
|
Force: opts.Force,
|
|
|
|
Range: bv.r,
|
|
|
|
Description: filename,
|
2023-05-03 10:59:09 +03:00
|
|
|
ParseOptsFn: func(init any) any {
|
2023-02-18 23:10:48 +03:00
|
|
|
v, err := copystructure.Copy(init)
|
2021-12-09 19:15:21 +03:00
|
|
|
if err != nil {
|
2023-02-18 23:10:48 +03:00
|
|
|
return nil
|
2021-12-09 19:15:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(opts.Remain) > 0 {
|
2023-02-18 23:10:48 +03:00
|
|
|
if err := mapstruct.ToStruct(opts.Remain, &v); err != nil {
|
2021-12-09 19:15:21 +03:00
|
|
|
// TODO: currently ignores failed struct mappings
|
2023-02-18 23:10:48 +03:00
|
|
|
return nil
|
2021-12-09 19:15:21 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-18 23:10:48 +03:00
|
|
|
// nil if same as init
|
|
|
|
if reflect.DeepEqual(init, v) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-09 19:15:21 +03:00
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
return v
|
2021-12-09 19:15:21 +03:00
|
|
|
},
|
2021-11-16 13:41:14 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if dv == nil {
|
|
|
|
var decodeFormatsErr decode.FormatsError
|
|
|
|
if errors.As(err, &decodeFormatsErr) {
|
2022-05-20 16:10:41 +03:00
|
|
|
var vs []any
|
2021-11-16 13:41:14 +03:00
|
|
|
for _, fe := range decodeFormatsErr.Errs {
|
|
|
|
vs = append(vs, fe.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueError{vs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueError{err}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
var formatOutMap any
|
2021-12-09 19:15:21 +03:00
|
|
|
|
|
|
|
if formatOut != nil {
|
2022-06-20 21:27:41 +03:00
|
|
|
formatOutMap, err = mapstruct.ToMap(formatOut)
|
2021-12-09 19:15:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:58:31 +03:00
|
|
|
return makeDecodeValueOut(dv, decodeValueValue, formatOutMap)
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:07:52 +03:00
|
|
|
func valueOrFallbackKey(name string, baseKey func(name string) any, valueHas func(key any) any, valueKey func(name string) any) any {
|
|
|
|
v := valueHas(name)
|
|
|
|
if b, ok := v.(bool); ok && b {
|
|
|
|
return valueKey(name)
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
2023-09-02 21:07:52 +03:00
|
|
|
return baseKey(name)
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
2023-09-02 21:07:52 +03:00
|
|
|
func valueOrFallbackHas(key any, baseHas func(key any) any, valueHas func(key any) any) any {
|
|
|
|
v := valueHas(key)
|
|
|
|
if b, ok := v.(bool); ok && !b {
|
|
|
|
return baseHas(key)
|
2021-09-28 00:06:46 +03:00
|
|
|
}
|
2023-09-02 21:07:52 +03:00
|
|
|
return v
|
2021-09-28 00:06:46 +03:00
|
|
|
}
|
2021-08-12 00:51:00 +03:00
|
|
|
|
2023-04-28 19:44:24 +03:00
|
|
|
// TODO: make more efficient somehow? shallow values but might be hard
|
|
|
|
// when things like tovalue.key should behave like a jq value and not a decode value etc
|
2023-05-15 18:23:59 +03:00
|
|
|
func toValue(optsFn func() (*Options, error), v any) (any, error) {
|
|
|
|
return gojqex.ToGoJQValueFn(v, func(v any) (any, error) {
|
2023-04-28 19:44:24 +03:00
|
|
|
switch v := v.(type) {
|
|
|
|
case JQValueEx:
|
|
|
|
if optsFn == nil {
|
2023-05-15 18:23:59 +03:00
|
|
|
return v.JQValueToGoJQ(), nil
|
2023-04-28 19:44:24 +03:00
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
return v.JQValueToGoJQEx(optsFn), nil
|
2023-04-28 19:44:24 +03:00
|
|
|
case gojq.JQValue:
|
2023-05-15 18:23:59 +03:00
|
|
|
return v.JQValueToGoJQ(), nil
|
2023-04-28 19:44:24 +03:00
|
|
|
default:
|
2023-05-15 18:23:59 +03:00
|
|
|
return v, nil
|
2022-06-07 16:33:55 +03:00
|
|
|
}
|
2023-04-28 19:44:24 +03:00
|
|
|
})
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:58:31 +03:00
|
|
|
type decodeValueKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
decodeValueValue decodeValueKind = iota
|
|
|
|
decodeValueActual
|
|
|
|
decodeValueSym
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeDecodeValue(dv *decode.Value, kind decodeValueKind) any {
|
|
|
|
return makeDecodeValueOut(dv, kind, nil)
|
2021-12-09 19:15:21 +03:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:58:31 +03:00
|
|
|
func makeDecodeValueOut(dv *decode.Value, kind decodeValueKind, out any) any {
|
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 {
|
2021-12-09 19:15:21 +03:00
|
|
|
return NewArrayDecodeValue(dv, out, vv)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-12-09 19:15:21 +03:00
|
|
|
return NewStructDecodeValue(dv, out, vv)
|
2022-09-30 14:58:23 +03:00
|
|
|
|
2023-10-07 18:16:21 +03:00
|
|
|
case scalar.Scalarable:
|
2022-09-25 17:58:31 +03:00
|
|
|
// TODO: rethink value/actual/sym handling
|
|
|
|
var vvv any
|
|
|
|
switch kind {
|
|
|
|
case decodeValueValue:
|
2022-09-30 14:58:23 +03:00
|
|
|
vvv = vv.ScalarValue()
|
2022-09-25 17:58:31 +03:00
|
|
|
case decodeValueActual:
|
2022-09-30 14:58:23 +03:00
|
|
|
vvv = vv.ScalarActual()
|
2022-09-25 17:58:31 +03:00
|
|
|
case decodeValueSym:
|
2022-09-30 14:58:23 +03:00
|
|
|
vvv = vv.ScalarSym()
|
2022-09-25 17:58:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch vvv := vvv.(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{
|
2022-08-12 16:27:51 +03:00
|
|
|
JQValue: &gojqex.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-09-25 17:58:31 +03:00
|
|
|
vvvC, err := bitio.CloneReader(vvv)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-25 17:58:31 +03:00
|
|
|
if _, err := bitioex.CopyBits(buf, vvvC); err != nil {
|
2021-11-21 23:27:44 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-12 16:27:51 +03:00
|
|
|
return gojqex.String([]rune(buf.String())), nil
|
2021-11-21 23:27:44 +03:00
|
|
|
},
|
|
|
|
},
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2023-05-17 12:23:32 +03:00
|
|
|
isRaw: true,
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
case bool:
|
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Boolean(vvv),
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
case int:
|
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Number{V: vvv},
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
case int64:
|
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Number{V: big.NewInt(vvv)},
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
case uint64:
|
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Number{V: new(big.Int).SetUint64(vvv)},
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Number{V: vvv},
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
case string:
|
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.String(vvv),
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
case []any:
|
2021-11-03 19:19:33 +03:00
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Array(vvv),
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
case map[string]any:
|
2021-11-03 19:19:33 +03:00
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Object(vvv),
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
return decodeValue{
|
2022-08-12 16:27:51 +03:00
|
|
|
JQValue: gojqex.Null{},
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
2022-01-14 03:58:42 +03:00
|
|
|
case *big.Int:
|
|
|
|
return decodeValue{
|
2022-09-25 17:58:31 +03:00
|
|
|
JQValue: gojqex.Number{V: vvv},
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv},
|
2022-01-14 03:58:42 +03:00
|
|
|
}
|
2022-11-20 21:22:16 +03:00
|
|
|
case Binary:
|
2023-05-11 18:48:02 +03:00
|
|
|
return vvv
|
2023-01-04 15:08:01 +03:00
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
default:
|
2022-09-25 17:58:31 +03:00
|
|
|
panic(fmt.Sprintf("unreachable vv %#+v", vvv))
|
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 {
|
2021-12-09 19:15:21 +03:00
|
|
|
dv *decode.Value
|
2022-05-20 16:10:41 +03:00
|
|
|
out any
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 19:35:04 +03:00
|
|
|
func (dvb decodeValueBase) DecodeValue() *decode.Value {
|
|
|
|
return dvb.dv
|
|
|
|
}
|
|
|
|
|
2023-05-15 18:23:59 +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) {
|
2023-10-07 18:16:21 +03:00
|
|
|
if s, ok := dvb.dv.V.(scalar.Scalarable); ok && s.ScalarFlags().IsSynthetic() {
|
|
|
|
return Binary{}, fmt.Errorf("synthetic value can't be a binary")
|
|
|
|
}
|
2022-02-08 20:44:48 +03:00
|
|
|
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 {
|
2023-09-02 21:07:52 +03:00
|
|
|
return []string{
|
2021-11-03 19:19:33 +03:00
|
|
|
"_actual",
|
2020-06-08 03:29:51 +03:00
|
|
|
"_bits",
|
2023-01-03 13:11:12 +03:00
|
|
|
"_buffer_root",
|
2020-06-08 03:29:51 +03:00
|
|
|
"_bytes",
|
2023-01-03 13:11:12 +03:00
|
|
|
"_description",
|
2023-09-02 21:07:52 +03:00
|
|
|
"_error",
|
2023-01-03 13:11:12 +03:00
|
|
|
"_format_root",
|
2023-09-02 21:07:52 +03:00
|
|
|
"_format",
|
2022-12-01 22:37:40 +03:00
|
|
|
"_gap",
|
2023-09-02 21:07:52 +03:00
|
|
|
"_index",
|
2023-01-03 13:11:12 +03:00
|
|
|
"_len",
|
|
|
|
"_name",
|
2023-09-02 21:07:52 +03:00
|
|
|
"_out",
|
2023-01-03 13:11:12 +03:00
|
|
|
"_parent",
|
|
|
|
"_path",
|
|
|
|
"_root",
|
|
|
|
"_start",
|
|
|
|
"_stop",
|
|
|
|
"_sym",
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2023-09-02 21:07:52 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2023-09-02 21:07:52 +03:00
|
|
|
func (dvb decodeValueBase) JQValueHas(key any) any {
|
|
|
|
name, ok := key.(string)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-12-10 20:18:48 +03:00
|
|
|
|
2023-09-02 21:07:52 +03:00
|
|
|
switch name {
|
|
|
|
case "_actual",
|
|
|
|
"_bits",
|
|
|
|
"_buffer_root",
|
|
|
|
"_bytes",
|
|
|
|
"_description",
|
|
|
|
"_error",
|
|
|
|
"_format_root",
|
|
|
|
"_format",
|
|
|
|
"_gap",
|
|
|
|
"_index",
|
|
|
|
"_len",
|
|
|
|
"_name",
|
|
|
|
"_out",
|
|
|
|
"_parent",
|
|
|
|
"_path",
|
|
|
|
"_root",
|
|
|
|
"_start",
|
|
|
|
"_stop",
|
|
|
|
"_sym":
|
|
|
|
return true
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:07:52 +03:00
|
|
|
return false
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (dvb decodeValueBase) JQValueKey(name string) any {
|
2021-08-12 00:51:00 +03:00
|
|
|
dv := dvb.dv
|
|
|
|
|
|
|
|
switch name {
|
2021-11-03 19:19:33 +03:00
|
|
|
case "_actual":
|
2022-09-25 17:58:31 +03:00
|
|
|
switch dv.V.(type) {
|
2023-10-07 18:16:21 +03:00
|
|
|
case scalar.Scalarable:
|
2022-09-25 17:58:31 +03:00
|
|
|
return makeDecodeValue(dv, decodeValueActual)
|
2021-11-03 19:19:33 +03:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-03 13:11:12 +03:00
|
|
|
case "_bits":
|
2023-10-07 18:16:21 +03:00
|
|
|
if s, ok := dv.V.(scalar.Scalarable); ok && s.ScalarFlags().IsSynthetic() {
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-03 13:11:12 +03:00
|
|
|
return Binary{
|
|
|
|
br: dv.RootReader,
|
|
|
|
r: dv.Range,
|
|
|
|
unit: 1,
|
|
|
|
}
|
|
|
|
case "_buffer_root":
|
|
|
|
// TODO: rename?
|
|
|
|
return makeDecodeValue(dv.BufferRoot(), decodeValueValue)
|
|
|
|
case "_bytes":
|
2023-10-07 18:16:21 +03:00
|
|
|
if s, ok := dv.V.(scalar.Scalarable); ok && s.ScalarFlags().IsSynthetic() {
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-03 13:11:12 +03:00
|
|
|
return Binary{
|
|
|
|
br: dv.RootReader,
|
|
|
|
r: dv.Range,
|
|
|
|
unit: 8,
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
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
|
2023-10-07 18:16:21 +03:00
|
|
|
case scalar.Scalarable:
|
2022-09-30 14:58:23 +03:00
|
|
|
desc := vv.ScalarDescription()
|
|
|
|
if desc == "" {
|
2021-11-19 13:44:23 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-30 14:58:23 +03:00
|
|
|
return desc
|
2021-11-03 19:19:33 +03:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-03 13:11:12 +03:00
|
|
|
case "_format_root":
|
|
|
|
// TODO: rename?
|
|
|
|
return makeDecodeValue(dv.FormatRoot(), decodeValueValue)
|
|
|
|
case "_gap":
|
|
|
|
switch vv := dv.V.(type) {
|
2023-10-07 18:16:21 +03:00
|
|
|
case scalar.Scalarable:
|
|
|
|
return vv.ScalarFlags().IsGap()
|
2023-01-03 13:11:12 +03:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case "_len":
|
|
|
|
return big.NewInt(dv.Range.Len)
|
|
|
|
case "_name":
|
|
|
|
return dv.Name
|
|
|
|
case "_parent":
|
|
|
|
if dv.Parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return makeDecodeValue(dv.Parent, decodeValueValue)
|
2021-08-12 00:51:00 +03:00
|
|
|
case "_path":
|
|
|
|
return valuePath(dv)
|
2023-01-03 13:11:12 +03:00
|
|
|
case "_root":
|
|
|
|
return makeDecodeValue(dv.Root(), decodeValueValue)
|
|
|
|
case "_start":
|
|
|
|
return big.NewInt(dv.Range.Start)
|
|
|
|
case "_stop":
|
|
|
|
return big.NewInt(dv.Range.Stop())
|
|
|
|
case "_sym":
|
|
|
|
switch dv.V.(type) {
|
2023-10-07 18:16:21 +03:00
|
|
|
case scalar.Scalarable:
|
2023-01-03 13:11:12 +03:00
|
|
|
return makeDecodeValue(dv, decodeValueSym)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:51:00 +03:00
|
|
|
case "_error":
|
2022-06-01 17:55:55 +03:00
|
|
|
var formatErr decode.FormatError
|
|
|
|
if errors.As(dv.Err, &formatErr) {
|
|
|
|
return formatErr.Value()
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-06-01 17:55:55 +03:00
|
|
|
return nil
|
2021-08-12 00:51:00 +03:00
|
|
|
case "_format":
|
2022-06-01 17:55:55 +03:00
|
|
|
if dv.Format != nil {
|
|
|
|
return dv.Format.Name
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
2022-06-01 17:55:55 +03:00
|
|
|
return nil
|
2021-12-09 19:15:21 +03:00
|
|
|
case "_out":
|
|
|
|
return dvb.out
|
2023-01-03 13:11:12 +03:00
|
|
|
|
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
|
|
|
|
2023-09-02 21:07:52 +03:00
|
|
|
return nil
|
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
|
2023-05-17 12:23:32 +03:00
|
|
|
isRaw bool
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v decodeValue) JQValueKey(name string) any {
|
2023-09-02 21:07:52 +03:00
|
|
|
return valueOrFallbackKey(name, v.decodeValueBase.JQValueKey, v.JQValue.JQValueHas, v.JQValue.JQValueKey)
|
2021-09-28 00:06:46 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v decodeValue) JQValueHas(key any) any {
|
2023-09-02 21:07:52 +03:00
|
|
|
return valueOrFallbackHas(key, v.decodeValueBase.JQValueHas, v.JQValue.JQValueHas)
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
func (v decodeValue) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
|
2023-05-17 12:23:32 +03:00
|
|
|
if !v.isRaw {
|
2021-10-20 02:28:26 +03:00
|
|
|
return v.JQValueToGoJQ()
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
2023-10-29 18:06:49 +03:00
|
|
|
if s, ok := v.dv.V.(scalar.Scalarable); ok && !s.ScalarFlags().IsSynthetic() {
|
|
|
|
bv, err := v.ToBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bv.JQValueToGoJQEx(optsFn)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
|
2023-10-29 18:06:49 +03:00
|
|
|
return v.JQValueToGoJQ()
|
2021-10-20 02:28:26 +03:00
|
|
|
|
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 {
|
2022-08-12 16:27:51 +03:00
|
|
|
gojqex.Base
|
2021-08-12 00:51:00 +03:00
|
|
|
decodeValueBase
|
2021-12-03 02:06:11 +03:00
|
|
|
*decode.Compound
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func NewArrayDecodeValue(dv *decode.Value, out any, c *decode.Compound) ArrayDecodeValue {
|
2021-09-27 12:01:14 +03:00
|
|
|
return ArrayDecodeValue{
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv, out: out},
|
2022-08-12 16:27:51 +03:00
|
|
|
Base: gojqex.Base{Typ: gojq.JQTypeArray},
|
2021-12-03 02:06:11 +03:00
|
|
|
Compound: c,
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueKey(name string) any {
|
2023-09-02 21:07:52 +03:00
|
|
|
return valueOrFallbackKey(name, v.decodeValueBase.JQValueKey, v.Base.JQValueHas, v.Base.JQValueKey)
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueSliceLen() any { return len(v.Compound.Children) }
|
|
|
|
func (v ArrayDecodeValue) JQValueLength() any { return len(v.Compound.Children) }
|
|
|
|
func (v ArrayDecodeValue) JQValueIndex(index int) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
// -1 outside after string, -2 outside before string
|
|
|
|
if index < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-25 17:58:31 +03:00
|
|
|
return makeDecodeValue((v.Compound.Children)[index], decodeValueValue)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueSlice(start int, end int) any {
|
|
|
|
vs := make([]any, end-start)
|
2021-12-03 02:06:11 +03:00
|
|
|
for i, e := range (v.Compound.Children)[start:end] {
|
2022-09-25 17:58:31 +03:00
|
|
|
vs[i] = makeDecodeValue(e, decodeValueValue)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueEach() any {
|
2021-12-03 02:06:11 +03:00
|
|
|
props := make([]gojq.PathValue, len(v.Compound.Children))
|
|
|
|
for i, f := range v.Compound.Children {
|
2022-09-25 17:58:31 +03:00
|
|
|
props[i] = gojq.PathValue{Path: i, Value: makeDecodeValue(f, decodeValueValue)}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return props
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueKeys() any {
|
|
|
|
vs := make([]any, len(v.Compound.Children))
|
2021-12-03 02:06:11 +03:00
|
|
|
for i := range v.Compound.Children {
|
2020-06-08 03:29:51 +03:00
|
|
|
vs[i] = i
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueHas(key any) any {
|
2023-09-02 21:07:52 +03:00
|
|
|
return valueOrFallbackHas(
|
2021-09-28 00:06:46 +03:00
|
|
|
key,
|
2023-09-02 21:07:52 +03:00
|
|
|
v.decodeValueBase.JQValueHas,
|
2022-05-20 16:10:41 +03:00
|
|
|
func(key any) any {
|
2021-09-28 00:06:46 +03:00
|
|
|
intKey, ok := key.(int)
|
|
|
|
if !ok {
|
2022-08-12 16:27:51 +03:00
|
|
|
return gojqex.HasKeyTypeError{L: gojq.JQTypeArray, R: fmt.Sprintf("%v", key)}
|
2021-09-28 00:06:46 +03:00
|
|
|
}
|
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
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
|
|
|
|
opts, err := optsFn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-02 13:20:01 +03:00
|
|
|
|
|
|
|
vs := make([]any, 0, len(v.Compound.Children))
|
|
|
|
for _, f := range v.Compound.Children {
|
|
|
|
switch s := f.V.(type) {
|
2023-10-07 18:16:21 +03:00
|
|
|
case scalar.Scalarable:
|
|
|
|
if s.ScalarFlags().IsGap() && opts.SkipGaps {
|
2023-04-02 13:20:01 +03:00
|
|
|
// skip, note for arrays this will affect indexes
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vs = append(vs, makeDecodeValue(f, decodeValueValue))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2023-04-02 13:20:01 +03:00
|
|
|
func (v ArrayDecodeValue) JQValueToGoJQ() any {
|
2023-05-15 18:23:59 +03:00
|
|
|
return v.JQValueToGoJQEx(func() (*Options, error) { return &Options{}, nil })
|
2023-04-02 13:20:01 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
// 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 {
|
2022-08-12 16:27:51 +03:00
|
|
|
gojqex.Base
|
2021-08-12 00:51:00 +03:00
|
|
|
decodeValueBase
|
2021-12-03 02:06:11 +03:00
|
|
|
*decode.Compound
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func NewStructDecodeValue(dv *decode.Value, out any, c *decode.Compound) StructDecodeValue {
|
2021-09-27 12:01:14 +03:00
|
|
|
return StructDecodeValue{
|
2021-12-09 19:15:21 +03:00
|
|
|
decodeValueBase: decodeValueBase{dv: dv, out: out},
|
2022-08-12 16:27:51 +03:00
|
|
|
Base: gojqex.Base{Typ: gojq.JQTypeObject},
|
2021-12-03 02:06:11 +03:00
|
|
|
Compound: c,
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v StructDecodeValue) JQValueLength() any { return len(v.Compound.Children) }
|
|
|
|
func (v StructDecodeValue) JQValueSliceLen() any { return len(v.Compound.Children) }
|
|
|
|
func (v StructDecodeValue) JQValueKey(name string) any {
|
2023-09-02 21:07:52 +03:00
|
|
|
return valueOrFallbackKey(
|
|
|
|
name,
|
|
|
|
v.decodeValueBase.JQValueKey,
|
|
|
|
func(key any) any {
|
|
|
|
stringKey, ok := key.(string)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v.Compound.ByName != nil {
|
|
|
|
if _, ok := v.Compound.ByName[stringKey]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
func(name string) any {
|
|
|
|
if v.Compound.ByName != nil {
|
|
|
|
if f, ok := v.Compound.ByName[name]; ok {
|
|
|
|
return makeDecodeValue(f, decodeValueValue)
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 12:31:46 +03:00
|
|
|
|
2023-09-02 21:07:52 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v StructDecodeValue) JQValueEach() any {
|
2021-12-03 02:06:11 +03:00
|
|
|
props := make([]gojq.PathValue, len(v.Compound.Children))
|
|
|
|
for i, f := range v.Compound.Children {
|
2022-09-25 17:58:31 +03:00
|
|
|
props[i] = gojq.PathValue{Path: f.Name, Value: makeDecodeValue(f, decodeValueValue)}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return props
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v StructDecodeValue) JQValueKeys() any {
|
|
|
|
vs := make([]any, len(v.Compound.Children))
|
2021-12-03 02:06:11 +03:00
|
|
|
for i, f := range v.Compound.Children {
|
2020-06-08 03:29:51 +03:00
|
|
|
vs[i] = f.Name
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (v StructDecodeValue) JQValueHas(key any) any {
|
2023-09-02 21:07:52 +03:00
|
|
|
return valueOrFallbackHas(
|
2021-09-28 00:06:46 +03:00
|
|
|
key,
|
2023-09-02 21:07:52 +03:00
|
|
|
v.decodeValueBase.JQValueHas,
|
2022-05-20 16:10:41 +03:00
|
|
|
func(key any) any {
|
2021-09-28 00:06:46 +03:00
|
|
|
stringKey, ok := key.(string)
|
|
|
|
if !ok {
|
2022-08-12 16:27:51 +03:00
|
|
|
return gojqex.HasKeyTypeError{L: gojq.JQTypeObject, R: fmt.Sprintf("%v", key)}
|
2021-09-28 00:06:46 +03:00
|
|
|
}
|
2023-09-02 21:07:52 +03:00
|
|
|
|
|
|
|
if v.Compound.ByName != nil {
|
|
|
|
if _, ok := v.Compound.ByName[stringKey]; ok {
|
2021-09-28 00:06:46 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 21:07:52 +03:00
|
|
|
|
2021-09-28 00:06:46 +03:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2023-05-15 18:23:59 +03:00
|
|
|
func (v StructDecodeValue) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
|
|
|
|
opts, err := optsFn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-02 13:20:01 +03:00
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
vm := make(map[string]any, len(v.Compound.Children))
|
2021-12-03 02:06:11 +03:00
|
|
|
for _, f := range v.Compound.Children {
|
2023-04-02 13:20:01 +03:00
|
|
|
switch s := f.V.(type) {
|
2023-10-07 18:16:21 +03:00
|
|
|
case scalar.Scalarable:
|
|
|
|
if s.ScalarFlags().IsGap() && opts.SkipGaps {
|
2023-04-02 13:20:01 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:58:31 +03:00
|
|
|
vm[f.Name] = makeDecodeValue(f, decodeValueValue)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return vm
|
|
|
|
}
|
2023-04-02 13:20:01 +03:00
|
|
|
func (v StructDecodeValue) JQValueToGoJQ() any {
|
2023-05-15 18:23:59 +03:00
|
|
|
return v.JQValueToGoJQEx(func() (*Options, error) { return &Options{}, nil })
|
2023-04-02 13:20:01 +03:00
|
|
|
}
|