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/gencomponents.go

133 lines
3.0 KiB
Go

4 years ago
package javagen
import (
"errors"
4 years ago
"fmt"
"io"
"path"
"strings"
"text/template"
"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 (
componentTemplate = `// Code generated by goctl. DO NOT EDIT.
4 years ago
package com.xhb.logic.http.packet.{{.packet}}.model;
import com.xhb.logic.http.DeProguardable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
4 years ago
{{.componentType}}
`
)
func genComponents(dir, packetName string, api *spec.ApiSpec) error {
types := apiutil.GetSharedTypes(api)
if len(types) == 0 {
return nil
}
for _, ty := range types {
if err := createComponent(dir, packetName, ty, api.Types); err != nil {
4 years ago
return err
}
}
return nil
}
func createComponent(dir, packetName string, ty spec.Type, types []spec.Type) error {
4 years ago
modelFile := util.Title(ty.Name) + ".java"
filename := path.Join(dir, modelDir, modelFile)
if err := util.RemoveOrQuit(filename); err != nil {
return err
}
fp, created, err := apiutil.MaybeCreateFile(dir, modelDir, modelFile)
if err != nil {
return err
}
if !created {
return nil
}
defer fp.Close()
tys, err := buildType(ty, types)
4 years ago
if err != nil {
return err
}
t := template.Must(template.New("componentType").Parse(componentTemplate))
return t.Execute(fp, map[string]string{
"componentType": tys,
"packet": packetName,
})
}
func buildType(ty spec.Type, types []spec.Type) (string, error) {
4 years ago
var builder strings.Builder
if err := writeType(&builder, ty, types); err != nil {
4 years ago
return "", apiutil.WrapErr(err, "Type "+ty.Name+" generate error")
}
return builder.String(), nil
}
func writeType(writer io.Writer, tp spec.Type, types []spec.Type) error {
4 years ago
fmt.Fprintf(writer, "public class %s implements DeProguardable {\n", util.Title(tp.Name))
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.IsInline {
_, err := member.GetPropertyName()
if err != nil {
return err
}
}
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)
4 years ago
}
}
return nil
}