Skip to content

Commit

Permalink
decode: Move name/description into DecodeOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 16, 2021
1 parent c7416e6 commit 26d615b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
34 changes: 18 additions & 16 deletions pkg/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const (
type Options interface{ decodeOptions() }

type DecodeOptions struct {
Name string
Description string
IsRoot bool
StartOffset int64
FormatOptions map[string]interface{}
Expand All @@ -117,12 +119,12 @@ type FormatOptions struct {
func (FormatOptions) decodeOptions() {}

// Decode try decode formats and return first success and all other decoder errors
func Decode(ctx context.Context, name string, description string, bb *bitio.Buffer, formats []*Format, opts ...Options) (*Value, interface{}, error) {
func Decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts ...Options) (*Value, interface{}, error) {
opts = append(opts, DecodeOptions{IsRoot: true})
return decode(ctx, name, description, bb, formats, opts)
return decode(ctx, bb, formats, opts)
}

func decode(ctx context.Context, name string, description string, bb *bitio.Buffer, formats []*Format, opts []Options) (*Value, interface{}, error) {
func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts []Options) (*Value, interface{}, error) {
if formats == nil {
panic("formats is nil, failed to register format?")
}
Expand All @@ -145,7 +147,7 @@ func decode(ctx context.Context, name string, description string, bb *bitio.Buff
decodeErr := DecodeFormatsError{}

for _, f := range formats {
d := NewDecoder(ctx, name, description, f, bb, decodeOpts)
d := NewDecoder(ctx, f, bb, decodeOpts)

var decodeV interface{}

Expand Down Expand Up @@ -219,15 +221,15 @@ type D struct {
}

// TODO: new struct decoder?
func NewDecoder(ctx context.Context, name string, description string, format *Format, bb *bitio.Buffer, opts DecodeOptions) *D {
func NewDecoder(ctx context.Context, format *Format, bb *bitio.Buffer, opts DecodeOptions) *D {
cbb := bb.Copy()

return &D{
Ctx: ctx,
Endian: BigEndian,
Value: &Value{
Name: name,
Description: description,
Name: opts.Name,
Description: opts.Description,
Format: format,
V: Struct{},
IsRoot: opts.IsRoot,
Expand Down Expand Up @@ -864,7 +866,7 @@ func (d *D) DecodeRangeFn(firstBit int64, nBits int64, fn func(d *D)) {
func (d *D) Format(formats []*Format, opts ...Options) interface{} {
bb := d.BitBufRange(d.Pos(), d.BitsLeft())
opts = append(opts, DecodeOptions{ReadBuf: d.readBuf, IsRoot: false, StartOffset: d.Pos()})
dv, v, err := decode(d.Ctx, "", "", bb, formats, opts)
dv, v, err := decode(d.Ctx, bb, formats, opts)
if dv == nil || dv.Errors() != nil {
panic(err)
}
Expand All @@ -891,8 +893,8 @@ func (d *D) Format(formats []*Format, opts ...Options) interface{} {

func (d *D) FieldTryFormat(name string, formats []*Format, opts ...Options) (*Value, interface{}, error) {
bb := d.BitBufRange(d.Pos(), d.BitsLeft())
opts = append(opts, DecodeOptions{ReadBuf: d.readBuf, IsRoot: false, StartOffset: d.Pos()})
dv, v, err := decode(d.Ctx, name, "", bb, formats, opts)
opts = append(opts, DecodeOptions{Name: name, ReadBuf: d.readBuf, IsRoot: false, StartOffset: d.Pos()})
dv, v, err := decode(d.Ctx, bb, formats, opts)
if dv == nil || dv.Errors() != nil {
return nil, nil, err
}
Expand All @@ -915,8 +917,8 @@ func (d *D) FieldFormat(name string, formats []*Format, opts ...Options) (*Value

func (d *D) FieldTryFormatLen(name string, nBits int64, formats []*Format, opts ...Options) (*Value, interface{}, error) {
bb := d.BitBufRange(d.Pos(), nBits)
opts = append(opts, DecodeOptions{ReadBuf: d.readBuf, IsRoot: false, StartOffset: d.Pos()})
dv, v, err := decode(d.Ctx, name, "", bb, formats, opts)
opts = append(opts, DecodeOptions{Name: name, ReadBuf: d.readBuf, IsRoot: false, StartOffset: d.Pos()})
dv, v, err := decode(d.Ctx, bb, formats, opts)
if dv == nil || dv.Errors() != nil {
return nil, nil, err
}
Expand All @@ -940,8 +942,8 @@ func (d *D) FieldFormatLen(name string, nBits int64, formats []*Format, opts ...
// TODO: return decooder?
func (d *D) FieldTryFormatRange(name string, firstBit int64, nBits int64, formats []*Format, opts ...Options) (*Value, interface{}, error) {
bb := d.BitBufRange(firstBit, nBits)
opts = append(opts, DecodeOptions{ReadBuf: d.readBuf, IsRoot: false, StartOffset: firstBit})
dv, v, err := decode(d.Ctx, name, "", bb, formats, opts)
opts = append(opts, DecodeOptions{Name: name, ReadBuf: d.readBuf, IsRoot: false, StartOffset: firstBit})
dv, v, err := decode(d.Ctx, bb, formats, opts)
if dv == nil || dv.Errors() != nil {
return nil, nil, err
}
Expand All @@ -961,8 +963,8 @@ func (d *D) FieldFormatRange(name string, firstBit int64, nBits int64, formats [
}

func (d *D) FieldTryFormatBitBuf(name string, bb *bitio.Buffer, formats []*Format, opts ...Options) (*Value, interface{}, error) {
opts = append(opts, DecodeOptions{ReadBuf: d.readBuf, IsRoot: true})
dv, v, err := decode(d.Ctx, name, "", bb, formats, opts)
opts = append(opts, DecodeOptions{Name: name, ReadBuf: d.readBuf, IsRoot: true})
dv, v, err := decode(d.Ctx, bb, formats, opts)
if dv == nil || dv.Errors() != nil {
return nil, nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/interp/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,12 @@ func (i *Interp) _decode(c interface{}, a []interface{}) interface{} {
return fmt.Errorf("%s: %w", formatName, err)
}

dv, _, err := decode.Decode(i.evalContext.ctx, "", filename, bb, decodeFormats, decode.DecodeOptions{FormatOptions: opts})
dv, _, err := decode.Decode(i.evalContext.ctx, bb, decodeFormats,
decode.DecodeOptions{
Description: filename,
FormatOptions: opts,
},
)
if dv == nil {
var decodeFormatsErr decode.DecodeFormatsError
if errors.As(err, &decodeFormatsErr) {
Expand Down

0 comments on commit 26d615b

Please sign in to comment.