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

support http multipart request #4378

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
23 changes: 12 additions & 11 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
templateFile: handlerTemplateFile,
builtinTemplate: handlerTemplate,
data: map[string]any{
"PkgName": pkgName,
"ImportPackages": genHandlerImports(group, route, rootPkg),
"HandlerName": handler,
"RequestType": util.Title(route.RequestTypeName()),
"LogicName": logicName,
"LogicType": strings.Title(getLogicName(route)),
"Call": strings.Title(strings.TrimSuffix(handler, "Handler")),
"HasResp": len(route.ResponseTypeName()) > 0,
"HasRequest": len(route.RequestTypeName()) > 0,
"HasDoc": len(route.JoinedDoc()) > 0,
"Doc": getDoc(route.JoinedDoc()),
"PkgName": pkgName,
"ImportPackages": genHandlerImports(group, route, rootPkg),
"HandlerName": handler,
"RequestType": util.Title(route.RequestTypeName()),
"LogicName": logicName,
"LogicType": strings.Title(getLogicName(route)),
"Call": strings.Title(strings.TrimSuffix(handler, "Handler")),
"HasResp": len(route.ResponseTypeName()) > 0,
"HasRequest": len(route.RequestTypeName()) > 0,
"HasDoc": len(route.JoinedDoc()) > 0,
"Doc": getDoc(route.JoinedDoc()),
"NeedParseMultipart": len(route.OpenFiles) > 0,
},
})
}
Expand Down
10 changes: 8 additions & 2 deletions tools/goctl/api/gogen/genlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group,
}
if len(route.RequestTypeName()) > 0 {
requestString = "req *" + requestGoTypeName(route, typesPacket)
if len(route.OpenFiles) > 0 {
requestString = "r * http.Request, " + requestString
}
}

