You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/javagen/genpacket.go

246 lines
6.1 KiB
Go

4 years ago
package javagen
import (
"bytes"
"fmt"
"strings"
"text/template"
"github.com/tal-tech/go-zero/core/stringx"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/util"
4 years ago
)
const packetTemplate = `package com.xhb.logic.http.packet.{{.packet}};
import com.xhb.core.packet.HttpPacket;
4 years ago
import com.xhb.core.network.HttpRequestClient;
{{.imports}}
4 years ago
{{.doc}}
public class {{.packetName}} extends HttpPacket<{{.responseType}}> {
4 years ago
{{.paramsDeclaration}}
public {{.packetName}}({{.params}}{{if .HasRequestBody}}{{.requestType}} request{{end}}) {
{{if .HasRequestBody}}super(request);{{else}}super(EmptyRequest.instance);{{end}}
{{if .HasRequestBody}}this.request = request;{{end}}{{.paramsSetter}}
4 years ago
}
@Override
public HttpRequestClient.Method requestMethod() {
return HttpRequestClient.Method.{{.method}};
}
@Override
public String requestUri() {
return {{.uri}};
}
}
`
func genPacket(dir, packetName string, api *spec.ApiSpec) error {
for _, route := range api.Service.Routes() {
4 years ago
if err := createWith(dir, api, route, packetName); err != nil {
return err
}
}
return nil
}
func createWith(dir string, api *spec.ApiSpec, route spec.Route, packetName string) error {
packet := route.Handler
4 years ago
packet = strings.Replace(packet, "Handler", "Packet", 1)
packet = strings.Title(packet)
if !strings.HasSuffix(packet, "Packet") {
packet += "Packet"
4 years ago
}
javaFile := packet + ".java"
fp, created, err := apiutil.MaybeCreateFile(dir, "", javaFile)
if err != nil {
return err
}
if !created {
return nil
}
defer fp.Close()
var hasRequestBody = false
if route.RequestType != nil {
if defineStruct, ok := route.RequestType.(spec.DefineStruct); ok {
hasRequestBody = len(defineStruct.GetBodyMembers()) > 0 || len(defineStruct.GetFormMembers()) > 0
4 years ago
}
}
params := strings.TrimSpace(paramsForRoute(route))
if len(params) > 0 && hasRequestBody {
params += ", "
}
4 years ago
paramsDeclaration := declarationForRoute(route)
paramsSetter := paramsSet(route)
imports := getImports(api, packetName)
if len(route.ResponseTypeName()) == 0 {
imports += fmt.Sprintf("\v%s", "import com.xhb.core.response.EmptyResponse;")
}
4 years ago
t := template.Must(template.New("packetTemplate").Parse(packetTemplate))
var tmplBytes bytes.Buffer
err = t.Execute(&tmplBytes, map[string]interface{}{
4 years ago
"packetName": packet,
"method": strings.ToUpper(route.Method),
"uri": processUri(route),
"responseType": stringx.TakeOne(util.Title(route.ResponseTypeName()), "EmptyResponse"),
4 years ago
"params": params,
"paramsDeclaration": strings.TrimSpace(paramsDeclaration),
"paramsSetter": paramsSetter,
4 years ago
"packet": packetName,
"requestType": util.Title(route.RequestTypeName()),
"HasRequestBody": hasRequestBody,
"imports": imports,
"doc": doc(route),
4 years ago
})
if err != nil {
return err
}
_, err = fp.WriteString(formatSource(tmplBytes.String()))
4 years ago
return nil
}
func doc(route spec.Route) string {
comment := route.JoinedDoc()
if len(comment) > 0 {
formatter := `
/*
%s
*/`
return fmt.Sprintf(formatter, comment)
}
return ""
}
func getImports(api *spec.ApiSpec, packetName string) string {
4 years ago
var builder strings.Builder
allTypes := api.Types
if len(allTypes) > 0 {
fmt.Fprintf(&builder, "import com.xhb.logic.http.packet.%s.model.*;\n", packetName)
4 years ago
}
4 years ago
4 years ago
return builder.String()
}
func paramsSet(route spec.Route) string {
path := route.Path
cops := strings.Split(path, "/")
var builder strings.Builder
for _, cop := range cops {
if len(cop) == 0 {
continue
}
if strings.HasPrefix(cop, ":") {
param := cop[1:]
builder.WriteString("\n")
builder.WriteString(fmt.Sprintf("\t\tthis.%s = %s;", param, param))
}
}
result := builder.String()
return result
}
func paramsForRoute(route spec.Route) string {
path := route.Path
cops := strings.Split(path, "/")
var builder strings.Builder
for _, cop := range cops {
if len(cop) == 0 {
continue
}
if strings.HasPrefix(cop, ":") {
builder.WriteString(fmt.Sprintf("String %s, ", cop[1:]))
}
}
return strings.TrimSuffix(builder.String(), ", ")
4 years ago
}
func declarationForRoute(route spec.Route) string {
path := route.Path
cops := strings.Split(path, "/")
var builder strings.Builder
writeIndent(&builder, 1)
for _, cop := range cops {
if len(cop) == 0 {
continue
}
if strings.HasPrefix(cop, ":") {
writeIndent(&builder, 1)
builder.WriteString(fmt.Sprintf("private String %s;\n", cop[1:]))
}
}
result := strings.TrimSpace(builder.String())
if len(result) > 0 {
result = "\n" + result
}
return result
}
func processUri(route spec.Route) string {
path := route.Path
4 years ago
var builder strings.Builder
cops := strings.Split(path, "/")
for index, cop := range cops {
if len(cop) == 0 {
continue
}
if strings.HasPrefix(cop, ":") {
builder.WriteString("/\" + " + cop[1:] + " + \"")
} else {
builder.WriteString("/" + cop)
if index == len(cops)-1 {
builder.WriteString("\"")
}
}
}
result := builder.String()
if strings.HasSuffix(result, " + \"") {
result = result[:len(result)-4]
}
if strings.HasPrefix(result, "/") {
result = strings.TrimPrefix(result, "/")
4 years ago
result = "\"" + result
}
return result + formString(route)
4 years ago
}
func formString(route spec.Route) string {
var keyValues []string
if defineStruct, ok := route.RequestType.(spec.DefineStruct); ok {
forms := defineStruct.GetFormMembers()
for _, item := range forms {
name, err := item.GetPropertyName()
if err != nil {
panic(err)
}
strcat := "?"
if len(keyValues) > 0 {
strcat = "&"
}
if item.Type.Name() == "bool" {
name = strings.TrimPrefix(name, "Is")
name = strings.TrimPrefix(name, "is")
keyValues = append(keyValues, fmt.Sprintf(`"%s%s=" + request.is%s()`, strcat, name, strings.Title(name)))
} else {
keyValues = append(keyValues, fmt.Sprintf(`"%s%s=" + request.get%s()`, strcat, name, strings.Title(name)))
}
}
if len(keyValues) > 0 {
return " + " + strings.Join(keyValues, " + ")
}
4 years ago
}
return ""
4 years ago
}