1
1
mirror of https://github.com/wader/fq.git synced 2024-12-25 06:12:30 +03:00
fq/pkg/scalar/scalar_gen.go.tmpl
Mattias Wadman 2fc0a71a47 decode: Refactor scalar usage
Move scalar into own package.
Split scalar code into decode related scalar code (that reads etc) and
scalar code that just transform the scalar value.
Use a scalar.Mapper interface instead of just a function.
Make mappers, assert and validat impement the interface.
2021-12-02 17:39:26 +01:00

64 lines
1.4 KiB
Cheetah

// Code below generated from scalar_gen.go.tmpl
package scalar
import (
"fmt"
"github.com/wader/fq/pkg/bitio"
)
{{- range $name, $t := $.types }}
// Type {{$name}}
// Actual{{$name}} asserts actual value is a {{$name}} and returns it
func (s S) Actual{{$name}}() {{$t.go_type}} {
v, ok := s.Actual.({{$t.go_type}})
if !ok {
panic(fmt.Sprintf("failed to type assert s.Actual %v as {{$t.go_type}}", s.Actual))
}
return v
}
// Sym{{$name}} asserts symbolic value is a {{$name}} and returns it
func (s S) Sym{{$name}}() {{$t.go_type}} {
v, ok := s.Sym.({{$t.go_type}})
if !ok {
panic(fmt.Sprintf("failed to type assert s.Sym %v as {{$t.go_type}}", s.Sym))
}
return v
}
{{end}}
{{- range $from_name, $from := $.types }}
{{- if ne $from.map_from false}}
// Map {{$from_name}} -> Scalar
type {{$from_name}}ToScalar map[{{$from.go_type}}]S
func (m {{$from_name}}ToScalar) MapScalar(s S) (S, error) {
a := s.Actual{{$from_name}}()
if ns, ok := m[a]; ok {
ns.Actual = a
s = ns
}
return s, nil
}
{{- range $to_name, $to := $.types }}
{{- if ne $to.map_to false}}
// Map {{$from_name}} -> sym {{$to_name}}
type {{$from_name}}ToSym{{$to_name}} map[{{$from.go_type}}]{{$to.go_type}}
func (m {{$from_name}}ToSym{{$to_name}}) MapScalar(s S) (S, error) {
if t, ok := m[s.Actual{{$from_name}}()]; ok {
s.Sym = t
}
return s, nil
}
{{- end}}
{{- end}}
{{- end}}
{{- end}}