Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gen): add OverrideRegexp function #725

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/ex_github/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions examples/ex_k8s/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/ex_openapi/output.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions gen/_template/globals.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ var regexMap = map[string]ogenregex.Regexp{
{{ quote $regex }}: ogenregex.MustCompile({{ quote $regex }}),
{{- end }}
}

// OverrideRegexp allows to override regexes used in generated code.
// Returns true if regex was overridden, false otherwise.
//
// NOTE: this function MUST be called before any generated code is executed.
func OverrideRegexp(key string, r ogenregex.Regexp) (ok bool) {
_, ok = regexMap[key]
regexMap[key] = r
return ok
}
{{- end }}

{{- with $ratStrings := $.RatStrings }}
Expand Down
11 changes: 11 additions & 0 deletions internal/integration/sample_api/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/integration/sample_api_nc/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/integration/sample_api_ns/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/integration/sample_api_nsnc/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/integration/test_allof/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/integration/test_anyof/oas_cfg_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions ogenregex/ogenregex.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,45 @@ var _ = []Regexp{
}

type goRegexp struct {
exp *regexp.Regexp
re *regexp.Regexp
}

// Go returns wrapper for Go's regexp.
func Go(re *regexp.Regexp) Regexp {
return goRegexp{re}
}

func (r goRegexp) Match(s []byte) (bool, error) {
return r.exp.Match(s), nil
return r.re.Match(s), nil
}

func (r goRegexp) MatchString(s string) (bool, error) {
return r.exp.MatchString(s), nil
return r.re.MatchString(s), nil
}

func (r goRegexp) String() string {
return r.exp.String()
return r.re.String()
}

type regexp2Regexp struct {
exp *regexp2.Regexp
re *regexp2.Regexp
}

// Regexp2 returns wrapper for dlclark/regexp2.
func Regexp2(re *regexp2.Regexp) Regexp {
return regexp2Regexp{re}
}

func (r regexp2Regexp) Match(s []byte) (bool, error) {
return r.exp.MatchRunes([]rune(string(s)))
return r.re.MatchRunes([]rune(string(s)))
}

func (r regexp2Regexp) MatchString(s string) (bool, error) {
return r.exp.MatchString(s)
return r.re.MatchString(s)
}

func (r regexp2Regexp) String() string {
return r.exp.String()
return r.re.String()
}

// Regexp is a regular expression interface.
Expand All @@ -63,11 +73,11 @@ type Regexp interface {
// Compile compiles a regular expression.
//
// NOTE: this function may compile the same expression multiple times and can
// be slow. Compile the expression once and reuse it.
// be slow. Call it once and reuse the result.
func Compile(exp string) (Regexp, error) {
if converted, ok := Convert(exp); ok {
if re, err := regexp.Compile(converted); err == nil {
return goRegexp{re}, nil
return Go(re), nil
}
}
re, err := regexp2.Compile(exp, regexp2.ECMAScript|regexp2.Unicode)
Expand All @@ -77,7 +87,7 @@ func Compile(exp string) (Regexp, error) {
// FIXME(tdakkota): Default timeout is "forever", which may lead to DoS.
// Probably, we should make this configurable.
re.MatchTimeout = 15 * time.Second
return regexp2Regexp{re}, nil
return Regexp2(re), nil
}

// MustCompile compiles a regular expression and panics on error.
Expand Down