* add g4 file

* new define api by g4

* reactor parser to g4gen

* add syntax parser & test

* add syntax parser & test

* add syntax parser & test

* update g4 file

* add import parse & test

* ractor AT lexer

* panic with error

* revert AT

* update g4 file

* update g4 file

* update g4 file

* optimize parser

* update g4 file

* parse info

* optimized java generator

* revert

* optimize java generator

* update java generator

* update java generator

* update java generator

* update java generator

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
master
kingxt 4 years ago committed by GitHub
parent 8d2e2753a2
commit 25cab2f273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
package javagen package javagen
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"path" "path"
@ -17,6 +18,8 @@ const (
package com.xhb.logic.http.packet.{{.packet}}.model; package com.xhb.logic.http.packet.{{.packet}}.model;
import com.xhb.logic.http.DeProguardable; import com.xhb.logic.http.DeProguardable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
{{.componentType}} {{.componentType}}
` `
@ -28,7 +31,7 @@ func genComponents(dir, packetName string, api *spec.ApiSpec) error {
return nil return nil
} }
for _, ty := range types { for _, ty := range types {
if err := createComponent(dir, packetName, ty); err != nil { if err := createComponent(dir, packetName, ty, api.Types); err != nil {
return err return err
} }
} }
@ -36,7 +39,7 @@ func genComponents(dir, packetName string, api *spec.ApiSpec) error {
return nil return nil
} }
func createComponent(dir, packetName string, ty spec.Type) error { func createComponent(dir, packetName string, ty spec.Type, types []spec.Type) error {
modelFile := util.Title(ty.Name) + ".java" modelFile := util.Title(ty.Name) + ".java"
filename := path.Join(dir, modelDir, modelFile) filename := path.Join(dir, modelDir, modelFile)
if err := util.RemoveOrQuit(filename); err != nil { if err := util.RemoveOrQuit(filename); err != nil {
@ -52,7 +55,7 @@ func createComponent(dir, packetName string, ty spec.Type) error {
} }
defer fp.Close() defer fp.Close()
tys, err := buildType(ty) tys, err := buildType(ty, types)
if err != nil { if err != nil {
return err return err
} }
@ -64,22 +67,60 @@ func createComponent(dir, packetName string, ty spec.Type) error {
}) })
} }
func buildType(ty spec.Type) (string, error) { func buildType(ty spec.Type, types []spec.Type) (string, error) {
var builder strings.Builder var builder strings.Builder
if err := writeType(&builder, ty); err != nil { if err := writeType(&builder, ty, types); err != nil {
return "", apiutil.WrapErr(err, "Type "+ty.Name+" generate error") return "", apiutil.WrapErr(err, "Type "+ty.Name+" generate error")
} }
return builder.String(), nil return builder.String(), nil
} }
func writeType(writer io.Writer, tp spec.Type) error { func writeType(writer io.Writer, tp spec.Type, types []spec.Type) error {
fmt.Fprintf(writer, "public class %s implements DeProguardable {\n", util.Title(tp.Name)) fmt.Fprintf(writer, "public class %s implements DeProguardable {\n", util.Title(tp.Name))
for _, member := range tp.Members { var members []spec.Member
if err := writeProperty(writer, member, 1); err != nil { err := writeMembers(writer, types, tp.Members, &members, 1)
return err if err != nil {
return err
}
genGetSet(writer, members, 1)
fmt.Fprintf(writer, "}")
return nil
}
func writeMembers(writer io.Writer, types []spec.Type, members []spec.Member, allMembers *[]spec.Member, indent int) error {
for _, member := range members {
if !member.IsBodyMember() {
continue
}
for _, item := range *allMembers {
if item.Name == member.Name {
continue
}
}
if member.IsInline {
hasInline := false
for _, ty := range types {
if strings.ToLower(ty.Name) == strings.ToLower(member.Name) {
err := writeMembers(writer, types, ty.Members, allMembers, indent)
if err != nil {
return err
}
hasInline = true
break
}
}
if !hasInline {
return errors.New("inline type " + member.Name + " not exist, please correct api file")
}
} else {
if err := writeProperty(writer, member, indent); err != nil {
return err
}
*allMembers = append(*allMembers, member)
} }
} }
genGetSet(writer, tp, 1)
fmt.Fprintf(writer, "}\n")
return nil return nil
} }

