Skip to content

Commit 70ba534

Browse files
lvan100lianghuan
authored andcommitted
Field names use Pascal case
1 parent 81f0482 commit 70ba534

File tree

5 files changed

+133
-3
lines changed

5 files changed

+133
-3
lines changed

gen/generator/generator.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package generator
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
"github.com/go-spring/gs-http-gen/lib/tidl"
2324
)
@@ -89,3 +90,24 @@ func CapitalizeASCII(s string) string {
8990
}
9091
return s
9192
}
93+
94+
// ToPascal converts a snake_case string to PascalCase.
95+
// For example: "hello_world" becomes "HelloWorld".
96+
func ToPascal(s string) string {
97+
var sb strings.Builder
98+
parts := strings.Split(s, "_")
99+
for _, part := range parts {
100+
if part == "" {
101+
continue
102+
}
103+
c := part[0]
104+
if 'a' <= c && c <= 'z' {
105+
c = c - 'a' + 'A'
106+
}
107+
sb.WriteByte(c)
108+
if len(part) > 1 {
109+
sb.WriteString(part[1:])
110+
}
111+
}
112+
return sb.String()
113+
}

gen/generator/generator_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package generator
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestToPascal(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input string
11+
expected string
12+
}{
13+
{
14+
name: "empty string",
15+
input: "",
16+
expected: "",
17+
},
18+
{
19+
name: "single lowercase word",
20+
input: "hello",
21+
expected: "Hello",
22+
},
23+
{
24+
name: "snake_case with two words",
25+
input: "hello_world",
26+
expected: "HelloWorld",
27+
},
28+
{
29+
name: "snake_case with multiple words",
30+
input: "hello_world_golang",
31+
expected: "HelloWorldGolang",
32+
},
33+
{
34+
name: "leading underscore",
35+
input: "_hello",
36+
expected: "Hello",
37+
},
38+
{
39+
name: "trailing underscore",
40+
input: "hello_",
41+
expected: "Hello",
42+
},
43+
{
44+
name: "multiple leading underscores",
45+
input: "__hello",
46+
expected: "Hello",
47+
},
48+
{
49+
name: "multiple trailing underscores",
50+
input: "hello__",
51+
expected: "Hello",
52+
},
53+
{
54+
name: "middle double underscore",
55+
input: "hello__world",
56+
expected: "HelloWorld",
57+
},
58+
{
59+
name: "multiple underscores",
60+
input: "hello___world___golang",
61+
expected: "HelloWorldGolang",
62+
},
63+
{
64+
name: "all uppercase snake_case",
65+
input: "HELLO_WORLD",
66+
expected: "HELLOWORLD",
67+
},
68+
{
69+
name: "mixed case snake_case",
70+
input: "Hello_World",
71+
expected: "HelloWorld",
72+
},
73+
{
74+
name: "with numbers",
75+
input: "http_200_status",
76+
expected: "Http200Status",
77+
},
78+
{
79+
name: "first character uppercase",
80+
input: "Http_client",
81+
expected: "HttpClient",
82+
},
83+
{
84+
name: "only underscores",
85+
input: "___",
86+
expected: "",
87+
},
88+
{
89+
name: "single character words",
90+
input: "a_b_c",
91+
expected: "ABC",
92+
},
93+
{
94+
name: "non-ascii characters",
95+
input: "héllo_wörld",
96+
expected: "HélloWörld",
97+
},
98+
}
99+
100+
for _, tt := range tests {
101+
t.Run(tt.name, func(t *testing.T) {
102+
actual := ToPascal(tt.input)
103+
if actual != tt.expected {
104+
t.Errorf("ToPascal(%q) = %q, expected %q", tt.input, actual, tt.expected)
105+
}
106+
})
107+
}
108+
}

gen/generator/golang/type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ func convertType(ctx Context, t tidl.Type) (Type, error) {
491491
}
492492

493493
// Get field name and Go type
494-
fieldName := generator.CapitalizeASCII(f.Name)
494+
fieldName := generator.ToPascal(f.Name)
495495
typeName, err := getTypeName(ctx, f.FieldType, f.Annotations)
496496
if err != nil {
497497
return Type{}, err

gen/testdata/manager/idl/manager.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ type UpdateManagerReq {
125125
float? salary (validate="$ >= SALARY_MIN && $ <= SALARY_MAX")
126126
string? role
127127
ManagerLevel? level
128-
DepartmentInfo? deptInfo
128+
DepartmentInfo? dept_info
129129
ContactInfo? contact
130130
}
131131

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
// ToolVersion defines the current version of this code generation tool.
29-
const ToolVersion = "v0.0.1"
29+
const ToolVersion = "v0.0.2"
3030

3131
func main() {
3232
var (

0 commit comments

Comments
 (0)