-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk_generator.go
129 lines (102 loc) · 2.67 KB
/
sdk_generator.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package gots_sdk
import (
"log"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"github.com/tkrajina/typescriptify-golang-structs/typescriptify"
)
type AddSdkFunc func(api *Api)
type ApiSdk struct {
R *gin.Engine
Model *typescriptify.TypeScriptify
toSdk AddSdkFunc
}
func (sdk *ApiSdk) GenerateSdkFunc(fname string) (createSdkJs func()) {
funcscripts := []string{}
sdk.toSdk = func(api *Api) {
query := api.Query
payload := api.Payload
response := api.Response
if query != nil {
sdk.Model.Add(query)
}
if payload != nil {
sdk.Model.Add(payload)
}
if response != nil {
sdk.Model.Add(response)
}
funcscripts = append(funcscripts, api.GenerateTs())
}
return func() {
basepath := filepath.Join(fname)
os.Remove(basepath)
f, err := os.OpenFile(basepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
log.Fatal(err)
}
defer f.Close()
model, _ := sdk.Model.Convert(map[string]string{})
f.WriteString(templateImportHead)
f.WriteString("\n\n")
f.WriteString(model)
f.WriteString("\n")
f.WriteString(templateClassApi)
f.WriteString("\n")
f.WriteString(strings.Join(funcscripts, "\n"))
}
}
type RegisterFunc func(api *Api, handlers ...gin.HandlerFunc) gin.IRoutes
func (sdk *ApiSdk) Register(api *Api, handlers ...gin.HandlerFunc) gin.IRoutes {
sdk.toSdk(api)
return sdk.R.Handle(api.Method, api.RelativePath, handlers...)
}
type SdkGroup struct {
sdk *ApiSdk
G *gin.RouterGroup
Basepath string
}
func (grp *SdkGroup) Register(api *Api, handlers ...gin.HandlerFunc) gin.IRoutes {
api.GroupPath = grp.Basepath
grp.sdk.toSdk(api)
return grp.G.Handle(api.Method, api.RelativePath, handlers...)
}
func (grp *SdkGroup) Group(path string) *SdkGroup {
base, _ := url.JoinPath(grp.Basepath, path)
newGroup := SdkGroup{
sdk: grp.sdk,
G: grp.G.Group(path),
Basepath: base,
}
return &newGroup
}
func (sdk *ApiSdk) Group(relativePath string) *SdkGroup {
newGroup := SdkGroup{
sdk: sdk,
G: sdk.R.Group(relativePath),
Basepath: relativePath,
}
return &newGroup
}
func (sdk *ApiSdk) RegisterGroup(relativePath string, groupHandler func(group *gin.RouterGroup, register RegisterFunc)) {
r := sdk.R.Group(relativePath)
var registfn RegisterFunc = func(api *Api, handlers ...gin.HandlerFunc) gin.IRoutes {
api.GroupPath = relativePath
sdk.toSdk(api)
return r.Handle(api.Method, api.RelativePath, handlers...)
}
groupHandler(r, registfn)
}
func NewApiSdk(r *gin.Engine) *ApiSdk {
sdk := &ApiSdk{
Model: typescriptify.New(),
toSdk: func(api *Api) {},
R: r,
}
sdk.Model.CreateInterface = true
sdk.Model.CreateConstructor = false
return sdk
}