@ -19,23 +19,27 @@ const packetTemplate = `package com.xhb.logic.http.packet.{{.packet}};
import com.google.gson.Gson; import com.google.gson.Gson;
import com.xhb.commons.JSON; import com.xhb.commons.JSON;
import com.xhb.commons.JsonParser; import com.xhb.commons.JsonMarshal;
import com.xhb.core.network.HttpRequestClient; import com.xhb.core.network.HttpRequestClient;
import com.xhb.core.packet.HttpRequestPacket; import com.xhb.core.packet.HttpRequestPacket;
import com.xhb.core.response.HttpResponseData; import com.xhb.core.response.HttpResponseData;
import com.xhb.logic.http.DeProguardable; import com.xhb.logic.http.DeProguardable;
{{if not .HasRequestBody}}
import com.xhb.logic.http.request.EmptyRequest;
{{end}}
{{.import}} {{.import}}
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject; import org.json.JSONObject;
public class {{.packetName}} extends HttpRequestPacket<{{.packetName}}.{{.packetName}}Response> { public class {{.packetName}} extends HttpRequestPacket<{{.packetName}}.{{.packetName}}Response> {
{{.paramsDeclaration}} {{.paramsDeclaration}}
public {{.packetName}}({{.params}}{{.requestType}} request) { public {{.packetName}}({{.params}}{{if .HasRequestBody}}, {{.requestType}} request{{end}}) {
super(request); {{if .HasRequestBody}}super(request);{{else}}super(EmptyRequest.instance);{{end}}
this.request = request;{{.paramsSet}} {{if .HasRequestBody}}this.request = request;{{end}}{{.paramsSet}}
} }
@Override @Override
@ -113,7 +117,8 @@ func createWith(dir string, api *spec.ApiSpec, route spec.Route, packetName stri
} else { } else {
fmt.Fprintln(&builder) fmt.Fprintln(&builder)
} }
if err := genType(&builder, tp); err != nil {
if err := genType(&builder, tp, api.Types); err != nil {
return err return err
} }
} }
@ -126,7 +131,7 @@ func createWith(dir string, api *spec.ApiSpec, route spec.Route, packetName stri
t := template.Must(template.New("packetTemplate").Parse(packetTemplate)) t := template.Must(template.New("packetTemplate").Parse(packetTemplate))
var tmplBytes bytes.Buffer var tmplBytes bytes.Buffer
err = t.Execute(&tmplBytes, map[string]string{ err = t.Execute(&tmplBytes, map[string]interface{}{
"packetName": packet, "packetName": packet,
"method": strings.ToUpper(route.Method), "method": strings.ToUpper(route.Method),
"uri": processUri(route), "uri": processUri(route),
@ -137,6 +142,7 @@ func createWith(dir string, api *spec.ApiSpec, route spec.Route, packetName stri
"paramsSet": paramsSet, "paramsSet": paramsSet,
"packet": packetName, "packet": packetName,
"requestType": util.Title(route.RequestType.Name), "requestType": util.Title(route.RequestType.Name),
"HasRequestBody": len(route.RequestType.GetBodyMembers()) > 0,
"import": getImports(api, route, packetName), "import": getImports(api, route, packetName),
}) })
if err != nil { if err != nil {
@ -209,7 +215,7 @@ func paramsForRoute(route spec.Route) string {
builder.WriteString(fmt.Sprintf("String %s, ", cop[1:])) builder.WriteString(fmt.Sprintf("String %s, ", cop[1:]))
} }
} }
return builder.String() return strings.TrimSuffix(builder.String(), ", ")
} }
func declarationForRoute(route spec.Route) string { func declarationForRoute(route spec.Route) string {
@ -260,18 +266,22 @@ func processUri(route spec.Route) string {
return result return result
} }
func genType(writer io.Writer, tp spec.Type) error { func genType(writer io.Writer, tp spec.Type, types []spec.Type) error {
if len(tp.GetBodyMembers()) == 0 {
return nil
}
writeIndent(writer, 1) writeIndent(writer, 1)
fmt.Fprintf(writer, "static class %s implements DeProguardable {\n", util.Title(tp.Name)) fmt.Fprintf(writer, "static class %s implements DeProguardable {\n", util.Title(tp.Name))
for _, member := range tp.Members { var members []spec.Member
if err := writeProperty(writer, member, 2); err != nil { err := writeMembers(writer, types, tp.Members, &members, 2)
return err if err != nil {
} return err
} }
writeBreakline(writer) writeNewline(writer)
writeIndent(writer, 1) writeIndent(writer, 1)
genGetSet(writer, tp, 2) genGetSet(writer, members, 2)
writeIndent(writer, 1) writeIndent(writer, 1)
fmt.Fprintln(writer, "}") fmt.Fprintln(writer, "}")

@ -67,8 +67,8 @@ func indentString(indent int) string {
return result return result
} }
func writeBreakline(writer io.Writer) { func writeNewline(writer io.Writer) {
fmt.Fprint(writer, "\n") fmt.Fprint(writer, util.NL)
} }
func isPrimitiveType(tp string) bool { func isPrimitiveType(tp string) bool {
@ -87,6 +87,7 @@ func goTypeToJava(tp string) (string, error) {
if len(tp) == 0 { if len(tp) == 0 {
return "", errors.New("property type empty") return "", errors.New("property type empty")
} }
if strings.HasPrefix(tp, "*") { if strings.HasPrefix(tp, "*") {
tp = tp[1:] tp = tp[1:]
} }
@ -107,39 +108,44 @@ func goTypeToJava(tp string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if len(tys) == 0 { if len(tys) == 0 {
return "", fmt.Errorf("%s tp parse error", tp) return "", fmt.Errorf("%s tp parse error", tp)
} }
return fmt.Sprintf("java.util.ArrayList<%s>", util.Title(tys[0])), nil return fmt.Sprintf("java.util.ArrayList<%s>", util.Title(tys[0])), nil
} else if strings.HasPrefix(tp, "map") { } else if strings.HasPrefix(tp, "map") {
tys, err := apiutil.DecomposeType(tp) tys, err := apiutil.DecomposeType(tp)
if err != nil { if err != nil {
return "", err return "", err
} }
if len(tys) == 2 { if len(tys) == 2 {
return "", fmt.Errorf("%s tp parse error", tp) return "", fmt.Errorf("%s tp parse error", tp)
} }
return fmt.Sprintf("java.util.HashMap<String, %s>", util.Title(tys[1])), nil return fmt.Sprintf("java.util.HashMap<String, %s>", util.Title(tys[1])), nil
} }
return util.Title(tp), nil return util.Title(tp), nil
} }
func genGetSet(writer io.Writer, tp spec.Type, indent int) error { func genGetSet(writer io.Writer, members []spec.Member, indent int) error {
t := template.Must(template.New("getSetTemplate").Parse(getSetTemplate)) t := template.Must(template.New("getSetTemplate").Parse(getSetTemplate))
for _, member := range tp.Members { for _, member := range members {
var tmplBytes bytes.Buffer var tmplBytes bytes.Buffer
oty, err := goTypeToJava(member.Type) oty, err := goTypeToJava(member.Type)
if err != nil { if err != nil {
return err return err
} }
tyString := oty tyString := oty
decorator := "" decorator := ""
if !isPrimitiveType(member.Type) { if !isPrimitiveType(member.Type) {
if member.IsOptional() { if member.IsOptional() {
decorator = "@org.jetbrains.annotations.Nullable " decorator = "@Nullable "
} else { } else {
decorator = "@org.jetbrains.annotations.NotNull " decorator = "@NotNull "
} }
tyString = decorator + tyString tyString = decorator + tyString
} }
@ -155,6 +161,7 @@ func genGetSet(writer io.Writer, tp spec.Type, indent int) error {
if err != nil { if err != nil {
return err return err
} }
r := tmplBytes.String() r := tmplBytes.String()
r = strings.Replace(r, " boolean get", " boolean is", 1) r = strings.Replace(r, " boolean get", " boolean is", 1)
writer.Write([]byte(r)) writer.Write([]byte(r))

Loading…
Cancel
Save