Skip to content

Commit b3e7a53

Browse files
committed
fix(mangling): fixed name mangling when special chars are substituted
This PR fixes name mangling methods (ToJSONName, ToCommandName, etc) for cases when a special character is expanded, like so: "get$ref" should be jasonified as "getDollarRef". The issue was that the extra space added was not trimmed before name reconstruction, so in the example above, we get "getDollar Ref" instead. * contributes go-swagger/go-swagger#2821 Signed-off-by: Frederic BIDON <[email protected]>
1 parent 0ddf107 commit b3e7a53

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (s byInitialism) Less(i, j int) bool {
174174

175175
// Removes leading whitespaces
176176
func trim(str string) string {
177-
return strings.Trim(str, " ")
177+
return strings.TrimSpace(str)
178178
}
179179

180180
// Shortcut to strings.ToUpper()
@@ -231,7 +231,7 @@ func ToHumanNameLower(name string) string {
231231
if !w.IsInitialism() {
232232
out = append(out, lower(w.GetOriginal()))
233233
} else {
234-
out = append(out, w.GetOriginal())
234+
out = append(out, trim(w.GetOriginal()))
235235
}
236236
}
237237

@@ -244,7 +244,7 @@ func ToHumanNameTitle(name string) string {
244244

245245
out := make([]string, 0, len(in))
246246
for _, w := range in {
247-
original := w.GetOriginal()
247+
original := trim(w.GetOriginal())
248248
if !w.IsInitialism() {
249249
out = append(out, Camelize(original))
250250
} else {
@@ -264,7 +264,7 @@ func ToJSONName(name string) string {
264264
out = append(out, lower(w))
265265
continue
266266
}
267-
out = append(out, Camelize(w))
267+
out = append(out, Camelize(trim(w)))
268268
}
269269
return strings.Join(out, "")
270270
}

util_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func TestToGoName(t *testing.T) {
111111
{"日本語findThingById", "X日本語findThingByID"},
112112
{"findTHINGSbyID", "FindTHINGSbyID"},
113113
{"x-isAnOptionalHeader0", "XIsAnOptionalHeader0"},
114+
{"get$ref", "GetDollarRef"},
114115
}
115116

116117
for _, k := range commonInitialisms.sorted() {
@@ -242,6 +243,7 @@ func TestToFileName(t *testing.T) {
242243
{"ELB.HTTPLoadBalancer", "elb_http_load_balancer"},
243244
{"elbHTTPLoadBalancer", "elb_http_load_balancer"},
244245
{"ELBHTTPLoadBalancer", "elb_http_load_balancer"},
246+
{"get$Ref", "get_dollar_ref"},
245247
}
246248
for _, k := range commonInitialisms.sorted() {
247249
samples = append(samples,
@@ -261,6 +263,8 @@ func TestToCommandName(t *testing.T) {
261263
{"SampleText", "sample-text"},
262264
{"FindThingByID", "find-thing-by-id"},
263265
{"elbHTTPLoadBalancer", "elb-http-load-balancer"},
266+
{"get$ref", "get-dollar-ref"},
267+
{"get!ref", "get-bang-ref"},
264268
}
265269

266270
for _, k := range commonInitialisms.sorted() {
@@ -298,6 +302,8 @@ func TestToJSONName(t *testing.T) {
298302
{"SampleText", "sampleText"},
299303
{"FindThingByID", "findThingById"},
300304
{"elbHTTPLoadBalancer", "elbHttpLoadBalancer"},
305+
{"get$ref", "getDollarRef"},
306+
{"get!ref", "getBangRef"},
301307
}
302308

303309
for _, k := range commonInitialisms.sorted() {
@@ -426,6 +432,8 @@ func TestCamelize(t *testing.T) {
426432
{"elbHTTPLoadBalancer", "Elbhttploadbalancer"},
427433
{"ELBHTTPLoadBalancer", "Elbhttploadbalancer"},
428434
{"12ab", "12ab"},
435+
{"get$Ref", "Get$ref"},
436+
{"get!Ref", "Get!ref"},
429437
}
430438

431439
for _, sample := range samples {
@@ -447,6 +455,8 @@ func TestToHumanNameTitle(t *testing.T) {
447455
{"ELB.HTTPLoadBalancer", "ELB HTTP Load Balancer"},
448456
{"elbHTTPLoadBalancer", "elb HTTP Load Balancer"},
449457
{"ELBHTTPLoadBalancer", "ELB HTTP Load Balancer"},
458+
{"get$ref", "Get Dollar Ref"},
459+
{"get!ref", "Get Bang Ref"},
450460
}
451461

452462
for _, sample := range samples {
@@ -471,6 +481,8 @@ func TestToVarName(t *testing.T) {
471481
{"Id", "id"},
472482
{"HTTP", "http"},
473483
{"A", "a"},
484+
{"get$ref", "getDollarRef"},
485+
{"get!ref", "getBangRef"},
474486
}
475487

476488
for _, sample := range samples {
@@ -504,6 +516,8 @@ func TestToGoNameUnicode(t *testing.T) {
504516
{"abc", "Abc"},
505517
{"éabc", "Éabc"},
506518
{":éabc", "Éabc"},
519+
{"get$ref", "GetDollarRef"},
520+
{"get!ref", "GetBangRef"},
507521
// TODO: non unicode char
508522
}
509523

0 commit comments

Comments
 (0)