-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.go
61 lines (55 loc) · 2.29 KB
/
responses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package client
import (
"fmt"
"github.com/specgen-io/specgen/v2/goven/generator"
"github.com/specgen-io/specgen/v2/goven/kotlin/packages"
"github.com/specgen-io/specgen/v2/goven/kotlin/types"
"github.com/specgen-io/specgen/v2/goven/kotlin/writer"
"github.com/specgen-io/specgen/v2/goven/spec"
)
func responseCreate(response *spec.OperationResponse, resultVar string) string {
if len(response.Operation.Responses.Success()) > 1 {
return fmt.Sprintf(`return %s.%s(%s)`, responseInterfaceName(response.Operation), response.Name.PascalCase(), resultVar)
} else {
if resultVar != "" {
return fmt.Sprintf(`return %s`, resultVar)
} else {
return `return`
}
}
}
func responses(api *spec.Api, types *types.Types, apiPackage packages.Package, modelsVersionPackage packages.Package, errorModelsPackage packages.Package) []generator.CodeFile {
files := []generator.CodeFile{}
for _, operation := range api.Operations {
if len(operation.Responses.Success()) > 1 {
files = append(files, *responseInterface(types, &operation, apiPackage, modelsVersionPackage, errorModelsPackage))
}
}
return files
}
func responseInterface(types *types.Types, operation *spec.NamedOperation, apiPackage packages.Package, modelsVersionPackage packages.Package, errorModelsPackage packages.Package) *generator.CodeFile {
w := writer.New(apiPackage, responseInterfaceName(operation))
w.Line(`import %s`, modelsVersionPackage.PackageStar)
w.Line(`import %s`, errorModelsPackage.PackageStar)
w.EmptyLine()
w.Line(`interface [[.ClassName]] {`)
for index, response := range operation.Responses.Success() {
if index > 0 {
w.EmptyLine()
}
responseImpl(w.Indented(), types, response)
}
w.Line(`}`)
return w.ToCodeFile()
}
func responseImpl(w *writer.Writer, types *types.Types, response *spec.OperationResponse) {
serviceResponseImplementationName := response.Name.PascalCase()
if !response.Body.IsEmpty() {
w.Line(`class %s(var body: %s) : %s`, serviceResponseImplementationName, types.Kotlin(&response.Body.Type.Definition), responseInterfaceName(response.Operation))
} else {
w.Line(`class %s : %s`, serviceResponseImplementationName, responseInterfaceName(response.Operation))
}
}
func responseInterfaceName(operation *spec.NamedOperation) string {
return fmt.Sprintf(`%sResponse`, operation.Name.PascalCase())
}