Skip to content

Commit 531aaa4

Browse files
committed
Fix concurrency for acronyms
Closes #43
1 parent 6ce6fd7 commit 531aaa4

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

acronyms.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package strcase
22

3-
var uppercaseAcronym = map[string]string{
4-
"ID": "id",
5-
}
3+
import (
4+
"sync"
5+
)
6+
7+
var uppercaseAcronym = sync.Map{}
8+
//"ID": "id",
69

710
// ConfigureAcronym allows you to add additional words which will be considered acronyms
811
func ConfigureAcronym(key, val string) {
9-
uppercaseAcronym[key] = val
12+
uppercaseAcronym.Store(key, val)
1013
}

camel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func toCamelInitCase(s string, initCase bool) string {
3535
if s == "" {
3636
return s
3737
}
38-
a, hasAcronym := uppercaseAcronym[s]
38+
a, hasAcronym := uppercaseAcronym.Load(s)
3939
if hasAcronym {
40-
s = a
40+
s = a.(string)
4141
}
4242

4343
n := strings.Builder{}

concurrency_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package strcase
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestConcurrency(t *testing.T) {
8+
for i := 0; i < 10000; i++ {
9+
go doConvert()
10+
}
11+
12+
}
13+
14+
func doConvert() {
15+
var snakes = []string{"code", "exchange", "pe_ratio", "profit_margin", "updated_date"}
16+
for _, v := range snakes {
17+
_ = convertDatabaseNameToCamelCase(v)
18+
}
19+
}
20+
21+
func convertDatabaseNameToCamelCase(d string) (s string) {
22+
ConfigureAcronym("price_book_mrq", "PriceBookMRQ")
23+
ConfigureAcronym("pe_ratio", "PERatio")
24+
ConfigureAcronym("peg_ratio", "PEGRatio")
25+
ConfigureAcronym("eps_estimate_current_year", "EPSEstimateCurrentYear")
26+
ConfigureAcronym("eps_estimate_next_year", "EPSEstimateNextYear")
27+
ConfigureAcronym("eps_estimate_next_quarter", "EPSNextQuarter")
28+
ConfigureAcronym("eps_estimate_current_quarter", "EPSEstimateCurrentQuarter")
29+
ConfigureAcronym("ebitda", "EBITDA")
30+
ConfigureAcronym("operating_margin_ttm", "OperatingMarginTTM")
31+
ConfigureAcronym("return_on_assets_ttm", "ReturnOnAssetsTTM")
32+
ConfigureAcronym("return_on_equity_ttm", "ReturnOnEquityTTM")
33+
ConfigureAcronym("revenue_ttm", "RevenueTTM")
34+
ConfigureAcronym("revenue_per_share_ttm", "RevenuePerShareTTM")
35+
ConfigureAcronym("quarterly_revenue_growth_yoy", "QuarterlyRevenueGrowthYOY")
36+
ConfigureAcronym("gross_profit_ttm", "GrossProfitTTM")
37+
ConfigureAcronym("diluted_eps_ttm", "DilutedEpsTTM")
38+
ConfigureAcronym("quarterly_earnings_growth_yoy", "QuarterlyEarningsGrowthYOY")
39+
ConfigureAcronym("two_hundred_day_ma", "TwoHundredDayMA")
40+
ConfigureAcronym("trailing_pe", "TrailingPE")
41+
ConfigureAcronym("forward_pe", "ForwardPE")
42+
ConfigureAcronym("price_sales_ttm", "PriceSalesTTM")
43+
ConfigureAcronym("price_book_mrq", "PriceBookMRQ")
44+
s = ToCamel(d)
45+
return
46+
}

0 commit comments

Comments
 (0)