Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mangling): fixed name mangling when special chars are substituted #78

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading