1// Code below generated from scalar_gen.go.tmpl 2package decode 3 4import ( 5 "fmt" 6 7 "github.com/wader/fq/pkg/bitio" 8 "github.com/wader/fq/pkg/scalar" 9) 10 11{{- range $name, $t := $.types }} 12 // Type {{$name}} 13 14 // TryField{{$name}}ScalarFn tries to add a field, calls scalar functions and returns actual value as a {{$name}} 15 func (d *D) TryField{{$name}}ScalarFn(name string, fn func(d *D) (scalar.S, error), sms ...scalar.Mapper) ({{$t.go_type}}, error) { 16 v, err := d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return fn(d) }, sms...) 17 if err != nil { 18 return {{$t.zero}}, err 19 } 20 return v.Actual{{$name}}(), err 21 } 22 23 // Field{{$name}}ScalarFn adds a field, calls scalar functions and returns actual value as a {{$name}} 24 func (d *D) Field{{$name}}ScalarFn(name string, fn func(d *D) scalar.S, sms ...scalar.Mapper) {{$t.go_type}} { 25 v, err := d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return fn(d), nil }, sms...) 26 if err != nil { 27 panic(IOError{Err: err, Name: name, Op: "{{$name}}", Pos: d.Pos()}) 28 } 29 return v.Actual{{$name}}() 30 } 31 32 // Field{{$name}}Fn adds a field, calls {{$t.go_type}} decode function and returns actual value as a {{$name}} 33 func (d *D) Field{{$name}}Fn(name string, fn func(d *D) {{$t.go_type}}, sms ...scalar.Mapper) {{$t.go_type}} { 34 return d.Field{{$name}}ScalarFn(name, func(d *D) scalar.S { return scalar.S{Actual: fn(d) } }, sms...) 35 } 36 37 // TryField{{$name}}Fn tries to add a field, calls {{$t.go_type}} decode function and returns actual value as a {{$name}} 38 func (d *D) TryField{{$name}}Fn(name string, fn func(d *D) ({{$t.go_type}}, error), sms ...scalar.Mapper) ({{$t.go_type}}, error) { 39 return d.TryField{{$name}}ScalarFn(name, func(d *D) (scalar.S, error) { 40 v, err := fn(d) 41 return scalar.S{Actual: v}, err 42 }, sms...) 43 } 44 45 // TryFieldScalar{{$name}}Fn tries to add a field, calls {{$t.go_type}} decode function and returns scalar 46 func (d *D) TryFieldScalar{{$name}}Fn(name string, fn func(d *D) ({{$t.go_type}}, error), sms ...scalar.Mapper) (*scalar.S, error) { 47 return d.TryFieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { 48 v, err := fn(d) 49 return scalar.S{Actual: v}, err 50 }, sms...) 51 } 52 53 // FieldScalar{{$name}}Fn tries to add a field, calls {{$t.go_type}} decode function and returns scalar 54 func (d *D) FieldScalar{{$name}}Fn(name string, fn func(d *D) {{$t.go_type}}, sms ...scalar.Mapper) *scalar.S { 55 v, err := d.TryFieldScalar{{$name}}Fn(name, func(d *D) ({{$t.go_type}}, error) { return fn(d), nil }, sms...) 56 if err != nil { 57 panic(IOError{Err: err, Name: name, Op: "{{$name}}", Pos: d.Pos()}) 58 } 59 return v 60 } 61{{end}} 62 63{{- range $name, $t := $.types }} 64 {{- if $t.compare}} 65 // Require/Assert/Validate {{$name}} 66 67 func require{{$name}}(name string, s scalar.S, desc bool, fail bool, vs ...{{$t.go_type}}) (scalar.S, error) { 68 a := s.Actual{{$name}}() 69 for _, b := range vs { 70 if {{$t.compare}} { 71 if desc { 72 s.Description = "valid" 73 } 74 return s, nil 75 } 76 } 77 if desc { 78 s.Description = "invalid" 79 } 80 if fail { 81 return s, fmt.Errorf("failed to %s {{$name}}", name) 82 } 83 return s, nil 84 } 85 86 // Require{{$name}} that actual value is one of given {{$t.go_type}} values 87 func (d *D) Require{{$name}}(vs ...{{$t.go_type}}) scalar.Mapper { 88 return scalar.Fn(func(s scalar.S) (scalar.S, error) { return require{{$name}}("require", s, false, true, vs...) }) 89 } 90 91 // Assert{{$name}} validate and asserts that actual value is one of given {{$t.go_type}} values 92 func (d *D) Assert{{$name}}(vs ...{{$t.go_type}}) scalar.Mapper { 93 return scalar.Fn(func(s scalar.S) (scalar.S, error) { return require{{$name}}("assert", s, true, !d.Options.Force, vs...) }) 94 } 95 96 // Validate{{$name}} validates that actual value is one of given {{$t.go_type}} values 97 func (d *D) Validate{{$name}}(vs ...{{$t.go_type}}) scalar.Mapper { 98 return scalar.Fn(func(s scalar.S) (scalar.S, error) { return require{{$name}}("validate", s, true, false, vs...) }) 99 } 100 {{- end}} 101 {{- if $t.range}} 102 // Require/Assert/ValidatRange {{$name}} 103 104 func requireRange{{$name}}(name string, s scalar.S, desc bool, fail bool, start, end {{$t.go_type}}) (scalar.S, error) { 105 a := s.Actual{{$name}}() 106 if {{$t.range}} { 107 if desc { 108 s.Description = "valid" 109 } 110 return s, nil 111 } 112 if desc { 113 s.Description = "invalid" 114 } 115 if fail { 116 return s, fmt.Errorf("failed to %s {{$name}} range %v-%v", name, start, end) 117 } 118 return s, nil 119 } 120 121 // Require{{$name}}Range require that actual value is in range 122 func (d *D) Require{{$name}}Range(start, end {{$t.go_type}}) scalar.Mapper { 123 return scalar.Fn(func(s scalar.S) (scalar.S, error) { return requireRange{{$name}}("require", s, false, true, start, end) }) 124 } 125 126 // Assert{{$name}}Range asserts that actual value is in range 127 func (d *D) Assert{{$name}}Range(start, end {{$t.go_type}}) scalar.Mapper { 128 return scalar.Fn(func(s scalar.S) (scalar.S, error) { return requireRange{{$name}}("assert", s, true, !d.Options.Force, start, end) }) 129 } 130 131 // Validate{{$name}}Range validates that actual value is in range 132 func (d *D) Validate{{$name}}Range(start, end {{$t.go_type}}) scalar.Mapper { 133 return scalar.Fn(func(s scalar.S) (scalar.S, error) { return requireRange{{$name}}("validate", s, true, false, start, end) }) 134 } 135 {{- end}} 136{{- end}} 137 138{{- range $r := $.readers }} 139 {{- $t := index $.types $r.type }} 140 141 {{- range $v := $r.variants }} 142 {{- $range_start := 1 }} 143 {{- $range_stop := 1 }} 144 {{- if $v.range }} 145 {{- $range_start = index $v.range 0 }} 146 {{- $range_stop = index $v.range 1 }} 147 {{- end}} 148 149 {{- range $n := xrange $range_start $range_stop }} 150 151 // Reader {{$r.name}}{{replace $v.name "$n" $n}} 152 153 // Try{{$r.name}}{{replace $v.name "$n" $n}} tries to read {{replace $v.doc "$n" $n}} 154 func (d *D) Try{{$r.name}}{{replace $v.name "$n" $n}}({{$v.params}}) ({{$t.go_type}}, error) { return {{replace $v.call "$n" $n}} } 155 156 // {{$r.name}}{{replace $v.name "$n" $n}} reads {{replace $v.doc "$n" $n}} 157 func (d *D) {{$r.name}}{{replace $v.name "$n" $n}}({{$v.params}}) {{$t.go_type}} { 158 v, err := {{replace $v.call "$n" $n}} 159 if err != nil { 160 panic(IOError{Err: err, Op: "{{$r.name}}{{replace $v.name "$n" $n}}", Pos: d.Pos()}) 161 } 162 return v 163 } 164 165 // TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}} tries to add a field and read {{replace $v.doc "$n" $n}} 166 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) { 167 s, err := d.TryFieldScalarFn(name, func(s scalar.S) (scalar.S, error) { 168 v, err := {{replace $v.call "$n" $n}} 169 s.Actual = v 170 return s, err 171 }, sms...) 172 if err != nil { 173 return nil, err 174 } 175 return s, err 176 } 177 178 // FieldScalar{{$r.name}}{{replace $v.name "$n" $n}} adds a field and reads {{replace $v.doc "$n" $n}} 179 func (d *D) FieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name string{{if $v.params}}, {{$v.params}}{{end}}, sms ...scalar.Mapper) *scalar.S { 180 s, err := d.TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...) 181 if err != nil { 182 panic(IOError{Err: err, Name: name, Op: "{{$r.name}}{{replace $v.name "$n" $n}}", Pos: d.Pos()}) 183 } 184 return s 185 } 186 187 // TryField{{$r.name}}{{replace $v.name "$n" $n}} tries to add a field and read {{replace $v.doc "$n" $n}} 188 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) { 189 s, err := d.TryFieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...) 190 return s.Actual{{$r.type}}(), err 191 } 192 193 // Field{{$r.name}}{{replace $v.name "$n" $n}} adds a field and reads {{replace $v.doc "$n" $n}} 194 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}} { 195 return d.FieldScalar{{$r.name}}{{replace $v.name "$n" $n}}(name{{if $v.args}}, {{$v.args}}{{end}}, sms...).Actual{{$r.type}}() 196 } 197 {{- end}} 198 {{- end}} 199{{- end}} 200