Skip to content

Commit 7421bdf

Browse files
authored
refactor: compile regex only once (#3063)
1 parent a4bf3a7 commit 7421bdf

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

‎api/generate.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
"github.com/99designs/gqlgen/plugin/resolvergen"
1414
)
1515

16+
var (
17+
urlRegex = regexp.MustCompile(`(?s)@link.*\(.*url:.*?"(.*?)"[^)]+\)`) // regex to grab the url of a link directive, should it exist
18+
versionRegex = regexp.MustCompile(`v(\d+).(\d+)$`) // regex to grab the version number from a url
19+
)
20+
1621
func Generate(cfg *config.Config, option ...Option) error {
1722
_ = syscall.Unlink(cfg.Exec.Filename)
1823
if cfg.Model.IsDefined() {
@@ -26,8 +31,6 @@ func Generate(cfg *config.Config, option ...Option) error {
2631
plugins = append(plugins, resolvergen.New())
2732
if cfg.Federation.IsDefined() {
2833
if cfg.Federation.Version == 0 { // default to using the user's choice of version, but if unset, try to sort out which federation version to use
29-
urlRegex := regexp.MustCompile(`(?s)@link.*\(.*url:.*?"(.*?)"[^)]+\)`) // regex to grab the url of a link directive, should it exist
30-
versionRegex := regexp.MustCompile(`v(\d+).(\d+)$`) // regex to grab the version number from a url
3134
// check the sources, and if one is marked as federation v2, we mark the entirety to be generated using that format
3235
for _, v := range cfg.Sources {
3336
cfg.Federation.Version = 1

‎client/client.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ func (p *Client) RawPost(query string, options ...Option) (*Response, error) {
106106
return respDataRaw, nil
107107
}
108108

109+
var boundaryRegex = regexp.MustCompile(`multipart/form-data; ?boundary=.*`)
110+
109111
func (p *Client) newRequest(query string, options ...Option) (*http.Request, error) {
110112
bd := &Request{
111113
Query: query,
@@ -124,7 +126,7 @@ func (p *Client) newRequest(query string, options ...Option) (*http.Request, err
124126

125127
contentType := bd.HTTP.Header.Get("Content-Type")
126128
switch {
127-
case regexp.MustCompile(`multipart/form-data; ?boundary=.*`).MatchString(contentType):
129+
case boundaryRegex.MatchString(contentType):
128130
break
129131
case "application/json" == contentType:
130132
requestBody, err := json.Marshal(bd)

0 commit comments

Comments
 (0)