clear and update
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -92,11 +93,11 @@ func main() {
|
||||
|
||||
res := toMicronautKotlin(config)
|
||||
|
||||
if err := os.WriteFile("models.kt", []byte(res["models"]), 0600); err != nil {
|
||||
if err := os.WriteFile("res/models.kt", []byte(res["models"]), 0600); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile("client.kt", []byte(res["client"]), 0600); err != nil {
|
||||
if err := os.WriteFile("res/client.kt", []byte(res["client"]), 0600); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -122,11 +123,23 @@ func toMicronautKotlin(config *config) map[string]string {
|
||||
sbClient.WriteString("\n")
|
||||
sbClient.WriteString("@Client(value = \"INPUT URL\")\n")
|
||||
sbClient.WriteString("interface GDSClient {\n\n")
|
||||
for path, methods := range config.Paths {
|
||||
for method, v := range methods {
|
||||
sbClient.WriteString(fmt.Sprintf("\t@%s(value = \"%s\")\n", toMicronautKotlinMethod(method), path))
|
||||
sbClient.WriteString(fmt.Sprintf("\tfun %s(\n", v.OperationID))
|
||||
|
||||
pathKeys := getSortKeys(config.Paths)
|
||||
for _, path := range pathKeys {
|
||||
methods := config.Paths[path]
|
||||
methodKeys := getSortKeys(methods)
|
||||
for _, method := range methodKeys {
|
||||
v := methods[method]
|
||||
|
||||
if v.Summary != "" {
|
||||
sbClient.WriteString(fmt.Sprintf("\t// %s\n", v.Summary))
|
||||
}
|
||||
sbClient.WriteString(fmt.Sprintf("\t@%s(value = \"%s\")\n", toMicronautKotlinMethod(method), path))
|
||||
sbClient.WriteString(fmt.Sprintf("\tfun %s(", v.OperationID))
|
||||
|
||||
if len(v.Parameters) != 0 || v.RequestBody != nil {
|
||||
sbClient.WriteString("\n")
|
||||
}
|
||||
for _, p := range v.Parameters {
|
||||
sbClient.WriteString("\t\t")
|
||||
switch p.In {
|
||||
@@ -139,12 +152,20 @@ func toMicronautKotlin(config *config) map[string]string {
|
||||
}
|
||||
sbClient.WriteString(fmt.Sprintf(" %s: %s,\n", p.Name, p.Schema.toKt()))
|
||||
}
|
||||
if v.RequestBody != nil {
|
||||
sbClient.WriteString(fmt.Sprintf("\t\t@Body %s: %s,\n", "body", v.RequestBody.Content.ApplicationJSON.Schema.toKt()))
|
||||
}
|
||||
if len(v.Parameters) != 0 || v.RequestBody != nil {
|
||||
sbClient.WriteString("\t")
|
||||
}
|
||||
|
||||
// todo body and test
|
||||
|
||||
sbClient.WriteString("\t")
|
||||
sbClient.WriteString(fmt.Sprintf("): HttpResponse<%s>\n", v.Responses.Num200.Content.ApplicationJSON.Schema.toKt()))
|
||||
sbClient.WriteString("\n")
|
||||
sbClient.WriteString(")")
|
||||
|
||||
resp := v.Responses.Num200.Content.ApplicationJSON.Schema.toKt()
|
||||
if resp != "" {
|
||||
sbClient.WriteString(fmt.Sprintf(": HttpResponse<%s>", resp))
|
||||
}
|
||||
sbClient.WriteString("\n\n")
|
||||
}
|
||||
}
|
||||
sbClient.WriteString("}\n\n")
|
||||
@@ -189,3 +210,12 @@ func refToName(ref string) string {
|
||||
arr := strings.Split(ref, "/")
|
||||
return arr[len(arr)-1]
|
||||
}
|
||||
|
||||
func getSortKeys[T any](m map[string]T) []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user