Skip to content

Commit

Permalink
fix(mangling): fixed name mangling when special chars are substituted
Browse files Browse the repository at this point in the history
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 <fredbi@yahoo.com>
  • Loading branch information
fredbi committed Dec 27, 2023
1 parent 0ddf107 commit b3e7a53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (s byInitialism) Less(i, j int) bool {

// Removes leading whitespaces
func trim(str string) string {
return strings.Trim(str, " ")
return strings.TrimSpace(str)
}

// Shortcut to strings.ToUpper()
Expand Down Expand Up @@ -231,7 +231,7 @@ func ToHumanNameLower(name string) string {
if !w.IsInitialism() {
out = append(out, lower(w.GetOriginal()))
} else {
out = append(out, w.GetOriginal())
out = append(out, trim(w.GetOriginal()))
}
}

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

out := make([]string, 0, len(in))
for _, w := range in {
original := w.GetOriginal()
original := trim(w.GetOriginal())
if !w.IsInitialism() {
out = append(out, Camelize(original))
} else {
Expand All @@ -264,7 +264,7 @@ func ToJSONName(name string) string {
out = append(out, lower(w))
continue
}
out = append(out, Camelize(w))
out = append(out, Camelize(trim(w)))
}
return strings.Join(out, "")
}
Expand Down
14 changes: 14 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestToGoName(t *testing.T) {
{"日本語findThingById", "X日本語findThingByID"},
{"findTHINGSbyID", "FindTHINGSbyID"},
{"x-isAnOptionalHeader0", "XIsAnOptionalHeader0"},
{"get$ref", "GetDollarRef"},
}

for _, k := range commonInitialisms.sorted() {
Expand Down Expand Up @@ -242,6 +243,7 @@ func TestToFileName(t *testing.T) {
{"ELB.HTTPLoadBalancer", "elb_http_load_balancer"},
{"elbHTTPLoadBalancer", "elb_http_load_balancer"},
{"ELBHTTPLoadBalancer", "elb_http_load_balancer"},
{"get$Ref", "get_dollar_ref"},
}
for _, k := range commonInitialisms.sorted() {
samples = append(samples,
Expand All @@ -261,6 +263,8 @@ func TestToCommandName(t *testing.T) {
{"SampleText", "sample-text"},
{"FindThingByID", "find-thing-by-id"},
{"elbHTTPLoadBalancer", "elb-http-load-balancer"},
{"get$ref", "get-dollar-ref"},
{"get!ref", "get-bang-ref"},
}

for _, k := range commonInitialisms.sorted() {
Expand Down Expand Up @@ -298,6 +302,8 @@ func TestToJSONName(t *testing.T) {
{"SampleText", "sampleText"},
{"FindThingByID", "findThingById"},
{"elbHTTPLoadBalancer", "elbHttpLoadBalancer"},
{"get$ref", "getDollarRef"},
{"get!ref", "getBangRef"},
}

for _, k := range commonInitialisms.sorted() {
Expand Down Expand Up @@ -426,6 +432,8 @@ func TestCamelize(t *testing.T) {
{"elbHTTPLoadBalancer", "Elbhttploadbalancer"},
{"ELBHTTPLoadBalancer", "Elbhttploadbalancer"},
{"12ab", "12ab"},
{"get$Ref", "Get$ref"},
{"get!Ref", "Get!ref"},
}

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

for _, sample := range samples {
Expand All @@ -471,6 +481,8 @@ func TestToVarName(t *testing.T) {
{"Id", "id"},
{"HTTP", "http"},
{"A", "a"},
{"get$ref", "getDollarRef"},
{"get!ref", "getBangRef"},
}

for _, sample := range samples {
Expand Down Expand Up @@ -504,6 +516,8 @@ func TestToGoNameUnicode(t *testing.T) {
{"abc", "Abc"},
{"éabc", "Éabc"},
{":éabc", "Éabc"},
{"get$ref", "GetDollarRef"},
{"get!ref", "GetBangRef"},
// TODO: non unicode char
}

Expand Down

0 comments on commit b3e7a53

Please sign in to comment.