Skip to content

Commit

Permalink
fix(gen): add setup of regexMap/ratMap into jsonschema template
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Apr 18, 2022
1 parent 59ff299 commit 7e3dccc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
22 changes: 1 addition & 21 deletions gen/_template/cfg.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,7 @@
{{ $pkg := $.Package }}
{{ template "header" $ }}

{{- with $regexStrings := $.RegexStrings }}
var regexMap = map[string]*regexp.Regexp{
{{- range $regex := $regexStrings }}
{{ quote $regex }}: regexp.MustCompile({{ quote $regex }}),
{{- end }}
}
{{- end }}

{{- with $ratStrings := $.RatStrings }}
var ratMap = map[string]*big.Rat{
{{- range $rat := $ratStrings }}
{{ quote $rat }}: func() *big.Rat {
r := new(big.Rat)
if err := r.UnmarshalText([]byte({{ quote $rat }})); err != nil {
panic(fmt.Sprintf("rat %q: %v", {{ quote $rat }}, err))
}
return r
}(),
{{- end }}
}
{{- end }}
{{ template "globals" $ }}

// bufPool is pool of bytes.Buffer for encoding and decoding.
var bufPool = &sync.Pool{
Expand Down
27 changes: 27 additions & 0 deletions gen/_template/globals.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{- /*gotype: github.com/ogen-go/ogen/gen.TemplateConfig*/ -}}

{{ define "globals" }}

{{- with $regexStrings := $.RegexStrings }}
var regexMap = map[string]*regexp.Regexp{
{{- range $regex := $regexStrings }}
{{ quote $regex }}: regexp.MustCompile({{ quote $regex }}),
{{- end }}
}
{{- end }}

{{- with $ratStrings := $.RatStrings }}
var ratMap = map[string]*big.Rat{
{{- range $rat := $ratStrings }}
{{ quote $rat }}: func() *big.Rat {
r := new(big.Rat)
if err := r.UnmarshalText([]byte({{ quote $rat }})); err != nil {
panic(fmt.Sprintf("rat %q: %v", {{ quote $rat }}, err))
}
return r
}(),
{{- end }}
}
{{- end }}

{{- end }}
5 changes: 5 additions & 0 deletions gen/_template/jsonschema.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{{- /*gotype: github.com/ogen-go/ogen/gen.TemplateConfig*/ -}}

{{ define "jsonschema" }}

{{ $pkg := $.Package }}
{{ template "header" $ }}

{{ template "globals" $ }}

{{ template "schemas/body" $ }}
{{ template "json/body" $ }}
{{ template "validators/body" $ }}
Expand Down

0 comments on commit 7e3dccc

Please sign in to comment.