// Code below generated from decode_gen.go.tmpl package decode import ( "fmt" "math/big" "github.com/wader/fq/pkg/bitio" "github.com/wader/fq/pkg/scalar" ) {{- range $name, $t := $.types }} // Type {{$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) { v, err := d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return fn(d) }, sms...) if err != nil { return {{$t.zero}}, err } return v.Actual{{$name}}(), err } // 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}} { v, err := d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return fn(d), nil }, sms...) if err != nil { panic(IOError{Err: err, Name: name, Op: "{{$name}}", Pos: d.Pos()}) } return v.Actual{{$name}}() } // Field{{$name}}Fn adds a field, calls {{$t.go_type}} decode function and returns actual value as a {{$name}} func (d *D) Field{{$name}}Fn(name string, fn func(d *D) {{$t.go_type}}, sms ...scalar.Mapper) {{$t.go_type}} { return d.Field{{$name}}ScalarFn(name, func(d *D) scalar.S { return scalar.S{Actual: fn(d) } }, sms...) } // TryField{{$name}}Fn tries to add a field, calls {{$t.go_type}} decode function and returns actual value as a {{$name}} func (d *D) TryField{{$name}}Fn(name string, fn func(d *D) ({{$t.go_type}}, error), sms ...scalar.Mapper) ({{$t.go_type}}, error) { return d.TryField{{$name}}ScalarFn(name, func(d *D) (scalar.S, error) { v, err := fn(d) return scalar.S{Actual: v}, err }, sms...) } // 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) { return d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { v, err := fn(d) return scalar.S{Actual: v}, err }, sms...) } // FieldScalar{{$name}}Fn tries to add a field, calls {{$t.go_type}} decode function and returns scalar func (d *D) FieldScalar{{$name}}Fn(name string, fn func(d *D) {{$t.go_type}}, sms ...scalar.Mapper) *scalar.S { v, err := d.TryFieldScalar{{$name}}Fn(name, func(d *D) ({{$t.go_type}}, error) { return fn(d), nil }, sms...) if err != nil { panic(IOError{Err: err, Name: name, Op: "{{$name}}", Pos: d.Pos()}) } return v } {{end}} {{- range $name, $t := $.types }} {{- if $t.compare}} // Require/Assert/Validate {{$name}} func require{{$name}}(name string, s scalar.S, desc bool, fail bool, vs ...{{$t.go_type}}) (scalar.S, error) { a := s.Actual{{$name}}() for _, b := range vs { if {{$t.compare}} { if desc { s.Description = "valid" } return s, nil } } if desc { s.Description = "invalid" } if fail { return s, fmt.Errorf("failed to %s {{$name}}", name) } return s, nil } // Require{{$name}} that actual value is one of given {{$t.go_type}} values func (d *D) Require{{$name}}(vs ...{{$t.go_type}}) scalar.Mapper { return scalar.Fn(func(s scalar.S) (scalar.S, error) { return require{{$name}}("require", s, false, true, vs...) }) } // Assert{{$name}} validate and asserts that actual value is one of given {{$t.go_type}} values func (d *D) Assert{{$name}}(vs ...{{$t.go_type}}) scalar.Mapper { return scalar.Fn(func(s scalar.S) (scalar.S, error) { return require{{$name}}("assert", s, true, !d.Options.Force, vs...) }) } // Validate{{$name}} validates that actual value is one of given {{$t.go_type}} values func (d *D) Validate{{$name}}(vs ...{{$t.go_type}}) scalar.Mapper { return scalar.Fn(func(s scalar.S) (scalar.S, error) { return require{{$name}}("validate", s, true, false, vs...) }) } {{- end}} {{- if $t.range}} // Require/Assert/ValidatRange {{$name}} func requireRange{{$name}}(name string, s scalar.S, desc bool, fail bool, start, end {{$t.go_type}}) (scalar.S, error) { a := s.Actual{{$name}}() if {{$t.range}} { if desc { s.Description = "valid" } return s, nil } if desc { s.Description = "invalid" } if fail { return s, fmt.Errorf("failed to %s {{$name}} range %v-%v", name, start, end) } return s, nil } // Require{{$name}}Range require that actual value is in range func (d *D) Require{{$name}}Range(start, end {{$t.go_type}}) scalar.Mapper { return scalar.Fn(func(s scalar.S) (scalar.S, error) { return requireRange{{$name}}("require", s, false, true, start, end) }) } // Assert{{$name}}Range asserts that actual value is in range func (d *D) Assert{{$name}}Range(start, end {{$t.go_type}}) scalar.Mapper { return scalar.Fn(func(s scalar.S) (scalar.S, error) { return requireRange{{$name}}("assert", s, true, !d.Options.Force, start, end) }) } // Validate{{$name}}Range validates that actual value is in range func (d *D) Validate{{$name}}Range(start, end {{$t.go_type}}) scalar.Mapper { return scalar.Fn(func(s scalar.S) (scalar.S, error) { return requireRange{{$name}}("validate", s, true, false, start, end) }) } {{- end}} {{- end}} {{- range $r := $.readers }} {{- $t := index $.types $r.type }} {{- range $v := $r.variants }} {{- $range_start := 1 }} {{- $range_stop := 2 }} {{- if $v.range }} {{- $range_start = index $v.range 0 }} {{- $range_stop = index $v.range 1 }} {{- end}} {{- range $n := xrange $range_start $range_stop }} // Reader {{$r.name}}{{replace $v.name "$n" $n}} // Try{{$r.name}}{{replace $v.name "$n" $n}} tries to read {{replace $v.doc "$n" $n}} func (d *D) Try{{$r.name}}{{replace $v.name "$n" $n}}({{$v.params}}) ({{$t.go_type}}, error) { return {{replace $v.call "$n" $n}} } // {{$r.name}}{{replace $v.name "$n" $n}} reads {{replace $v.doc "$n" $n}} func (d *D) {{$r.name}}{{replace $v.name "$n" $n}}({{$v.params}}) {{$t.go_type}} { v, err := {{replace $v.call "$n" $n}} if err != nil { panic(IOError{Err: err, Op: "{{$r.name}}{{replace $v.name "$n" $n}}", Pos: d.Pos()}) } return v } // TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}} tries to add a field and read {{replace $v.doc "$n" $n}} 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) { s, err := d.TryFieldScalarFn(name, func(s scalar.S) (scalar.S, error) { v, err := {{replace $v.call "$n" $n}} s.Actual = v return s, err }, sms...) if err != nil { return nil, err } return s, err } // 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}} 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}} { return d.FieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...).Actual{{$r.type}}() } {{- end}} {{- end}} {{- end}}