-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
310 lines (246 loc) · 6.22 KB
/
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package go2go
import (
"bytes"
"crypto/sha1"
"crypto/sha256"
_ "embed"
"encoding/hex"
"fmt"
"go/format"
"sort"
"strconv"
"strings"
"text/template"
tstypes "github.com/go-generalize/go2ts/pkg/types"
"golang.org/x/xerrors"
)
type Generator struct {
ExternalGenerator func(tstypes.Type) (*GeneratedType, bool)
types map[string]tstypes.Type
generatorParam
converted map[string]string
prereserved map[string]string
reserved map[string]struct{}
imported map[string]string
}
type GeneratedType struct {
Path string
Name string
}
type objectEntry struct {
Field string
Type string
Tag string
}
type object struct {
Name string
Fields []objectEntry
}
type constantEnum struct {
Name string
Value string
}
type constant struct {
Name string
Base string
Enums []constantEnum
}
type imported struct {
Alias, Path string
}
type generatorParam struct {
Consts []constant
Objects []object
Imported []imported
UseTimePackage bool
}
type metadata struct {
upperStructName string
inlineIndex int
}
func NewGenerator(types map[string]tstypes.Type, prereserved []string) *Generator {
prs := map[string]string{}
for _, p := range prereserved {
_, name := SplitPackegeStruct(p)
prs[name] = p
}
return &Generator{
types: types,
converted: map[string]string{},
reserved: map[string]struct{}{},
prereserved: prs,
imported: map[string]string{},
}
}
func (g *Generator) getImportAlias(path string) string {
cs := sha256.Sum256([]byte(path))
return "external_" + hex.EncodeToString(cs[:])[:7]
}
func (g *Generator) convert(v tstypes.Type, meta *metadata) string {
if g.ExternalGenerator != nil {
if res, ok := g.ExternalGenerator(v); ok {
if res.Path == "" {
return res.Name
}
alias := g.getImportAlias(res.Path)
g.imported[alias] = res.Path
return alias + "." + res.Name
}
}
switch v := v.(type) {
case *tstypes.Array:
return "[]" + g.convert(v.Inner, meta)
case *tstypes.Object:
return g.convertObject(v, meta)
case *tstypes.String:
return g.convertString(v, meta)
case *tstypes.Number:
return g.convertNumber(v, meta)
case *tstypes.Boolean:
return "bool"
case *tstypes.Date:
g.UseTimePackage = true
return "time.Time"
case *tstypes.Nullable:
_, isArray := v.Inner.(*tstypes.Array)
_, isMap := v.Inner.(*tstypes.Map)
if isArray || isMap {
return g.convert(v.Inner, meta)
}
return "*" + g.convert(v.Inner, meta)
case *tstypes.Any:
return "interface{}"
case *tstypes.Map:
return fmt.Sprintf("map[%s]%s", g.convert(v.Key, meta), g.convert(v.Value, meta))
default:
panic("unsupported")
}
}
func (g *Generator) convertString(str *tstypes.String, upper *metadata) string {
if len(str.Enum) == 0 {
return "string"
}
if name, ok := g.converted[str.Name]; ok {
return name
}
name := g.getConvertedType(str.Name, upper)
consts := make([]constantEnum, 0, len(str.RawEnum))
_, orig := SplitPackegeStruct(str.Name)
for _, e := range str.RawEnum {
key := name + e.Key
if strings.HasPrefix(e.Key, orig) {
key = name + strings.TrimPrefix(e.Key, orig)
}
consts = append(consts, constantEnum{
Name: key,
Value: strconv.Quote(e.Value),
})
}
g.Consts = append(g.Consts, constant{
Name: name,
Base: "string",
Enums: consts,
})
return name
}
func (g *Generator) convertNumber(num *tstypes.Number, upper *metadata) string {
if len(num.Enum) == 0 {
return getBasicTypeName(num.RawType)
}
if name, ok := g.converted[num.Name]; ok {
return name
}
name := g.getConvertedType(num.Name, upper)
enums := make([]constantEnum, 0, len(num.RawEnum))
_, orig := SplitPackegeStruct(num.Name)
for _, e := range num.RawEnum {
key := name + e.Key
if strings.HasPrefix(e.Key, orig) {
key = name + strings.TrimPrefix(e.Key, orig)
}
enums = append(enums, constantEnum{
Name: key,
Value: fmt.Sprint(e.Value),
}) // Support multiple types
}
g.Consts = append(g.Consts, constant{
Name: name,
Base: getBasicTypeName(num.RawType),
Enums: enums,
})
return name
}
func (g *Generator) convertObject(obj *tstypes.Object, upper *metadata) string {
var converted object
if name, ok := g.converted[obj.Name]; ok {
return name
}
name := g.getConvertedType(obj.Name, upper)
entries := make([]tstypes.ObjectEntry, 0, len(obj.Entries))
for _, v := range obj.Entries {
entries = append(entries, v)
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].RawName < entries[j].RawName
})
for i, e := range entries {
converted.Fields = append(converted.Fields, objectEntry{
Field: e.RawName,
Type: g.convert(e.Type, &metadata{upperStructName: name, inlineIndex: i}),
Tag: e.RawTag,
})
}
converted.Name = name
g.Objects = append(g.Objects, converted)
return name
}
func (g *Generator) getConvertedType(fullName string, meta *metadata) string {
var name string
if fullName == "" {
name = meta.upperStructName + "Inline" + fmt.Sprintf("%03d", meta.inlineIndex)
} else {
_, name = SplitPackegeStruct(fullName)
prev, prereserved := g.prereserved[fullName]
_, reserved := g.reserved[name]
if (prereserved && prev != fullName) || reserved {
hash := fmt.Sprintf("%x", sha1.Sum([]byte(fullName)))
name = name + "_" + hash[:4]
}
g.reserved[name] = struct{}{}
}
g.converted[fullName] = name
return name
}
//go:embed templates/types.go.tmpl
var templateBase string
func (g *Generator) Generate() (string, error) {
for _, v := range g.types {
g.convert(v, nil)
}
g.Imported = make([]imported, 0, len(g.imported))
for k, v := range g.imported {
g.Imported = append(g.Imported, imported{
Alias: k,
Path: v,
})
}
sort.Slice(g.Imported, func(i, j int) bool {
return g.Imported[i].Path < g.Imported[j].Path
})
sort.Slice(g.Objects, func(i, j int) bool {
return g.Objects[i].Name < g.Objects[j].Name
})
sort.Slice(g.Consts, func(i, j int) bool {
return g.Consts[i].Name < g.Consts[j].Name
})
tmpl := template.Must(template.New("").Parse(templateBase))
buf := bytes.NewBuffer(nil)
if err := tmpl.Execute(buf, g.generatorParam); err != nil {
return "", xerrors.Errorf("failed to generate template: %w", err)
}
b, err := format.Source(buf.Bytes())
if err != nil {
return "", xerrors.Errorf("failed to format source code: %w", err)
}
return string(b), nil
}