File: value.go

package info (click to toggle)
fq 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 106,624 kB
  • sloc: xml: 2,835; makefile: 250; sh: 241; exp: 57; ansic: 21
file content (293 lines) | stat: -rw-r--r-- 5,852 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package decode

import (
	"errors"
	"fmt"

	"github.com/wader/fq/internal/cmpex"
	"github.com/wader/fq/pkg/bitio"
	"github.com/wader/fq/pkg/ranges"
	"github.com/wader/fq/pkg/scalar"
	"golang.org/x/exp/slices"
)

type Compound struct {
	IsArray     bool
	Children    []*Value
	ByName      map[string]*Value
	Description string
}

// 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?
type Value struct {
	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
}

type WalkFn func(v *Value, rootV *Value, depth int, rootDepth int) error

var ErrWalkSkipChildren = errors.New("skip children")
var ErrWalkBreak = errors.New("break")
var ErrWalkStop = errors.New("stop")

type WalkOpts struct {
	PreOrder bool
	OneRoot  bool
	Fn       WalkFn
}

func (v *Value) Walk(opts WalkOpts) error {
	var walkFn WalkFn

	walkFn = func(wv *Value, rootV *Value, depth int, rootDepth int) error {
		if opts.OneRoot && wv != v && wv.IsRoot {
			return nil
		}

		rootDepthDelta := 0
		// only count switching to a new root
		if wv.IsRoot && wv != rootV {
			rootV = wv
			rootDepthDelta = 1
		}

		if opts.PreOrder {
			err := opts.Fn(wv, rootV, depth, rootDepth+rootDepthDelta)
			switch {
			case errors.Is(err, ErrWalkSkipChildren):
				return nil
			case errors.Is(err, ErrWalkStop):
				fallthrough
			default:
				if err != nil {
					return err
				}
			}
		}

		switch wvv := wv.V.(type) {
		case *Compound:
			for _, wv := range wvv.Children {
				if err := walkFn(wv, rootV, depth+1, rootDepth+rootDepthDelta); err != nil {
					if errors.Is(err, ErrWalkBreak) {
						break
					}
					return err
				}
			}
		}

		if !opts.PreOrder {
			err := opts.Fn(wv, rootV, depth, rootDepth+rootDepthDelta)
			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
	rootV := v.BufferRoot()

	err := walkFn(v, rootV, 0, 0)
	if errors.Is(err, ErrWalkStop) {
		err = nil
	}

	return err
}

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,
	})
}

func (v *Value) root(findSubRoot bool, findFormatRoot bool) *Value {
	rootV := v
	for rootV.Parent != nil {
		if findSubRoot && rootV.IsRoot {
			break
		}
		if findFormatRoot && rootV.Format != nil {
			break
		}

		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) }

func (v *Value) Errors() []error {
	var errs []error
	_ = v.WalkPreOrder(func(v *Value, _ *Value, _ int, _ int) error {
		if v.Err != nil {
			errs = append(errs, v.Err)
		}
		return nil
	})
	return errs
}

func (v *Value) InnerRange() ranges.Range {
	if v.IsRoot {
		return ranges.Range{Start: 0, Len: v.Range.Len}
	}
	return v.Range
}

func (v *Value) postProcess() {
	if err := v.WalkRootPostOrder(func(v *Value, _ *Value, _ int, _ int) error {
		switch vv := v.V.(type) {
		case *Compound:
			first := true
			for _, f := range vv.Children {
				if f.IsRoot {
					continue
				}
				if s, ok := f.V.(scalar.Scalarable); ok && s.ScalarFlags().IsSynthetic() {
					continue
				}

				if first {
					v.Range = f.Range
					first = false
				} else {
					v.Range = ranges.MinMax(v.Range, f.Range)
				}
			}

			// 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) int {
					return cmpex.Compare(a.Range.Start, b.Range.Start)
				})
			}

			v.Index = -1
			if vv.IsArray {
				for i, f := range vv.Children {
					f.Index = i
				}
			} else {
				for _, f := range vv.Children {
					f.Index = -1
				}
			}
		}
		return nil
	}); err != nil {
		panic(err)
	}
}

// TODO: rethink this
func (v *Value) TryUintScalarFn(sms ...scalar.UintMapper) error {
	var err error
	sr, ok := v.V.(*scalar.Uint)
	if !ok {
		panic("not a scalar value")
	}
	s := *sr
	for _, sm := range sms {
		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)
		if err != nil {
			break
		}
	}
	v.V = &s
	return err
}

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
}