Skip to content

Commit

Permalink
Merge pull request #206 from wubin1989/main
Browse files Browse the repository at this point in the history
chore: optimize code generation when GET http request with struct par…
  • Loading branch information
wubin1989 authored May 26, 2024
2 parents b53c03d + 749a36f commit ea34fbe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
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

0 comments on commit ea34fbe

Please sign in to comment.