gen.go 4.2 KB

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