-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from chaosannals/main
implement php http client generate.
- Loading branch information
Showing
30 changed files
with
768 additions
and
1,465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea | ||
.idea | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package generate | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/zeromicro/go-zero/tools/goctl/api/spec" | ||
"github.com/zeromicro/goctl-php/template" | ||
"github.com/zeromicro/goctl-php/util" | ||
) | ||
|
||
func genClient(dir string, ns string, api *spec.ApiSpec) error { | ||
data := template.PhpTemplateData{ | ||
Namespace: ns, | ||
} | ||
|
||
if err := template.WriteFile(dir, "ApiBaseClient", template.ApiBaseClient, data); err != nil { | ||
return err | ||
} | ||
if err := template.WriteFile(dir, "ApiException", template.ApiException, data); err != nil { | ||
return err | ||
} | ||
if err := template.WriteFile(dir, "ApiBody", template.ApiBody, data); err != nil { | ||
return err | ||
} | ||
return writeClient(dir, ns, api) | ||
} | ||
|
||
func writeClient(dir string, ns string, api *spec.ApiSpec) error { | ||
name := util.CamelCase(api.Service.Name, true) | ||
|
||
data := template.PhpApiClientTemplateData{ | ||
PhpTemplateData: template.PhpTemplateData{Namespace: ns}, | ||
ClientName: name, | ||
Routes: []template.PhpApiClientRouteTemplateData{}, | ||
} | ||
|
||
for _, g := range api.Service.Groups { | ||
prefix := g.GetAnnotation("prefix") | ||
|
||
// 路由 | ||
for _, r := range g.Routes { | ||
route := template.PhpApiClientRouteTemplateData{ | ||
HttpMethod: strings.ToLower(r.Method), | ||
UrlPath: r.Path, | ||
Prefix: prefix, | ||
ActionPrefix: util.CamelCase(prefix, true), | ||
ActionName: util.CamelCase(r.Path, true), | ||
} | ||
|
||
if r.RequestType != nil { | ||
requestType := util.CamelCase(r.RequestType.Name(), true) | ||
route.RequestType = &requestType | ||
route.RequestHasPathParams = hasTagMembers(r.RequestType, pathTagKey) | ||
route.RequestHasQueryString = hasTagMembers(r.RequestType, formTagKey) | ||
route.RequestHasHeaders = hasTagMembers(r.RequestType, headerTagKey) | ||
route.RequestHasBody = hasTagMembers(r.RequestType, bodyTagKey) | ||
} | ||
|
||
if r.ResponseType != nil { | ||
responseType := util.CamelCase(r.ResponseType.Name(), true) | ||
route.ResponseType = &responseType | ||
|
||
definedType, ok := r.ResponseType.(spec.DefineStruct) | ||
if !ok { | ||
return fmt.Errorf("type %s not supported", responseType) | ||
} | ||
if rh, err := enumResponseSubMessageKey(&definedType, headerTagKey); err != nil { | ||
return err | ||
} else { | ||
route.ResponseHeaders = rh | ||
} | ||
if rb, err := enumResponseSubMessageKey(&definedType, bodyTagKey); err != nil { | ||
return err | ||
} else { | ||
route.ResponseBody = rb | ||
} | ||
} | ||
|
||
data.Routes = append(data.Routes, route) | ||
} | ||
} | ||
|
||
return template.WriteFile(dir, fmt.Sprintf("%sClient", name), template.ApiClient, data) | ||
} | ||
|
||
func enumResponseSubMessageKey(definedType *spec.DefineStruct, tag string) (map[string]string, error) { | ||
// 获取字段 | ||
ms := definedType.GetTagMembers(tag) | ||
if len(ms) <= 0 { | ||
return nil, nil | ||
} | ||
|
||
result := map[string]string{} | ||
|
||
for _, m := range ms { | ||
tags := m.Tags() | ||
k := "" | ||
if len(tags) > 0 { | ||
k = tags[0].Name | ||
} else { | ||
k = m.Name | ||
} | ||
result[util.CamelCase(m.Name, true)] = k | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package generate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gookit/color" | ||
"github.com/zeromicro/go-zero/core/logx" | ||
"github.com/zeromicro/go-zero/tools/goctl/api/parser" | ||
"github.com/zeromicro/go-zero/tools/goctl/plugin" | ||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" | ||
) | ||
|
||
func PhpCommand(p *plugin.Plugin, ns string) error { | ||
api, e := parser.Parse(p.ApiFilePath) | ||
if e != nil { | ||
return e | ||
} | ||
|
||
if err := api.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
logx.Must(pathx.MkdirIfNotExist(p.Dir)) | ||
logx.Must(genMessages(p.Dir, ns, api)) | ||
logx.Must(genClient(p.Dir, ns, api)) | ||
|
||
fmt.Println(color.Green.Render("Done.")) | ||
return nil | ||
} |
Oops, something went wrong.