2020-06-08 03:29:51 +03:00
|
|
|
package decode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-02-10 22:06:38 +03:00
|
|
|
"fmt"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/ranges"
|
2022-02-01 17:26:45 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2022-08-31 11:34:32 +03:00
|
|
|
"golang.org/x/exp/slices"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
type Compound struct {
|
2022-05-03 16:42:40 +03:00
|
|
|
IsArray bool
|
|
|
|
Children []*Value
|
2022-09-01 12:31:46 +03:00
|
|
|
ByName map[string]*Value
|
2021-11-03 19:19:33 +03:00
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:55:55 +03:00
|
|
|
// TODO: Encoding, u16le, varint etc, encode?
|
|
|
|
// TODO: Value/Compound interface? can have per type and save memory
|
|
|
|
// TODO: Make some fields optional somehow? map/slice?
|
2020-06-08 03:29:51 +03:00
|
|
|
type Value struct {
|
2022-06-01 17:55:55 +03:00
|
|
|
Parent *Value
|
|
|
|
Name string
|
|
|
|
V any // scalar.S or Compound (array/struct)
|
|
|
|
Index int // index in parent array/struct
|
|
|
|
Range ranges.Range
|
|
|
|
RootReader bitio.ReaderAtSeeker
|
|
|
|
IsRoot bool // TODO: rework?
|
|
|
|
Format *Format // TODO: rework
|
|
|
|
Description string
|
|
|
|
Err error
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type WalkFn func(v *Value, rootV *Value, depth int, rootDepth int) error
|
|
|
|
|
|
|
|
var ErrWalkSkipChildren = errors.New("skip children")
|
2021-08-30 13:51:42 +03:00
|
|
|
var ErrWalkBreak = errors.New("break")
|
2020-06-08 03:29:51 +03:00
|
|
|
var ErrWalkStop = errors.New("stop")
|
|
|
|
|
2021-10-18 16:06:08 +03:00
|
|
|
type WalkOpts struct {
|
|
|
|
PreOrder bool
|
|
|
|
OneRoot bool
|
|
|
|
Fn WalkFn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Value) Walk(opts WalkOpts) error {
|
2020-06-08 03:29:51 +03:00
|
|
|
var walkFn WalkFn
|
2021-10-18 16:06:08 +03:00
|
|
|
|
|
|
|
walkFn = func(wv *Value, rootV *Value, depth int, rootDepth int) error {
|
|
|
|
if opts.OneRoot && wv != v && wv.IsRoot {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
rootDepthDelta := 0
|
2021-11-30 14:51:52 +03:00
|
|
|
// only count switching to a new root
|
|
|
|
if wv.IsRoot && wv != rootV {
|
2021-10-18 16:06:08 +03:00
|
|
|
rootV = wv
|
2020-06-08 03:29:51 +03:00
|
|
|
rootDepthDelta = 1
|
|
|
|
}
|
|
|
|
|
2021-10-18 16:06:08 +03:00
|
|
|
if opts.PreOrder {
|
|
|
|
err := opts.Fn(wv, rootV, depth, rootDepth+rootDepthDelta)
|
2020-06-08 03:29:51 +03:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, ErrWalkSkipChildren):
|
|
|
|
return nil
|
|
|
|
case errors.Is(err, ErrWalkStop):
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
switch wvv := wv.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *Compound:
|
|
|
|
for _, wv := range wvv.Children {
|
2020-06-08 03:29:51 +03:00
|
|
|
if err := walkFn(wv, rootV, depth+1, rootDepth+rootDepthDelta); err != nil {
|
2021-08-30 13:51:42 +03:00
|
|
|
if errors.Is(err, ErrWalkBreak) {
|
|
|
|
break
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
|
2021-10-18 16:06:08 +03:00
|
|
|
if !opts.PreOrder {
|
|
|
|
err := opts.Fn(wv, rootV, depth, rootDepth+rootDepthDelta)
|
2020-06-08 03:29:51 +03:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, ErrWalkSkipChildren):
|
|
|
|
return errors.New("can't skip children in post-order")
|
|
|
|
case errors.Is(err, ErrWalkStop):
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// figure out root value for v as it might not be a root itself
|
2021-09-28 00:11:18 +03:00
|
|
|
rootV := v.BufferRoot()
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
err := walkFn(v, rootV, 0, 0)
|
|
|
|
if errors.Is(err, ErrWalkStop) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 16:06:08 +03:00
|
|
|
func (v *Value) WalkPreOrder(fn WalkFn) error {
|
|
|
|
return v.Walk(WalkOpts{
|
|
|
|
PreOrder: true,
|
|
|
|
Fn: fn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Value) WalkPostOrder(fn WalkFn) error {
|
|
|
|
return v.Walk(WalkOpts{
|
|
|
|
PreOrder: false,
|
|
|
|
Fn: fn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Value) WalkRootPreOrder(fn WalkFn) error {
|
|
|
|
return v.Walk(WalkOpts{
|
|
|
|
PreOrder: true,
|
|
|
|
OneRoot: true,
|
|
|
|
Fn: fn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Value) WalkRootPostOrder(fn WalkFn) error {
|
|
|
|
return v.Walk(WalkOpts{
|
|
|
|
PreOrder: false,
|
|
|
|
OneRoot: true,
|
|
|
|
Fn: fn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-28 00:11:18 +03:00
|
|
|
func (v *Value) root(findSubRoot bool, findFormatRoot bool) *Value {
|
|
|
|
rootV := v
|
|
|
|
for rootV.Parent != nil {
|
|
|
|
if findSubRoot && rootV.IsRoot {
|
|
|
|
break
|
|
|
|
}
|
2022-06-01 17:55:55 +03:00
|
|
|
if findFormatRoot && rootV.Format != nil {
|
|
|
|
break
|
2021-09-28 00:11:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rootV = rootV.Parent
|
|
|
|
}
|
|
|
|
return rootV
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Value) Root() *Value { return v.root(false, false) }
|
|
|
|
func (v *Value) BufferRoot() *Value { return v.root(true, false) }
|
|
|
|
func (v *Value) FormatRoot() *Value { return v.root(true, true) }
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func (v *Value) Errors() []error {
|
|
|
|
var errs []error
|
2022-06-01 17:55:55 +03:00
|
|
|
_ = v.WalkPreOrder(func(v *Value, _ *Value, _ int, _ int) error {
|
|
|
|
if v.Err != nil {
|
|
|
|
errs = append(errs, v.Err)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:14:49 +03:00
|
|
|
func (v *Value) InnerRange() ranges.Range {
|
|
|
|
if v.IsRoot {
|
|
|
|
return ranges.Range{Start: 0, Len: v.Range.Len}
|
|
|
|
}
|
|
|
|
return v.Range
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func (v *Value) postProcess() {
|
2022-07-19 19:41:26 +03:00
|
|
|
if err := v.WalkRootPostOrder(func(v *Value, _ *Value, _ int, _ int) error {
|
2020-06-08 03:29:51 +03:00
|
|
|
switch vv := v.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *Compound:
|
2020-06-08 03:29:51 +03:00
|
|
|
first := true
|
2021-12-03 02:06:11 +03:00
|
|
|
for _, f := range vv.Children {
|
2020-06-08 03:29:51 +03:00
|
|
|
if f.IsRoot {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if first {
|
|
|
|
v.Range = f.Range
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
v.Range = ranges.MinMax(v.Range, f.Range)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 18:45:28 +03:00
|
|
|
// sort struct fields and make sure to keep order if range is the same
|
|
|
|
if !vv.IsArray {
|
|
|
|
slices.SortStableFunc(vv.Children, func(a, b *Value) bool { return a.Range.Start < b.Range.Start })
|
2022-05-03 16:42:40 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-12-10 20:18:48 +03:00
|
|
|
v.Index = -1
|
|
|
|
if vv.IsArray {
|
|
|
|
for i, f := range vv.Children {
|
|
|
|
f.Index = i
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, f := range vv.Children {
|
|
|
|
f.Index = -1
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 17:26:45 +03:00
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
// TODO: rethink this
|
|
|
|
func (v *Value) TryUintScalarFn(sms ...scalar.UintMapper) error {
|
2022-02-01 17:26:45 +03:00
|
|
|
var err error
|
2022-09-30 14:58:23 +03:00
|
|
|
sr, ok := v.V.(*scalar.Uint)
|
2022-02-01 17:26:45 +03:00
|
|
|
if !ok {
|
|
|
|
panic("not a scalar value")
|
|
|
|
}
|
|
|
|
s := *sr
|
|
|
|
for _, sm := range sms {
|
2022-09-30 14:58:23 +03:00
|
|
|
s, err = sm.MapUint(s)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.V = &s
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Value) TryBitBufScalarFn(sms ...scalar.BitBufMapper) error {
|
|
|
|
var err error
|
|
|
|
sr, ok := v.V.(*scalar.BitBuf)
|
|
|
|
if !ok {
|
|
|
|
panic("not a scalar value")
|
|
|
|
}
|
|
|
|
s := *sr
|
|
|
|
for _, sm := range sms {
|
|
|
|
s, err = sm.MapBitBuf(s)
|
2022-02-01 17:26:45 +03:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.V = &s
|
|
|
|
return err
|
|
|
|
}
|
2023-02-10 22:06:38 +03:00
|
|
|
|
|
|
|
func (v *Value) Remove() error {
|
|
|
|
p := v.Parent
|
|
|
|
if p == nil {
|
|
|
|
return fmt.Errorf("d has no parent")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch fv := p.V.(type) {
|
|
|
|
case *Compound:
|
|
|
|
if !fv.IsArray {
|
|
|
|
if _, ok := fv.ByName[v.Name]; !ok {
|
|
|
|
return fmt.Errorf("d not in parent ByName")
|
|
|
|
}
|
|
|
|
delete(fv.ByName, p.Name)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
var cs []*Value
|
|
|
|
for _, c := range fv.Children {
|
|
|
|
if c == v {
|
|
|
|
found = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cs = append(cs, c)
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("d not in parent children")
|
|
|
|
}
|
|
|
|
fv.Children = cs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|