1
1
mirror of https://github.com/wader/fq.git synced 2024-10-04 15:38:17 +03:00

Merge pull request #413 from wader/decode-remove-rangesort

decode: Remove RangeSorted flag as we can decide on array/struct instead
This commit is contained in:
Mattias Wadman 2022-09-01 17:55:25 +02:00 committed by GitHub
commit df1a81ed26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 10 deletions

View File

@ -168,7 +168,6 @@ func newDecoder(ctx context.Context, format Format, br bitio.ReaderAtSeeker, opt
}
rootV := &Compound{
IsArray: format.RootArray,
RangeSorted: !format.RootArray,
Children: nil,
Description: opts.Description,
}
@ -765,7 +764,7 @@ func (d *D) FieldMustGet(name string) *Value {
// FieldArray decode array of fields. Will not be range sorted.
func (d *D) FieldArray(name string, fn func(d *D), sms ...scalar.Mapper) *D {
c := &Compound{IsArray: true, RangeSorted: false}
c := &Compound{IsArray: true}
cd := d.fieldDecoder(name, d.bitBuf, c)
d.AddChild(cd.Value)
fn(cd)
@ -779,7 +778,7 @@ func (d *D) FieldArrayValue(name string) *D {
// FieldStruct decode array of fields. Will be range sorted.
func (d *D) FieldStruct(name string, fn func(d *D)) *D {
c := &Compound{IsArray: false, RangeSorted: true}
c := &Compound{IsArray: false}
cd := d.fieldDecoder(name, d.bitBuf, c)
d.AddChild(cd.Value)
fn(cd)
@ -1104,7 +1103,7 @@ func (d *D) FieldRootBitBuf(name string, br bitio.ReaderAtSeeker, sms ...scalar.
}
func (d *D) FieldArrayRootBitBufFn(name string, br bitio.ReaderAtSeeker, fn func(d *D)) *Value {
c := &Compound{IsArray: true, RangeSorted: false}
c := &Compound{IsArray: true}
cd := d.fieldDecoder(name, br, c)
cd.Value.IsRoot = true
d.AddChild(cd.Value)
@ -1116,7 +1115,7 @@ func (d *D) FieldArrayRootBitBufFn(name string, br bitio.ReaderAtSeeker, fn func
}
func (d *D) FieldStructRootBitBufFn(name string, br bitio.ReaderAtSeeker, fn func(d *D)) *Value {
c := &Compound{IsArray: false, RangeSorted: true}
c := &Compound{IsArray: false}
cd := d.fieldDecoder(name, br, c)
cd.Value.IsRoot = true
d.AddChild(cd.Value)

View File

@ -11,7 +11,6 @@ import (
type Compound struct {
IsArray bool
RangeSorted bool
Children []*Value
ByName map[string]*Value
Description string
@ -198,10 +197,9 @@ func (v *Value) postProcess() {
}
}
// TODO: really sort array? if sort it needs to be stable to keep the order
// of value with same range start, think null values etc
if vv.RangeSorted {
slices.SortFunc(vv.Children, func(a, b *Value) bool { return a.Range.Start < b.Range.Start })
// 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 })
}
v.Index = -1