Skip to content

Commit

Permalink
Precompile regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Sneagan committed Nov 25, 2024
1 parent 2ebcbcd commit e09aa43
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions libs/wallet/provider/uphold/uphold.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,11 +1183,13 @@ func FundWallet(ctx context.Context, destWallet *Wallet, amount decimal.Decimal)
return balance.TotalProbi, nil
}

var redactRegexp = []regexp.Regexp{
*regexp.MustCompile(`"description":\s*"[^"]+"\s*,?`),
*regexp.MustCompile(`"\w+Country":\s*"[^"]+"\s*,?`),
}
func redactUnneededContent(sbody string) string {
for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} {
re := regexp.MustCompile(p)
for _, re := range redactRegexp {
sbody = re.ReplaceAllString(sbody, "")
}
sbody = strings.ReplaceAll(sbody, ",}", "}")
return sbody
return strings.ReplaceAll(sbody, ",}", "}")
}
2 changes: 1 addition & 1 deletion libs/wallet/provider/uphold/uphold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func requireDonorWallet(t *testing.T) *Wallet {
}

func TestRedactUnneeded(t *testing.T) {
response := `{"description":"some unneeded content","UKCountry":"foo","TestCountry":"bar","NoMatch": true}`
response := `{"description":"some unneeded content","OKCountry":"foo","TestCountry":"bar","NoMatch": true}`
result := `{"NoMatch": true}`
testValue := redactUnneededContent(response)
assert.Equal(t, result, testValue)
Expand Down

0 comments on commit e09aa43

Please sign in to comment.