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

chore: optimize code generation when GET http request with struct par… #206

Merged
merged 1 commit into from
May 26, 2024
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
20 changes: 18 additions & 2 deletions cmd/internal/svc/codegen/httphandlerimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var appendHttpHandlerImplTmpl = `
{{ $p.Name }} = new({{ $p.Type | toSlice }})
{{- else if eq $p.Type "context.Context"}}
{{ $p.Name }} {{ $p.Type }}
{{- else if and (eq $m.HttpMethod "GET") (not (isBuiltin $p)) }}
{{- else if and (and (eq $m.HttpMethod "GET") (not (isBuiltin $p))) (gt (len $m.QueryVars) 1) }}
{{ $p.Name }}Wrapper struct {
{{ $p.Name | title }} {{ $p.Type }} ` + "`" + `json:"{{ $p.Name }}"` + "`" + `
}
Expand Down Expand Up @@ -143,6 +143,7 @@ var appendHttpHandlerImplTmpl = `
}
{{- $formParsed = true }}
{{- end }}
{{- if gt (len $m.QueryVars) 1 }}
if _err := rest.DecodeForm(&{{ $p.Name }}Wrapper, _req.Form); _err != nil {
rest.HandleBadRequestErr(_err)
} else {
Expand All @@ -157,6 +158,21 @@ var appendHttpHandlerImplTmpl = `
{{- end }}
}
{{- else }}
if _err := rest.DecodeForm(&{{ $p.Name }}, _req.Form); _err != nil {
rest.HandleBadRequestErr(_err)
} else {
{{- if isStruct $p }}
if _err := rest.ValidateStruct({{ $p.Name }}); _err != nil {
rest.HandleBadRequestErr(_err)
}
{{- else }}
if _err := rest.ValidateVar({{ $p.Name }}, "{{$p.ValidateTag}}", ""); _err != nil {
rest.HandleBadRequestErr(_err)
}
{{- end }}
}
{{- end }}
{{- else }}
{{- if isOptional $p.Type }}
if _err := json.NewDecoder(_req.Body).Decode(&{{$p.Name}}); _err != nil {
if _err != io.EOF {
Expand Down Expand Up @@ -320,7 +336,7 @@ var appendHttpHandlerImplTmpl = `
*{{ $p.Name }}...,
{{- else if eq $p.Type "context.Context"}}
{{ $p.Name }},
{{- else if and (eq $m.HttpMethod "GET") (not (isBuiltin $p)) }}
{{- else if and (and (eq $m.HttpMethod "GET") (not (isBuiltin $p))) (gt (len $m.QueryVars) 1) }}
{{ $p.Name }}Wrapper.{{ $p.Name | title }},
{{- else }}
{{ $p.Name }},
Expand Down
16 changes: 7 additions & 9 deletions framework/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,14 @@ func (srv *RestServer) Serve(ln net.Listener) {
docs := mapset.NewSet[DocItem]()
for _, r := range all {
if strings.Contains(r.Pattern, gddPathPrefix+"doc") {
resources := strings.Split(strings.TrimPrefix(strings.TrimSuffix(r.Pattern, gddPathPrefix+"doc"), "/"), "/")
if len(resources) > 0 {
module := resources[len(resources)-1]
if stringutils.IsNotEmpty(module) {
docs.Add(DocItem{
Label: module,
Value: r.Pattern,
})
}
module := strings.TrimSuffix(r.Pattern, gddPathPrefix+"doc")
if stringutils.IsEmpty(module) {
module = "/"
}
docs.Add(DocItem{
Label: module,
Value: r.Pattern,
})
}
}
Docs = docs.ToSlice()
Expand Down
3 changes: 2 additions & 1 deletion toolkit/astutils/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ type MethodMeta struct {
// all will be put into request body as multipart/form-data data.
Params []FieldMeta
// Results response
Results []FieldMeta
Results []FieldMeta
QueryVars []FieldMeta
// PathVars not support when generate client code from service interface in svc.go file
// when generate client code from openapi3 spec json file, PathVars is parameters in url as path variable.
PathVars []FieldMeta
Expand Down
5 changes: 5 additions & 0 deletions toolkit/astutils/interfacecollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,16 @@ func (ic *InterfaceCollector) field2Methods(list []*ast.Field) []MethodMeta {
params = ic.field2Params(ft.Params.List)
}

queryVars := make([]FieldMeta, 0)
httpMethod, endpoint := Pattern(mn)
pvs := pathVariables(endpoint)
for i := range params {
if sliceutils.StringContains(pvs, params[i].Name) {
params[i].IsPathVariable = true
}
if httpMethod == http.MethodGet && !params[i].IsPathVariable && params[i].Type != "context.Context" {
queryVars = append(queryVars, params[i])
}
}

var results []FieldMeta
Expand All @@ -136,6 +140,7 @@ func (ic *InterfaceCollector) field2Methods(list []*ast.Field) []MethodMeta {
Annotations: annotations,
HasPathVariable: len(pvs) > 0,
HttpMethod: httpMethod,
QueryVars: queryVars,
})
}
return methods
Expand Down
Loading