From 531aaa44de12ec166ceb71d9bdad7c8295e4235f Mon Sep 17 00:00:00 2001 From: Ian Coleman Date: Thu, 13 Jul 2023 10:09:19 +1000 Subject: [PATCH] Fix concurrency for acronyms Closes #43 --- acronyms.go | 11 +++++++---- camel.go | 4 ++-- concurrency_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 concurrency_test.go diff --git a/acronyms.go b/acronyms.go index 9c3110d..7a2fb09 100644 --- a/acronyms.go +++ b/acronyms.go @@ -1,10 +1,13 @@ package strcase -var uppercaseAcronym = map[string]string{ - "ID": "id", -} +import ( + "sync" +) + +var uppercaseAcronym = sync.Map{} + //"ID": "id", // ConfigureAcronym allows you to add additional words which will be considered acronyms func ConfigureAcronym(key, val string) { - uppercaseAcronym[key] = val + uppercaseAcronym.Store(key, val) } diff --git a/camel.go b/camel.go index c928561..6b88689 100644 --- a/camel.go +++ b/camel.go @@ -35,9 +35,9 @@ func toCamelInitCase(s string, initCase bool) string { if s == "" { return s } - a, hasAcronym := uppercaseAcronym[s] + a, hasAcronym := uppercaseAcronym.Load(s) if hasAcronym { - s = a + s = a.(string) } n := strings.Builder{} diff --git a/concurrency_test.go b/concurrency_test.go new file mode 100644 index 0000000..bfe04e2 --- /dev/null +++ b/concurrency_test.go @@ -0,0 +1,46 @@ +package strcase + +import ( + "testing" +) + +func TestConcurrency(t *testing.T) { + for i := 0; i < 10000; i++ { + go doConvert() + } + +} + +func doConvert() { + var snakes = []string{"code", "exchange", "pe_ratio", "profit_margin", "updated_date"} + for _, v := range snakes { + _ = convertDatabaseNameToCamelCase(v) + } +} + +func convertDatabaseNameToCamelCase(d string) (s string) { + ConfigureAcronym("price_book_mrq", "PriceBookMRQ") + ConfigureAcronym("pe_ratio", "PERatio") + ConfigureAcronym("peg_ratio", "PEGRatio") + ConfigureAcronym("eps_estimate_current_year", "EPSEstimateCurrentYear") + ConfigureAcronym("eps_estimate_next_year", "EPSEstimateNextYear") + ConfigureAcronym("eps_estimate_next_quarter", "EPSNextQuarter") + ConfigureAcronym("eps_estimate_current_quarter", "EPSEstimateCurrentQuarter") + ConfigureAcronym("ebitda", "EBITDA") + ConfigureAcronym("operating_margin_ttm", "OperatingMarginTTM") + ConfigureAcronym("return_on_assets_ttm", "ReturnOnAssetsTTM") + ConfigureAcronym("return_on_equity_ttm", "ReturnOnEquityTTM") + ConfigureAcronym("revenue_ttm", "RevenueTTM") + ConfigureAcronym("revenue_per_share_ttm", "RevenuePerShareTTM") + ConfigureAcronym("quarterly_revenue_growth_yoy", "QuarterlyRevenueGrowthYOY") + ConfigureAcronym("gross_profit_ttm", "GrossProfitTTM") + ConfigureAcronym("diluted_eps_ttm", "DilutedEpsTTM") + ConfigureAcronym("quarterly_earnings_growth_yoy", "QuarterlyEarningsGrowthYOY") + ConfigureAcronym("two_hundred_day_ma", "TwoHundredDayMA") + ConfigureAcronym("trailing_pe", "TrailingPE") + ConfigureAcronym("forward_pe", "ForwardPE") + ConfigureAcronym("price_sales_ttm", "PriceSalesTTM") + ConfigureAcronym("price_book_mrq", "PriceBookMRQ") + s = ToCamel(d) + return +}