From 1252bd9cde4ee556490378b587584e60988a428e Mon Sep 17 00:00:00 2001 From: Steven Zack Date: Tue, 18 Aug 2020 21:49:31 +0800 Subject: [PATCH] =?UTF-8?q?goctl=E7=94=9F=E6=88=90Kotlin=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20(#16)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复Kotlin连接失败抛出Exception;添加Kotlin连接超时 * 修复路径参数导致生成的Kotlin函数名带有:问题 * Added HTTP Patch Method * kotlin-add-patch-support * format-imports --- tools/goctl/api/gogen/genroutes.go | 1 + tools/goctl/api/ktgen/funcs.go | 17 +++++++++-------- tools/goctl/api/ktgen/gen.go | 30 ++++++++++++++++++------------ 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index b52f2e06..a643dc4f 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -43,6 +43,7 @@ var mapping = map[string]string{ "head": "http.MethodHead", "post": "http.MethodPost", "put": "http.MethodPut", + "patch": "http.MethodPatch", } type ( diff --git a/tools/goctl/api/ktgen/funcs.go b/tools/goctl/api/ktgen/funcs.go index 84060dd3..d24642c1 100644 --- a/tools/goctl/api/ktgen/funcs.go +++ b/tools/goctl/api/ktgen/funcs.go @@ -5,15 +5,16 @@ import ( "strings" "text/template" + "github.com/iancoleman/strcase" "github.com/tal-tech/go-zero/tools/goctl/api/util" ) var funcsMap = template.FuncMap{ - "lowCamelCase": lowCamelCase, - "pathToFuncName": pathToFuncName, - "parseType": parseType, - "add": add, - "upperCase": upperCase, + "lowCamelCase": lowCamelCase, + "routeToFuncName": routeToFuncName, + "parseType": parseType, + "add": add, + "upperCase": upperCase, } func lowCamelCase(s string) string { @@ -24,16 +25,16 @@ func lowCamelCase(s string) string { return util.ToLower(s[:1]) + s[1:] } -func pathToFuncName(path string) string { +func routeToFuncName(method, path string) string { if !strings.HasPrefix(path, "/") { path = "/" + path } path = strings.ReplaceAll(path, "/", "_") path = strings.ReplaceAll(path, "-", "_") + path = strings.ReplaceAll(path, ":", "With_") - camel := util.ToCamelCase(path) - return util.ToLower(camel[:1]) + camel[1:] + return strings.ToLower(method)+strcase.ToCamel(path) } func parseType(t string) string { diff --git a/tools/goctl/api/ktgen/gen.go b/tools/goctl/api/ktgen/gen.go index 8e4d2f19..40d08543 100644 --- a/tools/goctl/api/ktgen/gen.go +++ b/tools/goctl/api/ktgen/gen.go @@ -34,9 +34,10 @@ suspend fun apiRequest( ) = withContext(Dispatchers.IO) { val url = URL(SERVER + uri) with(url.openConnection() as HttpURLConnection) { + connectTimeout = 3000 requestMethod = method doInput = true - if (method == "POST" || method == "PUT") { + if (method == "POST" || method == "PUT" || method == "PATCH") { setRequestProperty("Content-Type", "application/json") doOutput = true val data = when (body) { @@ -51,17 +52,22 @@ suspend fun apiRequest( wr.write(data) wr.flush() } - if (responseCode >= 400) { - BufferedReader(InputStreamReader(errorStream)).use { - val response = it.readText() - onFail?.invoke(response) + + try { + if (responseCode >= 400) { + BufferedReader(InputStreamReader(errorStream)).use { + val response = it.readText() + onFail?.invoke(response) + } + return@with } - return@with - } - //response - BufferedReader(InputStreamReader(inputStream)).use { - val response = it.readText() - onOk?.invoke(response) + //response + BufferedReader(InputStreamReader(inputStream)).use { + val response = it.readText() + onOk?.invoke(response) + } + } catch (e: Exception) { + e.message?.let { onFail?.invoke(it) } } } 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}} ){{end}} {{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}} onOk: (({{with .ResponseType}}{{.Name}}{{end}}) -> Unit)? = null, onFail: ((String) -> Unit)? = null,