goctl生成Kotlin代码优化 (#16)

* 修复Kotlin连接失败抛出Exception;添加Kotlin连接超时

* 修复路径参数导致生成的Kotlin函数名带有:问题

* Added HTTP Patch Method

* kotlin-add-patch-support

* format-imports
master
Steven Zack 4 years ago committed by GitHub
parent 054d9b5540
commit 1252bd9cde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -43,6 +43,7 @@ var mapping = map[string]string{
"head": "http.MethodHead", "head": "http.MethodHead",
"post": "http.MethodPost", "post": "http.MethodPost",
"put": "http.MethodPut", "put": "http.MethodPut",
"patch": "http.MethodPatch",
} }
type ( type (

@ -5,12 +5,13 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/iancoleman/strcase"
"github.com/tal-tech/go-zero/tools/goctl/api/util" "github.com/tal-tech/go-zero/tools/goctl/api/util"
) )
var funcsMap = template.FuncMap{ var funcsMap = template.FuncMap{
"lowCamelCase": lowCamelCase, "lowCamelCase": lowCamelCase,
"pathToFuncName": pathToFuncName, "routeToFuncName": routeToFuncName,
"parseType": parseType, "parseType": parseType,
"add": add, "add": add,
"upperCase": upperCase, "upperCase": upperCase,
@ -24,16 +25,16 @@ func lowCamelCase(s string) string {
return util.ToLower(s[:1]) + s[1:] return util.ToLower(s[:1]) + s[1:]
} }
func pathToFuncName(path string) string { func routeToFuncName(method, path string) string {
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
} }
path = strings.ReplaceAll(path, "/", "_") path = strings.ReplaceAll(path, "/", "_")
path = strings.ReplaceAll(path, "-", "_") path = strings.ReplaceAll(path, "-", "_")
path = strings.ReplaceAll(path, ":", "With_")
camel := util.ToCamelCase(path) return strings.ToLower(method)+strcase.ToCamel(path)
return util.ToLower(camel[:1]) + camel[1:]
} }
func parseType(t string) string { func parseType(t string) string {

@ -34,9 +34,10 @@ suspend fun apiRequest(
) = withContext(Dispatchers.IO) { ) = withContext(Dispatchers.IO) {
val url = URL(SERVER + uri) val url = URL(SERVER + uri)
with(url.openConnection() as HttpURLConnection) { with(url.openConnection() as HttpURLConnection) {
connectTimeout = 3000
requestMethod = method requestMethod = method
doInput = true doInput = true
if (method == "POST" || method == "PUT") { if (method == "POST" || method == "PUT" || method == "PATCH") {
setRequestProperty("Content-Type", "application/json") setRequestProperty("Content-Type", "application/json")
doOutput = true doOutput = true
val data = when (body) { val data = when (body) {
@ -51,6 +52,8 @@ suspend fun apiRequest(
wr.write(data) wr.write(data)
wr.flush() wr.flush()
} }
try {
if (responseCode >= 400) { if (responseCode >= 400) {
BufferedReader(InputStreamReader(errorStream)).use { BufferedReader(InputStreamReader(errorStream)).use {
val response = it.readText() val response = it.readText()
@ -63,6 +66,9 @@ suspend fun apiRequest(
val response = it.readText() val response = it.readText()
onOk?.invoke(response) onOk?.invoke(response)
} }
} catch (e: Exception) {
e.message?.let { onFail?.invoke(it) }
}
} }
eventually?.invoke() eventually?.invoke()
} }
@ -77,7 +83,7 @@ object {{with .Info}}{{.Title}}{{end}}{
val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}} val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}}
){{end}} ){{end}}
{{with .Service}} {{with .Service}}
{{range .Routes}}suspend fun {{pathToFuncName .Path}}({{with .RequestType}}{{if ne .Name ""}} {{range .Routes}}suspend fun {{routeToFuncName .Method .Path}}({{with .RequestType}}{{if ne .Name ""}}
req:{{.Name}},{{end}}{{end}} req:{{.Name}},{{end}}{{end}}
onOk: (({{with .ResponseType}}{{.Name}}{{end}}) -> Unit)? = null, onOk: (({{with .ResponseType}}{{.Name}}{{end}}) -> Unit)? = null,
onFail: ((String) -> Unit)? = null, onFail: ((String) -> Unit)? = null,

Loading…
Cancel
Save