Skip to content

Commit 69b160d

Browse files
committed
address review feedback
1 parent 38fc4c5 commit 69b160d

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

internal/librariangen/bazel/parser.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"regexp"
2424
"strconv"
2525
"strings"
26+
"sync"
2627
)
2728

2829
// Config holds configuration extracted from a googleapis BUILD.bazel file.
@@ -59,6 +60,7 @@ func (c *Config) Validate() error {
5960
return nil
6061
}
6162

63+
var javaGapicLibraryRE = regexp.MustCompile(`java_gapic_library\((?s:.)*?\)`)
6264
// Parse reads a BUILD.bazel file from the given directory and extracts the
6365
// relevant configuration from the java_gapic_library rule.
6466
func Parse(dir string) (*Config, error) {
@@ -70,8 +72,7 @@ func Parse(dir string) (*Config, error) {
7072
}
7173
content := string(data)
7274

73-
re := regexp.MustCompile(`java_gapic_library\((?s:.)*?\)`)
74-
gapicLibraryBlock := re.FindString(content)
75+
gapicLibraryBlock := javaGapicLibraryRE.FindString(content)
7576
if gapicLibraryBlock != "" {
7677
c.hasGAPIC = true
7778
c.grpcServiceConfig = findString(gapicLibraryBlock, "grpc_service_config")
@@ -84,12 +85,23 @@ func Parse(dir string) (*Config, error) {
8485
if err := c.Validate(); err != nil {
8586
return nil, fmt.Errorf("librariangen: invalid bazel config in %s: %w", dir, err)
8687
}
87-
slog.Debug("librariangen: bazel config loaded", "conf", fmt.Sprintf("%+v", c))
88+
slog.Debug("librariangen: bazel config loaded", "conf", c)
8889
return c, nil
8990
}
9091

92+
var reCache = &sync.Map{}
93+
94+
func getRegexp(key, pattern string) *regexp.Regexp {
95+
val, ok := reCache.Load(key)
96+
if !ok {
97+
val = regexp.MustCompile(pattern)
98+
reCache.Store(key, val)
99+
}
100+
return val.(*regexp.Regexp)
101+
}
102+
91103
func findString(content, name string) string {
92-
re := regexp.MustCompile(fmt.Sprintf(`%s\s*=\s*"([^"]+)"`, name))
104+
re := getRegexp("findString_"+name, fmt.Sprintf(`%s\s*=\s*"([^"]+)"`, name))
93105
if match := re.FindStringSubmatch(content); len(match) > 1 {
94106
return match[1]
95107
}
@@ -98,7 +110,7 @@ func findString(content, name string) string {
98110
}
99111

100112
func findBool(content, name string) (bool, error) {
101-
re := regexp.MustCompile(fmt.Sprintf(`%s\s*=\s*(\w+)`, name))
113+
re := getRegexp("findBool_"+name, fmt.Sprintf(`%s\s*=\s*(\w+)`, name))
102114
if match := re.FindStringSubmatch(content); len(match) > 1 {
103115
if b, err := strconv.ParseBool(match[1]); err == nil {
104116
return b, nil

internal/librariangen/bazel/parser_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,31 @@ java_gapic_library(
5757
t.Fatalf("Parse() failed: %v", err)
5858
}
5959

60-
if !got.HasGAPIC() {
61-
t.Error("HasGAPIC() = false; want true")
62-
}
63-
if want := "cloudasset_v1.yaml"; got.ServiceYAML() != want {
64-
t.Errorf("ServiceYAML() = %q; want %q", got.ServiceYAML(), want)
65-
}
66-
if want := "cloudasset_grpc_service_config.json"; got.GRPCServiceConfig() != want {
67-
t.Errorf("GRPCServiceConfig() = %q; want %q", got.GRPCServiceConfig(), want)
68-
}
69-
if want := "grpc+rest"; got.Transport() != want {
70-
t.Errorf("Transport() = %q; want %q", got.Transport(), want)
71-
}
72-
if !got.HasRESTNumericEnums() {
73-
t.Error("HasRESTNumericEnums() = false; want true")
74-
}
60+
t.Run("HasGAPIC", func(t *testing.T) {
61+
if !got.HasGAPIC() {
62+
t.Error("HasGAPIC() = false; want true")
63+
}
64+
})
65+
t.Run("ServiceYAML", func(t *testing.T) {
66+
if want := "cloudasset_v1.yaml"; got.ServiceYAML() != want {
67+
t.Errorf("ServiceYAML() = %q; want %q", got.ServiceYAML(), want)
68+
}
69+
})
70+
t.Run("GRPCServiceConfig", func(t *testing.T) {
71+
if want := "cloudasset_grpc_service_config.json"; got.GRPCServiceConfig() != want {
72+
t.Errorf("GRPCServiceConfig() = %q; want %q", got.GRPCServiceConfig(), want)
73+
}
74+
})
75+
t.Run("Transport", func(t *testing.T) {
76+
if want := "grpc+rest"; got.Transport() != want {
77+
t.Errorf("Transport() = %q; want %q", got.Transport(), want)
78+
}
79+
})
80+
t.Run("HasRESTNumericEnums", func(t *testing.T) {
81+
if !got.HasRESTNumericEnums() {
82+
t.Error("HasRESTNumericEnums() = false; want true")
83+
}
84+
})
7585
}
7686

7787
func TestParse_serviceConfigIsTarget(t *testing.T) {

0 commit comments

Comments
 (0)