gen.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package ktgen
  2. import (
  3. "log"
  4. "os"
  5. "path/filepath"
  6. "text/template"
  7. "github.com/iancoleman/strcase"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/spec"
  9. )
  10. const (
  11. apiBaseTemplate = `package {{.}}
  12. import com.google.gson.Gson
  13. import kotlinx.coroutines.Dispatchers
  14. import kotlinx.coroutines.withContext
  15. import java.io.BufferedReader
  16. import java.io.InputStreamReader
  17. import java.io.OutputStreamWriter
  18. import java.net.HttpURLConnection
  19. import java.net.URL
  20. const val SERVER = "http://localhost:8080"
  21. suspend fun apiPost(
  22. uri: String,
  23. body: Any,
  24. onOk: ((String) -> Unit)? = null,
  25. onFail: ((String) -> Unit)? = null,
  26. eventually: (() -> Unit)? = null
  27. ) = withContext(Dispatchers.IO) {
  28. val url = URL(SERVER + uri)
  29. with(url.openConnection() as HttpURLConnection) {
  30. requestMethod = "POST"
  31. headerFields["Content-Type"] = listOf("Application/json")
  32. val data = when (body) {
  33. is String -> {
  34. body
  35. }
  36. else -> {
  37. Gson().toJson(body)
  38. }
  39. }
  40. val wr = OutputStreamWriter(outputStream)
  41. wr.write(data)
  42. wr.flush()
  43. //response
  44. BufferedReader(InputStreamReader(inputStream)).use {
  45. val response = it.readText()
  46. if (responseCode == 200) {
  47. onOk?.invoke(response)
  48. } else {
  49. onFail?.invoke(response)
  50. }
  51. }
  52. }
  53. eventually?.invoke()
  54. }
  55. suspend fun apiGet(
  56. uri: String,
  57. onOk: ((String) -> Unit)? = null,
  58. onFail: ((String) -> Unit)? = null,
  59. eventually: (() -> Unit)? = null
  60. ) = withContext(Dispatchers.IO) {
  61. val url = URL(SERVER + uri)
  62. with(url.openConnection() as HttpURLConnection) {
  63. requestMethod = "POST"
  64. headerFields["Content-Type"] = listOf("Application/json")
  65. val wr = OutputStreamWriter(outputStream)
  66. wr.flush()
  67. //response
  68. BufferedReader(InputStreamReader(inputStream)).use {
  69. val response = it.readText()
  70. if (responseCode == 200) {
  71. onOk?.invoke(response)
  72. } else {
  73. onFail?.invoke(response)
  74. }
  75. }
  76. }
  77. eventually?.invoke()
  78. }
  79. `
  80. apiTemplate = `package {{with .Info}}{{.Title}}{{end}}
  81. import com.google.gson.Gson
  82. object Api{
  83. {{range .Types}}
  84. data class {{.Name}}({{$length := (len .Members)}}{{range $i,$item := .Members}}
  85. val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}}
  86. ){{end}}
  87. {{with .Service}}
  88. {{range .Routes}}suspend fun {{pathToFuncName .Path}}({{if ne .Method "get"}}
  89. req:{{with .RequestType}}{{.Name}},{{end}}{{end}}
  90. onOk: (({{with .ResponseType}}{{.Name}}{{end}}) -> Unit)? = null,
  91. onFail: ((String) -> Unit)? = null,
  92. eventually: (() -> Unit)? = null
  93. ){
  94. api{{if eq .Method "get"}}Get{{else}}Post{{end}}("{{.Path}}",{{if ne .Method "get"}}req,{{end}} onOk = {
  95. onOk?.invoke(Gson().fromJson(it,{{with .ResponseType}}{{.Name}}{{end}}::class.java))
  96. }, onFail = onFail, eventually =eventually)
  97. }
  98. {{end}}{{end}}
  99. }`
  100. )
  101. func genBase(dir, pkg string, api *spec.ApiSpec) error {
  102. e := os.MkdirAll(dir, 0755)
  103. if e != nil {
  104. return e
  105. }
  106. path := filepath.Join(dir, "BaseApi.kt")
  107. if _, e := os.Stat(path); e == nil {
  108. return nil
  109. }
  110. file, e := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
  111. if e != nil {
  112. return e
  113. }
  114. defer file.Close()
  115. t, e := template.New("n").Parse(apiBaseTemplate)
  116. if e != nil {
  117. return e
  118. }
  119. e = t.Execute(file, pkg)
  120. if e != nil {
  121. return e
  122. }
  123. return nil
  124. }
  125. func genApi(dir, pkg string, api *spec.ApiSpec) error {
  126. path := filepath.Join(dir, strcase.ToCamel(api.Info.Title+"Api")+".kt")
  127. api.Info.Title = pkg
  128. e := os.MkdirAll(dir, 0755)
  129. if e != nil {
  130. return e
  131. }
  132. file, e := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
  133. if e != nil {
  134. return e
  135. }
  136. defer file.Close()
  137. t, e := template.New("api").Funcs(funcsMap).Parse(apiTemplate)
  138. if e != nil {
  139. log.Fatal(e)
  140. }
  141. e = t.Execute(file, api)
  142. if e != nil {
  143. log.Fatal(e)
  144. }
  145. return nil
  146. }