subDir := getLogicFolderPath(group, route)
return genFile(fileGenConfig{
dir: dir,
Expand All @@ -72,6 +74,7 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group,
"request": requestString,
"hasDoc": len(route.JoinedDoc()) > 0,
"doc": getDoc(route.JoinedDoc()),
"openFiles": route.OpenFiles,
},
})
}
Expand All @@ -91,7 +94,10 @@ func getLogicFolderPath(group spec.Group, route spec.Route) string {

func genLogicImports(route spec.Route, parentPkg string) string {
var imports []string
imports = append(imports, `"context"`+"\n")
imports = append(imports, `"context"`)
if len(route.OpenFiles) != 0 {
imports = append(imports, fmt.Sprintf("\"net/http\"\n"))
}
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
if shallImportTypesPackage(route) {
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
Expand Down
11 changes: 11 additions & 0 deletions tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ func writeType(writer io.Writer, tp spec.Type) error {

func writeMember(writer io.Writer, members []spec.Member) error {
for _, member := range members {
var existFileTag bool
for _, t := range member.Tags() {
if t.Key == "file" {
existFileTag = true
break
}
}
if existFileTag {
continue
}

if member.IsInline {
if _, err := fmt.Fprintf(writer, "%s\n", strings.Title(member.Type.Name())); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/handler.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
}

{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .NeedParseMultipart}}r,{{end}}{{if .HasRequest}}&req{{end}})
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
Expand Down
8 changes: 8 additions & 0 deletions tools/goctl/api/gogen/logic.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}}

func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
// todo: add your logic here and delete this line
{{ range $k,$v:=.openFiles }}
{{$v}},{{$v}}Header, err := r.FormFile("{{$v}}")
if err != nil {
return
}
defer {{$v}}.Close()
_ = {{$v}}Header
{{end}}

{{.returnString}}
}
21 changes: 14 additions & 7 deletions tools/goctl/api/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (p parser) fillTypes() error {
for _, member := range v.Members {
switch v := member.Type.(type) {
case spec.DefineStruct:
tp, err := p.findDefinedType(v.RawName)
tp, _, err := p.findDefinedType(v.RawName)
if err != nil {
return err
}
Expand All @@ -167,16 +167,22 @@ func (p parser) fillTypes() error {
return nil
}

func (p parser) findDefinedType(name string) (*spec.Type, error) {
func (p parser) findDefinedType(name string) (*spec.Type, []string, error) {
for _, item := range p.spec.Types {
if _, ok := item.(spec.DefineStruct); ok {
if s, ok := item.(spec.DefineStruct); ok {
if item.Name() == name {
return &item, nil
var fileTagNames []string
for _, m := range s.Members {
if m.Tag == "file" {
fileTagNames = append(fileTagNames, m.Name)
}
}
return &item, fileTagNames, nil
}
}
}

return nil, fmt.Errorf("type %s not defined", name)
return nil, nil, fmt.Errorf("type %s not defined", name)
}

func (p parser) fieldToMember(field *ast.TypeField) spec.Member {
Expand Down Expand Up @@ -351,19 +357,20 @@ func (p parser) fillRouteType(route *spec.Route) error {
if route.RequestType != nil {
switch route.RequestType.(type) {
case spec.DefineStruct:
tp, err := p.findDefinedType(route.RequestType.Name())
tp, tagNames, err := p.findDefinedType(route.RequestType.Name())
if err != nil {
return err
}

route.RequestType = *tp
route.OpenFiles = tagNames
}
}

if route.ResponseType != nil {
switch route.ResponseType.(type) {
case spec.DefineStruct:
tp, err := p.findDefinedType(route.ResponseType.Name())
tp, _, err := p.findDefinedType(route.ResponseType.Name())
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions tools/goctl/api/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type (
Path string
RequestType Type
ResponseType Type
OpenFiles []string
Docs Doc
Handler string
AtDoc AtDoc
Expand Down
25 changes: 17 additions & 8 deletions tools/goctl/pkg/parser/api/parser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,20 @@ func (a *Analyzer) fillRouteType(route *spec.Route) error {
if route.RequestType != nil {
switch route.RequestType.(type) {
case spec.DefineStruct:
tp, err := a.findDefinedType(route.RequestType.Name())
tp, tagNames, err := a.findDefinedType(route.RequestType.Name())
if err != nil {
return err
}

route.RequestType = tp
route.OpenFiles = tagNames
}
}

if route.ResponseType != nil {
switch route.ResponseType.(type) {
case spec.DefineStruct:
tp, err := a.findDefinedType(route.ResponseType.Name())
tp, _, err := a.findDefinedType(route.ResponseType.Name())
if err != nil {
return err
}
Expand Down Expand Up @@ -347,7 +348,7 @@ func (a *Analyzer) fillTypes() error {
for _, member := range v.Members {
switch v := member.Type.(type) {
case spec.DefineStruct:
tp, err := a.findDefinedType(v.RawName)
tp, _, err := a.findDefinedType(v.RawName)
if err != nil {
return err
}
Expand Down Expand Up @@ -390,16 +391,24 @@ func (a *Analyzer) fillTypeExpr(expr *ast.TypeExpr) error {
}
}

func (a *Analyzer) findDefinedType(name string) (spec.Type, error) {
func (a *Analyzer) findDefinedType(name string) (spec.Type, []string, error) {
for _, item := range a.spec.Types {
if _, ok := item.(spec.DefineStruct); ok {
if s, ok := item.(spec.DefineStruct); ok {
if item.Name() == name {
return item, nil
var fileTagNames []string
for _, m := range s.Members {
for _, t := range m.Tags() {
if t.Key == "file" {
fileTagNames = append(fileTagNames, t.Name)
}
}
}
return item, fileTagNames, nil
}
}
}

return nil, fmt.Errorf("type %s not defined", name)
return nil, nil, fmt.Errorf("type %s not defined", name)
}

func (a *Analyzer) getType(expr *ast.BodyStmt, req bool) (spec.Type, error) {
Expand All @@ -414,7 +423,7 @@ func (a *Analyzer) getType(expr *ast.BodyStmt, req bool) (spec.Type, error) {
if IsBaseType(body.Value.Token.Text) {
tp = spec.PrimitiveType{RawName: body.Value.Token.Text}
} else {
tp, err = a.findDefinedType(body.Value.Token.Text)
tp, _, err = a.findDefinedType(body.Value.Token.Text)
if err != nil {
return nil, err
}
Expand Down