Skip to content

Commit

Permalink
regexp.MustCompile outside in the function
Browse files Browse the repository at this point in the history
goos: linux
goarch: amd64
pkg: github.com/fujiwara/tfstate-lookup/tfstate
cpu: AMD Ryzen 5 3400G with Radeon Vega Graphics
BenchmarkLookupFile-8               5895            197672 ns/op          109939 B/op       1171 allocs/op
PASS
  • Loading branch information
fujiwara committed Sep 17, 2024
1 parent 2ac40ae commit f4fcddc
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,13 @@ func (s *TFState) Lookup(key string) (*Object, error) {
// we must quote them like `.outputs["repository-arn"]`.
//
// quoteJQQuery does it.
var (
quoteSplitRegex = regexp.MustCompile(`[.\[\]]`)
quoteIndexRegex = regexp.MustCompile(`^-?[0-9]+$`)
)

func quoteJQQuery(query string) string {
splitRegex := regexp.MustCompile(`[.\[\]]`)
indexRegex := regexp.MustCompile(`^-?[0-9]+$`)
parts := splitRegex.Split(query, -1)
parts := quoteSplitRegex.Split(query, -1)
parts_coalesced := make([]string, 0, len(parts))

for _, part := range parts {
Expand All @@ -259,7 +262,7 @@ func quoteJQQuery(query string) string {

for _, part := range parts_coalesced {
builder.WriteByte('[')
if indexRegex.MatchString(part) {
if quoteIndexRegex.MatchString(part) {
builder.WriteString(part)
} else {
if !strings.HasPrefix(part, `"`) {
Expand Down

0 comments on commit f4fcddc

Please sign in to comment.