fix inner type generate error (#377)

* fix point type bug

* optimized

* fix inner type error
This commit is contained in:
kingxt
2021-01-13 11:54:53 +08:00
committed by GitHub
parent cf3a1020b0
commit 9cd2015661
8 changed files with 92 additions and 54 deletions

View File

@@ -1,10 +1,8 @@
package javagen
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
"text/template"
@@ -20,6 +18,7 @@ import com.xhb.core.packet.HttpPacket;
import com.xhb.core.network.HttpRequestClient;
{{.imports}}
{{.doc}}
public class {{.packetName}} extends HttpPacket<{{.responseType}}> {
{{.paramsDeclaration}}
@@ -101,14 +100,28 @@ func createWith(dir string, api *spec.ApiSpec, route spec.Route, packetName stri
"requestType": util.Title(route.RequestTypeName()),
"HasRequestBody": hasRequestBody,
"imports": imports,
"doc": doc(route),
})
if err != nil {
return err
}
formatFile(&tmplBytes, fp)
_, err = fp.WriteString(formatSource(tmplBytes.String()))
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 {
var builder strings.Builder
allTypes := api.Types
@@ -118,24 +131,6 @@ func getImports(api *spec.ApiSpec, packetName string) string {
return builder.String()
}
func formatFile(tmplBytes *bytes.Buffer, file *os.File) {
scanner := bufio.NewScanner(tmplBytes)
builder := bufio.NewWriter(file)
defer builder.Flush()
preIsBreakLine := false
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if text == "" && preIsBreakLine {
continue
}
preIsBreakLine = text == ""
builder.WriteString(scanner.Text() + "\n")
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
}
func paramsSet(route spec.Route) string {
path := route.Path
cops := strings.Split(path, "/")