feat: use go:embed to embed templates (#1756)
This commit is contained in:
22
tools/goctl/api/ktgen/api.tpl
Normal file
22
tools/goctl/api/ktgen/api.tpl
Normal file
@@ -0,0 +1,22 @@
|
||||
package {{with .Info}}{{.Desc}}{{end}}
|
||||
|
||||
import com.google.gson.Gson
|
||||
|
||||
object {{with .Info}}{{.Title}}{{end}}{
|
||||
{{range .Types}}
|
||||
data class {{.Name}}({{$length := (len .Members)}}{{range $i,$item := .Members}}
|
||||
val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type.Name}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}}
|
||||
){{end}}
|
||||
{{with .Service}}
|
||||
{{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,
|
||||
eventually: (() -> Unit)? = null
|
||||
){
|
||||
apiRequest("{{upperCase .Method}}","{{.Path}}",{{with .RequestType}}{{if ne .Name ""}}body=req,{{end}}{{end}} onOk = { {{with .ResponseType}}
|
||||
onOk?.invoke({{if ne .Name ""}}Gson().fromJson(it,{{.Name}}::class.java){{end}}){{end}}
|
||||
}, onFail = onFail, eventually =eventually)
|
||||
}
|
||||
{{end}}{{end}}
|
||||
}
|
||||
61
tools/goctl/api/ktgen/apibase.tpl
Normal file
61
tools/goctl/api/ktgen/apibase.tpl
Normal file
@@ -0,0 +1,61 @@
|
||||
package {{.}}
|
||||
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.OutputStreamWriter
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
const val SERVER = "http://localhost:8080"
|
||||
|
||||
suspend fun apiRequest(
|
||||
method: String,
|
||||
uri: String,
|
||||
body: Any = "",
|
||||
onOk: ((String) -> Unit)? = null,
|
||||
onFail: ((String) -> Unit)? = null,
|
||||
eventually: (() -> Unit)? = null
|
||||
) = withContext(Dispatchers.IO) {
|
||||
val url = URL(SERVER + uri)
|
||||
with(url.openConnection() as HttpURLConnection) {
|
||||
connectTimeout = 3000
|
||||
requestMethod = method
|
||||
doInput = true
|
||||
if (method == "POST" || method == "PUT" || method == "PATCH") {
|
||||
setRequestProperty("Content-Type", "application/json")
|
||||
doOutput = true
|
||||
val data = when (body) {
|
||||
is String -> {
|
||||
body
|
||||
}
|
||||
else -> {
|
||||
Gson().toJson(body)
|
||||
}
|
||||
}
|
||||
val wr = OutputStreamWriter(outputStream)
|
||||
wr.write(data)
|
||||
wr.flush()
|
||||
}
|
||||
|
||||
try {
|
||||
if (responseCode >= 400) {
|
||||
BufferedReader(InputStreamReader(errorStream)).use {
|
||||
val response = it.readText()
|
||||
onFail?.invoke(response)
|
||||
}
|
||||
return@with
|
||||
}
|
||||
//response
|
||||
BufferedReader(InputStreamReader(inputStream)).use {
|
||||
val response = it.readText()
|
||||
onOk?.invoke(response)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.message?.let { onFail?.invoke(it) }
|
||||
}
|
||||
}
|
||||
eventually?.invoke()
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
||||
)
|
||||
|
||||
// KtCommand the generate kotlin code command entrance
|
||||
// KtCommand generates kotlin code command entrance
|
||||
func KtCommand(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
if apiFile == "" {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ktgen
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -10,91 +11,11 @@ import (
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
)
|
||||
|
||||
const (
|
||||
apiBaseTemplate = `package {{.}}
|
||||
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.OutputStreamWriter
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
const val SERVER = "http://localhost:8080"
|
||||
|
||||
suspend fun apiRequest(
|
||||
method: String,
|
||||
uri: String,
|
||||
body: Any = "",
|
||||
onOk: ((String) -> Unit)? = null,
|
||||
onFail: ((String) -> Unit)? = null,
|
||||
eventually: (() -> Unit)? = null
|
||||
) = withContext(Dispatchers.IO) {
|
||||
val url = URL(SERVER + uri)
|
||||
with(url.openConnection() as HttpURLConnection) {
|
||||
connectTimeout = 3000
|
||||
requestMethod = method
|
||||
doInput = true
|
||||
if (method == "POST" || method == "PUT" || method == "PATCH") {
|
||||
setRequestProperty("Content-Type", "application/json")
|
||||
doOutput = true
|
||||
val data = when (body) {
|
||||
is String -> {
|
||||
body
|
||||
}
|
||||
else -> {
|
||||
Gson().toJson(body)
|
||||
}
|
||||
}
|
||||
val wr = OutputStreamWriter(outputStream)
|
||||
wr.write(data)
|
||||
wr.flush()
|
||||
}
|
||||
|
||||
try {
|
||||
if (responseCode >= 400) {
|
||||
BufferedReader(InputStreamReader(errorStream)).use {
|
||||
val response = it.readText()
|
||||
onFail?.invoke(response)
|
||||
}
|
||||
return@with
|
||||
}
|
||||
//response
|
||||
BufferedReader(InputStreamReader(inputStream)).use {
|
||||
val response = it.readText()
|
||||
onOk?.invoke(response)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.message?.let { onFail?.invoke(it) }
|
||||
}
|
||||
}
|
||||
eventually?.invoke()
|
||||
}
|
||||
`
|
||||
apiTemplate = `package {{with .Info}}{{.Desc}}{{end}}
|
||||
|
||||
import com.google.gson.Gson
|
||||
|
||||
object {{with .Info}}{{.Title}}{{end}}{
|
||||
{{range .Types}}
|
||||
data class {{.Name}}({{$length := (len .Members)}}{{range $i,$item := .Members}}
|
||||
val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type.Name}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}}
|
||||
){{end}}
|
||||
{{with .Service}}
|
||||
{{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,
|
||||
eventually: (() -> Unit)? = null
|
||||
){
|
||||
apiRequest("{{upperCase .Method}}","{{.Path}}",{{with .RequestType}}{{if ne .Name ""}}body=req,{{end}}{{end}} onOk = { {{with .ResponseType}}
|
||||
onOk?.invoke({{if ne .Name ""}}Gson().fromJson(it,{{.Name}}::class.java){{end}}){{end}}
|
||||
}, onFail = onFail, eventually =eventually)
|
||||
}
|
||||
{{end}}{{end}}
|
||||
}`
|
||||
var (
|
||||
//go:embed apibase.tpl
|
||||
apiBaseTemplate string
|
||||
//go:embed api.tpl
|
||||
apiTemplate string
|
||||
)
|
||||
|
||||
func genBase(dir, pkg string, api *spec.ApiSpec) error {
|
||||
|
||||
Reference in New Issue
Block a user