Skip to content

Commit c53972f

Browse files
lvan100lianghuan
authored andcommitted
refactor(main): backup before removing default values
1 parent fe69bfe commit c53972f

File tree

14 files changed

+151
-1378
lines changed

14 files changed

+151
-1378
lines changed

gen/gen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func testProject(t *testing.T, dir string) {
6464
OutputDir: outDir,
6565
EnableServer: c.Server,
6666
EnableClient: c.Client,
67-
PackageName: "proto",
67+
GoPackage: "proto",
6868
ToolVersion: "v0.0.1",
6969
}
7070
if err = Gen(lang, config); err != nil {

gen/generator/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Config struct {
2828
OutputDir string // Directory where generated code will be written
2929
EnableServer bool // Whether to generate server code
3030
EnableClient bool // Whether to generate client code
31-
PackageName string // Go package name for generated code
31+
GoPackage string // Go package name for generated code
3232
ToolVersion string // Version of the code generation tool
3333
}
3434

gen/generator/golang/proto/http_handler.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/generator/golang/server.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/generator/golang/type.go

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const {{$c.Name}} {{$c.Type}} = {{$c.Value}}
120120
{{range $s := .Structs}}
121121
{{$s.Comment}}
122122
type {{$s.Name}} struct {
123+
ObjectBase
123124
{{- range $f := $s.Fields}}
124125
{{- if $f.Comment}}
125126
{{$f.Comment}}
@@ -128,61 +129,23 @@ const {{$c.Name}} {{$c.Type}} = {{$c.Value}}
128129
{{- end}}
129130
}
130131
131-
// New{{$s.Name}} creates a new instance of the struct and sets default values if defined
132-
func New{{$s.Name}}() *{{$s.Name}} {
133-
return &{{$s.Name}}{
134-
{{- range $f := $s.Fields}}
135-
{{- if $f.Default}}
136-
{{$f.Name}}: {{$f.Default}},
137-
{{- end}}
138-
{{- end}}
139-
}
140-
}
141-
142132
// New implements the Object interface
143133
func (x *{{$s.Name}}) New() any {
144-
return New{{$s.Name}}()
134+
return &{{$s.Name}}{}
145135
}
146136
147-
{{range $f := $s.Fields}}
148-
// Get{{$f.Name}} returns the value of {{$f.Name}}
149-
func (x *{{$s.Name}}) Get{{$f.Name}}() (r {{$f.Type}}) {
150-
if x != nil {
151-
return x.{{$f.Name}}
152-
}
153-
return r
154-
}
155-
156-
// Set{{$f.Name}} sets the value of {{$f.Name}}
157-
{{- if or (OptionalBaseType $f.TypeKind) (OptionalEnumType $f.TypeKind)}}
158-
func (x *{{$s.Name}}) Set{{$f.Name}}(v {{TrimPrefix $f.Type "*"}}) {
159-
if x != nil {
160-
x.{{$f.Name}} = &v
161-
}
162-
}
163-
{{- else}}
164-
func (x *{{$s.Name}}) Set{{$f.Name}}(v {{$f.Type}}) {
165-
if x != nil {
166-
x.{{$f.Name}} = v
167-
}
168-
}
169-
{{- end}}
170-
{{end}}
171-
172-
// Binding extracts non-body values (header, path, query) from *http.Request
173-
func (x *{{$s.Name}}) Binding(r *http.Request) error {
174-
{{- if $s.BindingCount}}
137+
{{- if $s.BindingCount}}
138+
// Binding extracts non-body values (header, path, query) from *http.Request
139+
func (x *{{$s.Name}}) Binding(r *http.Request) error {
175140
return Binding(r, []BindingField {
176141
{{- range $f := $s.Fields}}
177142
{{- if $f.Binding}}
178143
{"{{$s.Name}}.{{$f.Name}}", "{{$f.Binding.From}}", "{{$f.Binding.Name}}", &x.{{$f.Name}}},
179144
{{- end}}
180145
{{- end}}
181146
})
182-
{{- else}}
183-
return nil
184-
{{- end}}
185-
}
147+
}
148+
{{- end}}
186149
187150
// Validate checks field values using generated validation expressions
188151
func (x *{{$s.Name}}) Validate() error {
@@ -222,7 +185,7 @@ func (g *Generator) genType(ctx Context, fileName string, doc tidl.Document) err
222185

223186
buf := &bytes.Buffer{}
224187
err = typeTmpl.Execute(buf, map[string]any{
225-
"Package": ctx.config.PackageName,
188+
"Package": ctx.config.GoPackage,
226189
"Consts": consts,
227190
"Enums": enums,
228191
"Structs": types,

gen/generator/golang/validate.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/testdata/manager/go/manager_http_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
type MyManagerServer struct{}
1616

1717
func (m *MyManagerServer) GetManager(ctx context.Context, req *proto.ManagerReq) *proto.GetManagerResp {
18-
data := proto.NewManager()
19-
data.SetName("Jim")
20-
res := proto.NewGetManagerResp()
21-
res.SetData(data)
22-
return res
18+
return &proto.GetManagerResp{
19+
Data: &proto.Manager{
20+
Name: "Jim",
21+
Level: proto.ManagerLevelAsString(proto.ManagerLevel_JUNIOR),
22+
},
23+
}
2324
}
2425

2526
func (m *MyManagerServer) CreateManager(ctx context.Context, req *proto.CreateManagerReq) *proto.CreateManagerResp {

gen/testdata/manager/go/proto/http_handler.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)