|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package javagen
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"path"
|
|
|
|
@ -17,6 +18,8 @@ const (
|
|
|
|
|
package com.xhb.logic.http.packet.{{.packet}}.model;
|
|
|
|
|
|
|
|
|
|
import com.xhb.logic.http.DeProguardable;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
|
|
{{.componentType}}
|
|
|
|
|
`
|
|
|
|
@ -28,7 +31,7 @@ func genComponents(dir, packetName string, api *spec.ApiSpec) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
for _, ty := range types {
|
|
|
|
|
if err := createComponent(dir, packetName, ty); err != nil {
|
|
|
|
|
if err := createComponent(dir, packetName, ty, api.Types); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -36,7 +39,7 @@ func genComponents(dir, packetName string, api *spec.ApiSpec) error {
|
|
|
|
|
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"
|
|
|
|
|
filename := path.Join(dir, modelDir, modelFile)
|
|
|
|
|
if err := util.RemoveOrQuit(filename); err != nil {
|
|
|
|
@ -52,7 +55,7 @@ func createComponent(dir, packetName string, ty spec.Type) error {
|
|
|
|
|
}
|
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
|
|
tys, err := buildType(ty)
|
|
|
|
|
tys, err := buildType(ty, types)
|
|
|
|
|
if err != nil {
|
|
|
|
|
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
|
|
|
|
|
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 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))
|
|
|
|
|
for _, member := range tp.Members {
|
|
|
|
|
if err := writeProperty(writer, member, 1); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
var members []spec.Member
|
|
|
|
|
err := writeMembers(writer, types, tp.Members, &members, 1)
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|