1
1
mirror of https://github.com/wader/fq.git synced 2024-11-30 09:58:13 +03:00

decode: Generate Try?Scalar* methods for readers too

This commit is contained in:
Mattias Wadman 2021-12-04 18:54:35 +01:00
parent 9aec91a675
commit 75aa47556a
3 changed files with 9815 additions and 965 deletions

View File

@ -610,27 +610,27 @@ func (d *D) AssertLeastBytesLeft(nBytes int64) {
// TODO: rethink // TODO: rethink
func (d *D) FieldValueU(name string, a uint64, sms ...scalar.Mapper) { func (d *D) FieldValueU(name string, a uint64, sms ...scalar.Mapper) {
d.FieldScalar(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...) d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...)
} }
func (d *D) FieldValueS(name string, a int64, sms ...scalar.Mapper) { func (d *D) FieldValueS(name string, a int64, sms ...scalar.Mapper) {
d.FieldScalar(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...) d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...)
} }
func (d *D) FieldValueBool(name string, a bool, sms ...scalar.Mapper) { func (d *D) FieldValueBool(name string, a bool, sms ...scalar.Mapper) {
d.FieldScalar(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...) d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...)
} }
func (d *D) FieldValueFloat(name string, a float64, sms ...scalar.Mapper) { func (d *D) FieldValueFloat(name string, a float64, sms ...scalar.Mapper) {
d.FieldScalar(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...) d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...)
} }
func (d *D) FieldValueStr(name string, a string, sms ...scalar.Mapper) { func (d *D) FieldValueStr(name string, a string, sms ...scalar.Mapper) {
d.FieldScalar(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...) d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...)
} }
func (d *D) FieldValueRaw(name string, a []byte, sms ...scalar.Mapper) { func (d *D) FieldValueRaw(name string, a []byte, sms ...scalar.Mapper) {
d.FieldScalar(name, func(_ scalar.S) (scalar.S, error) { d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) {
return scalar.S{Actual: bitio.NewBufferFromBytes(a, -1)}, nil return scalar.S{Actual: bitio.NewBufferFromBytes(a, -1)}, nil
}, sms...) }, sms...)
} }
@ -926,7 +926,7 @@ func (d *D) FieldValue(name string, fn func() *Value) *Value {
} }
// looks a bit weird to force at least one ScalarFn arg // looks a bit weird to force at least one ScalarFn arg
func (d *D) TryFieldScalar(name string, sfn scalar.Fn, sms ...scalar.Mapper) (*scalar.S, error) { func (d *D) TryFieldScalarFn(name string, sfn scalar.Fn, sms ...scalar.Mapper) (*scalar.S, error) {
v, err := d.TryFieldValue(name, func() (*Value, error) { v, err := d.TryFieldValue(name, func() (*Value, error) {
s, err := sfn(scalar.S{}) s, err := sfn(scalar.S{})
if err != nil { if err != nil {
@ -946,8 +946,8 @@ func (d *D) TryFieldScalar(name string, sfn scalar.Fn, sms ...scalar.Mapper) (*s
return v.V.(*scalar.S), nil return v.V.(*scalar.S), nil
} }
func (d *D) FieldScalar(name string, sfn scalar.Fn, sms ...scalar.Mapper) *scalar.S { func (d *D) FieldScalarFn(name string, sfn scalar.Fn, sms ...scalar.Mapper) *scalar.S {
v, err := d.TryFieldScalar(name, sfn, sms...) v, err := d.TryFieldScalarFn(name, sfn, sms...)
if err != nil { if err != nil {
panic(err) panic(err)
} }

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@ import (
// TryField{{$name}}ScalarFn tries to add a field, calls scalar functions and returns actual value as a {{$name}} // TryField{{$name}}ScalarFn tries to add a field, calls scalar functions and returns actual value as a {{$name}}
func (d *D) TryField{{$name}}ScalarFn(name string, fn func(d *D) (scalar.S, error), sms ...scalar.Mapper) ({{$t.go_type}}, error) { func (d *D) TryField{{$name}}ScalarFn(name string, fn func(d *D) (scalar.S, error), sms ...scalar.Mapper) ({{$t.go_type}}, error) {
v, err := d.TryFieldScalar(name, func(_ scalar.S) (scalar.S, error) { return fn(d) }, sms...) v, err := d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return fn(d) }, sms...)
if err != nil { if err != nil {
return {{$t.zero}}, err return {{$t.zero}}, err
} }
@ -22,7 +22,7 @@ import (
// Field{{$name}}ScalarFn adds a field, calls scalar functions and returns actual value as a {{$name}} // Field{{$name}}ScalarFn adds a field, calls scalar functions and returns actual value as a {{$name}}
func (d *D) Field{{$name}}ScalarFn(name string, fn func(d *D) scalar.S, sms ...scalar.Mapper) {{$t.go_type}} { func (d *D) Field{{$name}}ScalarFn(name string, fn func(d *D) scalar.S, sms ...scalar.Mapper) {{$t.go_type}} {
v, err := d.TryFieldScalar(name, func(_ scalar.S) (scalar.S, error) { return fn(d), nil }, sms...) v, err := d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return fn(d), nil }, sms...)
if err != nil { if err != nil {
panic(IOError{Err: err, Name: name, Op: "{{$name}}", Pos: d.Pos()}) panic(IOError{Err: err, Name: name, Op: "{{$name}}", Pos: d.Pos()})
} }
@ -44,7 +44,7 @@ import (
// TryFieldScalar{{$name}}Fn tries to add a field, calls {{$t.go_type}} decode function and returns scalar // TryFieldScalar{{$name}}Fn tries to add a field, calls {{$t.go_type}} decode function and returns scalar
func (d *D) TryFieldScalar{{$name}}Fn(name string, fn func(d *D) ({{$t.go_type}}, error), sms ...scalar.Mapper) (*scalar.S, error) { func (d *D) TryFieldScalar{{$name}}Fn(name string, fn func(d *D) ({{$t.go_type}}, error), sms ...scalar.Mapper) (*scalar.S, error) {
return d.TryFieldScalar(name, func(_ scalar.S) (scalar.S, error) { return d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) {
v, err := fn(d) v, err := fn(d)
return scalar.S{Actual: v}, err return scalar.S{Actual: v}, err
}, sms...) }, sms...)
@ -118,34 +118,37 @@ import (
return v return v
} }
// TryField{{$r.name}}{{replace $v.name "$n" $n}} tries to add a field and read {{replace $v.doc "$n" $n}} // TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}} tries to add a field and read {{replace $v.doc "$n" $n}}
func (d *D) TryField{{$r.name}}{{replace $v.name "$n" $n}}(name string{{if $v.params}}, {{$v.params}}{{end}}, sms ...scalar.Mapper) ({{$t.go_type}}, error) { func (d *D) TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name string{{if $v.params}}, {{$v.params}}{{end}}, sms ...scalar.Mapper) (*scalar.S, error) {
{{- if $v.params}} s, err := d.TryFieldScalarFn(name, func(s scalar.S) (scalar.S, error) {
v, err := d.TryFieldScalar(name, func(s scalar.S) (scalar.S, error) {
v, err := {{replace $v.call "$n" $n}} v, err := {{replace $v.call "$n" $n}}
s.Actual = v s.Actual = v
return s, err return s, err
}, sms...) }, sms...)
if err != nil { if err != nil {
return {{$t.zero}}, err return nil, err
} }
return v.Actual{{$r.type}}(), err return s, err
{{- else}} }
return d.TryField{{$r.type}}Fn(name, (*D).Try{{$r.name}}{{replace $v.name "$n" $n}}, sms...)
{{- end}} // FieldScalar{{$r.name}}{{replace $v.name "$n" $n}} adds a field and reads {{replace $v.doc "$n" $n}}
func (d *D) FieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name string{{if $v.params}}, {{$v.params}}{{end}}, sms ...scalar.Mapper) *scalar.S {
s, err := d.TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...)
if err != nil {
panic(IOError{Err: err, Name: name, Op: "{{$r.name}}{{replace $v.name "$n" $n}}", Pos: d.Pos()})
}
return s
}
// TryField{{$r.name}}{{replace $v.name "$n" $n}} tries to add a field and read {{replace $v.doc "$n" $n}}
func (d *D) TryField{{$r.name}}{{replace $v.name "$n" $n}}(name string{{if $v.params}}, {{$v.params}}{{end}}, sms ...scalar.Mapper) ({{$t.go_type}}, error) {
s, err := d.TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...)
return s.Actual{{$r.type}}(), err
} }
// Field{{$r.name}}{{replace $v.name "$n" $n}} adds a field and reads {{replace $v.doc "$n" $n}} // Field{{$r.name}}{{replace $v.name "$n" $n}} adds a field and reads {{replace $v.doc "$n" $n}}
func (d *D) Field{{$r.name}}{{replace $v.name "$n" $n}}(name string{{if $v.params}}, {{$v.params}}{{end}}, sms ...scalar.Mapper) {{$t.go_type}} { func (d *D) Field{{$r.name}}{{replace $v.name "$n" $n}}(name string{{if $v.params}}, {{$v.params}}{{end}}, sms ...scalar.Mapper) {{$t.go_type}} {
{{- if $v.params}} return d.FieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...).Actual{{$r.type}}()
v, err := d.TryField{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...)
if err != nil {
panic(IOError{Err: err, Name: name, Op: "{{$r.name}}{{replace $v.name "$n" $n}}", Pos: d.Pos()})
}
return v
{{- else}}
return d.Field{{$r.type}}Fn(name, (*D).{{$r.name}}{{replace $v.name "$n" $n}}, sms...)
{{- end}}
} }
{{- end}} {{- end}}
{{- end}} {{- end}}