diff options
Diffstat (limited to 'vendor/github.com/go-openapi/swag')
-rw-r--r-- | vendor/github.com/go-openapi/swag/util.go | 40 | ||||
-rw-r--r-- | vendor/github.com/go-openapi/swag/util_test.go | 13 |
2 files changed, 24 insertions, 29 deletions
diff --git a/vendor/github.com/go-openapi/swag/util.go b/vendor/github.com/go-openapi/swag/util.go index 0efb41735..7e0f80a41 100644 --- a/vendor/github.com/go-openapi/swag/util.go +++ b/vendor/github.com/go-openapi/swag/util.go @@ -20,6 +20,7 @@ import ( "regexp" "sort" "strings" + "sync" "unicode" ) @@ -67,7 +68,9 @@ var commonInitialisms = map[string]bool{ } var initialisms []string -func init() { +var once sync.Once + +func sortInitialisms() { for k := range commonInitialisms { initialisms = append(initialisms, k) } @@ -166,6 +169,7 @@ func split(str string) (words []string) { str = rex1.ReplaceAllString(str, " $1") // check if consecutive single char things make up an initialism + once.Do(sortInitialisms) for _, k := range initialisms { str = strings.Replace(str, rex1.ReplaceAllString(k, " $1"), " "+k, -1) } @@ -205,34 +209,11 @@ func Camelize(word string) (camelized string) { // ToFileName lowercases and underscores a go type name func ToFileName(name string) string { var out []string - cml := trim(name) - - // Camelize any capital word preceding a reserved keyword ("initialism") - // thus, upper-cased words preceding a common initialism will get separated - // e.g: ELBHTTPLoadBalancer becomes elb_http_load_balancer - rexPrevious := regexp.MustCompile(`(?P<word>\p{Lu}{2,})(?:HTTP|OAI)`) - cml = rexPrevious.ReplaceAllStringFunc(cml, func(match string) (replaceInMatch string) { - for _, m := range rexPrevious.FindAllStringSubmatch(match, -1) { // [ match submatch ] - if m[1] != "" { - replaceInMatch = strings.Replace(m[0], m[1], Camelize(m[1]), -1) - } - } - return - }) - - // Pre-camelize reserved keywords ("initialisms") to avoid unnecessary hyphenization - for _, k := range initialisms { - cml = strings.Replace(cml, k, Camelize(k), -1) - } - - // Camelize other capital words to avoid unnecessary hyphenization - rexCase := regexp.MustCompile(`(\p{Lu}{2,})`) - cml = rexCase.ReplaceAllStringFunc(cml, Camelize) - // Final split with hyphens - for _, w := range split(cml) { + for _, w := range split(name) { out = append(out, lower(w)) } + return strings.Join(out, "_") } @@ -366,6 +347,13 @@ func IsZero(data interface{}) bool { return false } +// AddInitialisms add additional initialisms +func AddInitialisms(words ...string) { + for _, word := range words { + commonInitialisms[upper(word)] = true + } +} + // CommandLineOptionsGroup represents a group of user-defined command line options type CommandLineOptionsGroup struct { ShortDescription string diff --git a/vendor/github.com/go-openapi/swag/util_test.go b/vendor/github.com/go-openapi/swag/util_test.go index 3aeb925fa..c00738ef6 100644 --- a/vendor/github.com/go-openapi/swag/util_test.go +++ b/vendor/github.com/go-openapi/swag/util_test.go @@ -29,6 +29,10 @@ type translationSample struct { func titleize(s string) string { return strings.ToTitle(s[:1]) + lower(s[1:]) } +func init() { + AddInitialisms("elb", "cap", "capwd", "wd") +} + func TestToGoName(t *testing.T) { samples := []translationSample{ {"sample text", "SampleText"}, @@ -123,9 +127,9 @@ func TestToFileName(t *testing.T) { samples := []translationSample{ {"SampleText", "sample_text"}, {"FindThingByID", "find_thing_by_id"}, - {"CAPWD.folwdBYlc", "capwd_folwd_bylc"}, - {"CAPWDfolwdBYlc", "capwdfolwd_bylc"}, - {"CAP_WD_folwdBYlc", "cap_wd_folwd_bylc"}, + {"CAPWD.folwdBylc", "capwd_folwd_bylc"}, + {"CAPWDfolwdBylc", "capwdfolwd_bylc"}, + {"CAP_WD_folwdBylc", "cap_wd_folwd_bylc"}, {"TypeOAI_alias", "type_oai_alias"}, {"Type_OAI_alias", "type_oai_alias"}, {"Type_OAIAlias", "type_oai_alias"}, @@ -148,6 +152,7 @@ func TestToCommandName(t *testing.T) { samples := []translationSample{ {"SampleText", "sample-text"}, {"FindThingByID", "find-thing-by-id"}, + {"elbHTTPLoadBalancer", "elb-http-load-balancer"}, } for k := range commonInitialisms { @@ -165,6 +170,7 @@ func TestToHumanName(t *testing.T) { samples := []translationSample{ {"SampleText", "sample text"}, {"FindThingByID", "find thing by ID"}, + {"elbHTTPLoadBalancer", "elb HTTP load balancer"}, } for k := range commonInitialisms { @@ -182,6 +188,7 @@ func TestToJSONName(t *testing.T) { samples := []translationSample{ {"SampleText", "sampleText"}, {"FindThingByID", "findThingById"}, + {"elbHTTPLoadBalancer", "elbHttpLoadBalancer"}, } for k := range commonInitialisms { |