-
Notifications
You must be signed in to change notification settings - Fork 33
/
gen.go
387 lines (349 loc) · 9.13 KB
/
gen.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//go:generate templates -s templates -o templates/templates.go
package schematic
import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"os"
"strings"
"text/template"
bundle "github.com/interagent/schematic/templates"
)
var templates *template.Template
func init() {
templates = template.New("package.tmpl").Funcs(helpers)
templates = template.Must(bundle.Parse(templates))
}
// ResolvedSet stores a set of pointers to objects that have already been
// resolved to prevent infinite loops.
type ResolvedSet map[interface{}]bool
// Insert marks a pointer as resolved.
func (rs ResolvedSet) Insert(o interface{}) {
rs[o] = true
}
// Has returns if a pointer has already been resolved.
func (rs ResolvedSet) Has(o interface{}) bool {
return rs[o]
}
// Generate generates code according to the schema.
func (s *Schema) Generate() ([]byte, error) {
var buf bytes.Buffer
s = s.Resolve(nil, ResolvedSet{})
name := strings.ToLower(strings.Split(s.Title, " ")[0])
templates.ExecuteTemplate(&buf, "package.tmpl", name)
// TODO: Check if we need time.
templates.ExecuteTemplate(&buf, "imports.tmpl", []string{
"encoding/json", "fmt", "io", "reflect", "net/http", "runtime",
"time", "bytes", "context", "strings",
"github.com/google/go-querystring/query",
})
templates.ExecuteTemplate(&buf, "service.tmpl", struct {
Name string
URL string
Version string
}{
Name: name,
URL: s.URL(),
Version: s.Version,
})
for _, name := range sortedKeys(s.Properties) {
schema := s.Properties[name]
// Skipping definitions because there is no links, nor properties.
if schema.Links == nil && schema.Properties == nil {
continue
}
context := struct {
Name string
Definition *Schema
}{
Name: name,
Definition: schema,
}
if !context.Definition.AreTitleLinksUnique() {
return nil, fmt.Errorf("duplicate titles detected for %s", context.Name)
}
templates.ExecuteTemplate(&buf, "struct.tmpl", context)
templates.ExecuteTemplate(&buf, "funcs.tmpl", context)
}
// Remove blank lines added by text/template
bytes := newlines.ReplaceAll(buf.Bytes(), []byte(""))
// Format sources
clean, err := format.Source(bytes)
if err != nil {
return buf.Bytes(), err
}
return clean, nil
}
// Resolve resolves reference inside the schema.
func (s *Schema) Resolve(r *Schema, rs ResolvedSet) *Schema {
if r == nil {
r = s
}
for {
if s.Ref != nil {
s = s.Ref.Resolve(r)
} else if len(s.OneOf) > 0 {
s = s.OneOf[0].Ref.Resolve(r)
} else if len(s.AnyOf) > 0 {
s = s.AnyOf[0].Ref.Resolve(r)
} else {
break
}
}
if rs.Has(s) {
// Already resolved
return s
}
rs.Insert(s)
for n, d := range s.Definitions {
s.Definitions[n] = d.Resolve(r, rs)
}
for n, p := range s.Properties {
s.Properties[n] = p.Resolve(r, rs)
}
for n, p := range s.PatternProperties {
s.PatternProperties[n] = p.Resolve(r, rs)
}
if s.Items != nil {
s.Items = s.Items.Resolve(r, rs)
}
for _, l := range s.Links {
l.Resolve(r, rs)
}
return s
}
// Types returns the array of types described by this schema.
func (s *Schema) Types() (types []string, err error) {
if arr, ok := s.Type.([]interface{}); ok {
for _, v := range arr {
types = append(types, v.(string))
}
} else if str, ok := s.Type.(string); ok {
types = append(types, str)
} else {
err = fmt.Errorf("unknown type %v", s.Type)
}
return types, err
}
// GoType returns the Go type for the given schema as string.
func (s *Schema) GoType() string {
return s.goType(true, true)
}
// IsCustomType returns true if the schema declares a custom type.
func (s *Schema) IsCustomType() bool {
return len(s.Properties) > 0
}
func (s *Schema) goType(required bool, force bool) (goType string) {
// Resolve JSON reference/pointer
types, err := s.Types()
if err != nil {
fail(s, err)
}
for _, kind := range types {
switch kind {
case "boolean":
goType = "bool"
case "string":
switch s.Format {
case "date-time":
goType = "time.Time"
default:
goType = "string"
}
case "number":
goType = "float64"
case "integer":
goType = "int"
case "any":
goType = "interface{}"
case "array":
if s.Items != nil {
goType = "[]" + s.Items.goType(required, force)
} else {
goType = "[]interface{}"
}
case "object":
// Check if patternProperties exists.
if s.PatternProperties != nil {
for _, prop := range s.PatternProperties {
goType = fmt.Sprintf("map[string]%s", prop.GoType())
break // We don't support more than one pattern for now.
}
continue
}
buf := bytes.NewBufferString("struct {")
for _, name := range sortedKeys(s.Properties) {
prop := s.Properties[name]
req := contains(name, s.Required) || force
templates.ExecuteTemplate(buf, "field.tmpl", struct {
Definition *Schema
Name string
Required bool
Type string
}{
Definition: prop,
Name: name,
Required: req,
Type: prop.goType(req, force),
})
}
buf.WriteString("}")
goType = buf.String()
case "null":
continue
default:
fail(s, fmt.Errorf("unknown type %s", kind))
}
}
if goType == "" {
fail(s, fmt.Errorf("type not found : %s", types))
}
// Types allow null
if contains("null", types) || !(required || force) {
// Don't need a pointer for these types to be "nilable"
if goType != "interface{}" && !strings.HasPrefix(goType, "[]") && !strings.HasPrefix(goType, "map[") {
return "*" + goType
}
}
return goType
}
// Values returns function return values types.
func (s *Schema) Values(name string, l *Link) []string {
var values []string
name = returnType(name, s, l)
if s.EmptyResult(l) {
values = append(values, "error")
} else if s.ReturnsCustomType(l) {
values = append(values, fmt.Sprintf("*%s", name), "error")
} else {
values = append(values, name, "error")
}
return values
}
// AreTitleLinksUnique ensures that all titles are unique for a schema.
//
// If more than one link in a given schema has the same title, we cannot
// accurately generate the client from the schema. Although it's not strictly a
// schema violation, it needs to be fixed before the client can be properly
// generated.
func (s *Schema) AreTitleLinksUnique() bool {
titles := map[string]bool{}
var uniqueLinks []*Link
for _, link := range s.Links {
title := strings.ToLower(link.Title)
if _, ok := titles[title]; !ok {
uniqueLinks = append(uniqueLinks, link)
}
titles[title] = true
}
return len(uniqueLinks) == len(s.Links)
}
// URL returns schema base URL.
func (s *Schema) URL() string {
for _, l := range s.Links {
if l.Rel == "self" {
return l.HRef.String()
}
}
return ""
}
// ReturnsCustomType returns true if the link returns a custom type.
func (s *Schema) ReturnsCustomType(l *Link) bool {
if l.TargetSchema != nil {
return len(l.TargetSchema.Properties) > 0
}
return len(s.Properties) > 0
}
// ReturnedGoType returns Go type returned by the given link as a string.
func (s *Schema) ReturnedGoType(name string, l *Link) string {
if l.TargetSchema != nil {
if l.TargetSchema.Items == s {
return "[]" + initialCap(name)
}
return l.TargetSchema.goType(true, true)
}
return s.goType(true, true)
}
// EmptyResult retursn true if the link result should be empty.
func (s *Schema) EmptyResult(l *Link) bool {
var (
types []string
err error
)
if l.TargetSchema != nil {
types, err = l.TargetSchema.Types()
} else {
types, err = s.Types()
}
if err != nil {
return true
}
return len(types) == 1 && types[0] == "null"
}
// Parameters returns function parameters names and types.
func (l *Link) Parameters(name string) ([]string, map[string]string) {
if l.HRef == nil {
// No HRef property
panic(fmt.Errorf("no href property declared for %s", l.Title))
}
var order []string
params := make(map[string]string)
for _, name := range l.HRef.Order {
def := l.HRef.Schemas[name]
order = append(order, name)
params[name] = def.GoType()
}
if l.Schema != nil {
order = append(order, "o")
t, required := l.GoType()
if l.AcceptsCustomType() {
params["o"] = paramType(name, l)
} else {
params["o"] = t
}
if !required {
params["o"] = "*" + params["o"]
}
}
if l.Rel == "instances" && strings.ToUpper(l.Method) == "GET" {
order = append(order, "lr")
params["lr"] = "*ListRange"
}
return order, params
}
// AcceptsCustomType returns true if the link schema is not a primitive type
func (l *Link) AcceptsCustomType() bool {
if l.Schema != nil && l.Schema.IsCustomType() {
return true
}
return false
}
// Resolve resolve link schema and href.
func (l *Link) Resolve(r *Schema, rs ResolvedSet) {
if l.Schema != nil {
l.Schema = l.Schema.Resolve(r, rs)
}
if l.TargetSchema != nil {
l.TargetSchema = l.TargetSchema.Resolve(r, rs)
}
l.HRef.Resolve(r, rs)
}
// GoType returns Go type for the given schema as string and a bool specifying whether it is required
func (l *Link) GoType() (string, bool) {
t := l.Schema.goType(true, false)
if t[0] == '*' {
return t[1:], false
}
return t, true
}
func fail(v interface{}, err error) {
el, _ := json.MarshalIndent(v, " ", " ")
fmt.Fprintf(
os.Stderr,
"Error processing schema element:\n %s\n\nFailed with: %s\n",
el,
err,
)
os.Exit(1)
}