This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.go
276 lines (257 loc) · 9.62 KB
/
model.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
package model
//go:generate generatemodel -u http://references.taskcluster.net/manifest.json -f apis.json -o ../.. -m model-data.txt
import (
"encoding/json"
"fmt"
"github.com/taskcluster/taskcluster-client-java/codegenerator/utils"
"github.com/xeipuuv/gojsonschema"
"io"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
)
var (
apiDefs []APIDefinition
err error
)
type SortedAPIDefs []APIDefinition
// needed so that SortedAPIDefs can implement sort.Interface
func (a SortedAPIDefs) Len() int { return len(a) }
func (a SortedAPIDefs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortedAPIDefs) Less(i, j int) bool { return a[i].URL < a[j].URL }
type APIModel interface {
String() string
postPopulate(apiDef *APIDefinition)
generateAPICode(name string) string
setAPIDefinition(apiDef *APIDefinition)
}
// APIDefinition represents the definition of a REST API, comprising of the URL to the defintion
// of the API in json format, together with a URL to a json schema to validate the definition
type APIDefinition struct {
URL string `json:"url"`
SchemaURL string `json:"schema"`
Name string `json:"name"`
DocRoot string `json:"docroot"`
Data APIModel
schemaURLs []string
schemas map[string]*JsonSubSchema
PackageName string
PackagePath string
}
func (a *APIDefinition) generateAPICode() string {
return a.Data.generateAPICode(a.Name)
}
func (apiDef *APIDefinition) loadJson(reader io.Reader, schema *string) {
var m APIModel
switch *schema {
case "http://schemas.taskcluster.net/base/v1/api-reference.json":
m = new(API)
case "http://schemas.taskcluster.net/base/v1/exchanges-reference.json":
m = new(Exchange)
}
decoder := json.NewDecoder(reader)
err = decoder.Decode(m)
utils.ExitOnFail(err)
m.setAPIDefinition(apiDef)
m.postPopulate(apiDef)
apiDef.Data = m
}
func (apiDef *APIDefinition) loadJsonSchema(url string) *JsonSubSchema {
var resp *http.Response
resp, err = http.Get(url)
utils.ExitOnFail(err)
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
m := new(JsonSubSchema)
err = decoder.Decode(m)
utils.ExitOnFail(err)
m.postPopulate(apiDef)
return m
}
func (apiDef *APIDefinition) cacheJsonSchema(url *string) *JsonSubSchema {
// if url is not provided, there is nothing to download
if url == nil || *url == "" {
return nil
}
// workaround for problem where some urls don't end with a #
if (*url)[len(*url)-1:] != "#" {
*url += "#"
}
// only fetch if we haven't fetched already...
if _, ok := apiDef.schemas[*url]; !ok {
apiDef.schemas[*url] = apiDef.loadJsonSchema(*url)
apiDef.schemas[*url].SourceURL = *url
}
return apiDef.schemas[*url]
}
// LoadAPIs takes care of reading all json files and performing elementary
// processing of the data, such as assigning unique type names to entities
// which will be translated to go types.
//
// Data is unmarshaled into objects (or instances of go types) and then
// postPopulate is called on the objects. This in turn triggers further reading
// of json files and unmarshalling where schemas refer to other schemas.
//
// When LoadAPIs returns, all json schemas and sub schemas should have been
// read and unmarhsalled into go objects.
func LoadAPIs(apiManifestUrl, supplementaryDataFile string) []APIDefinition {
resp, err := http.Get(apiManifestUrl)
if err != nil {
fmt.Printf("Could not download api manifest from url: '%v'!\n", apiManifestUrl)
}
supDataReader, err := os.Open(supplementaryDataFile)
if err != nil {
fmt.Printf("Could not load supplementary data json file: '%v'!\n", supplementaryDataFile)
}
utils.ExitOnFail(err)
apiManifestDecoder := json.NewDecoder(resp.Body)
apiMan := make(map[string]string)
err = apiManifestDecoder.Decode(&apiMan)
utils.ExitOnFail(err)
supDataDecoder := json.NewDecoder(supDataReader)
err = supDataDecoder.Decode(&apiDefs)
utils.ExitOnFail(err)
sort.Sort(SortedAPIDefs(apiDefs))
// build up apis based on data in *both* data sources
for i := range apiMan {
// seach for apiMan[i] in apis
k := sort.Search(len(apiDefs), func(j int) bool {
return apiDefs[j].URL >= apiMan[i]
})
if k < len(apiDefs) && apiDefs[k].URL == apiMan[i] {
// url is present in supplementary data
apiDefs[k].Name = i
} else {
fmt.Printf(
"\nFATAL: Manifest from url '%v' contains key '%v' with url '%v', but this url does not exist in supplementary data file '%v', therefore exiting...\n\n",
apiManifestUrl, i, apiMan[i], supplementaryDataFile)
os.Exit(64)
}
}
for i := range apiDefs {
if apiDefs[i].Name == "" {
fmt.Printf(
"\nFATAL: Manifest from url '%v' does not contain url '%v' which does exist in supplementary data file '%v', therefore exiting...\n\n",
apiManifestUrl, apiDefs[i].URL, supplementaryDataFile)
os.Exit(65)
}
}
for i := range apiDefs {
// first check that the json schema is valid!
validateJson(apiDefs[i].SchemaURL, apiDefs[i].URL)
apiDefs[i].schemas = make(map[string]*JsonSubSchema)
var resp *http.Response
resp, err = http.Get(apiDefs[i].URL)
utils.ExitOnFail(err)
defer resp.Body.Close()
apiDefs[i].loadJson(resp.Body, &apiDefs[i].SchemaURL)
// now all data should be loaded, let's sort the schemas
apiDefs[i].schemaURLs = make([]string, 0, len(apiDefs[i].schemas))
for url := range apiDefs[i].schemas {
apiDefs[i].schemaURLs = append(apiDefs[i].schemaURLs, url)
}
sort.Strings(apiDefs[i].schemaURLs)
// finally, now we can generate normalised names
// for schemas
// keep a record of generated type names, so that we don't reuse old names
// map[string]bool acts like a set of strings
TypeName := make(map[string]bool)
for _, j := range apiDefs[i].schemaURLs {
apiDefs[i].schemas[j].TypeName = utils.Normalise(*apiDefs[i].schemas[j].Title, TypeName)
}
//////////////////////////////////////////////////////////////////////////////
// these next four lines are a temporary hack while waiting for https://github.com/taskcluster/taskcluster-queue/pull/31
if x, ok := apiDefs[i].schemas["http://schemas.taskcluster.net/queue/v1/list-artifacts-response.json#"]; ok {
y := "object"
x.Properties.Properties["artifacts"].Items.Type = &y
}
//////////////////////////////////////////////////////////////////////////////
}
return apiDefs
}
func validateJson(schemaUrl, docUrl string) {
schemaLoader := gojsonschema.NewReferenceLoader(schemaUrl)
docLoader := gojsonschema.NewReferenceLoader(docUrl)
result, err := gojsonschema.Validate(schemaLoader, docLoader)
utils.ExitOnFail(err)
if result.Valid() {
fmt.Printf("Document '%v' is valid against '%v'.\n", docUrl, schemaUrl)
} else {
fmt.Printf("Document '%v' is INVALID against '%v'.\n", docUrl, schemaUrl)
for _, desc := range result.Errors() {
fmt.Println("")
fmt.Printf("- %s\n", desc)
}
// os.Exit(70)
}
}
// GenerateCode takes the objects loaded into memory in LoadAPIs
// and writes them out as go code.
func GenerateCode(goOutputDir, modelData string) {
for i := range apiDefs {
apiDefs[i].PackageName = strings.ToLower(apiDefs[i].Name)
apiDefs[i].PackagePath = filepath.Join(goOutputDir, "src", "main", "java", "org", "mozilla", "taskcluster", "client", apiDefs[i].PackageName)
err = os.MkdirAll(apiDefs[i].PackagePath, 0755)
utils.ExitOnFail(err)
content := `// The following code is AUTO-GENERATED. Please DO NOT edit.
//
// This package was generated from the schema defined at
// ` + apiDefs[i].URL + `
`
generatePayloadTypes(&apiDefs[i])
content += apiDefs[i].generateAPICode()
className := strings.Title(apiDefs[i].Name)
sourceFile := filepath.Join(apiDefs[i].PackagePath, className+".java")
fmt.Println("Formatting source code " + sourceFile + "...")
// formattedContent, err := format.Source([]byte(content))
// in case of formatting failure, let's keep the unformatted version so
// we can troubleshoot more easily...
// if err != nil {
utils.WriteStringToFile(content, sourceFile)
// }
utils.ExitOnFail(err)
// utils.WriteStringToFile(string(formattedContent), sourceFile)
}
content := "The following file is an auto-generated static dump of the API models at time of code generation.\n"
content += "It is provided here for reference purposes, but is not used by any code.\n"
content += "\n"
for i := range apiDefs {
content += utils.Underline(apiDefs[i].URL)
content += apiDefs[i].Data.String() + "\n\n"
for _, url := range apiDefs[i].schemaURLs {
content += (utils.Underline(url))
content += apiDefs[i].schemas[url].String() + "\n\n"
}
}
utils.WriteStringToFile(content, modelData)
}
// This is where we generate nested and compoound types in go to represent json payloads
// which are used as inputs and outputs for the REST API endpoints, and also for Pulse
// message bodies for the Exchange APIs.
// Returns the generated code content, and a map of keys of extra packages to import, e.g.
// a generated type might use time.Time, so if not imported, this would have to be added.
// using a map of strings -> bool to simulate a set - true => include
func generatePayloadTypes(apiDef *APIDefinition) {
for _, i := range apiDef.schemaURLs {
extraPackages := make(map[string]bool)
content := "package org.mozilla.taskcluster.client." + strings.ToLower(apiDef.PackageName) + ";\n"
content += "\n"
var typeContent string
var simpleType string
typeContent, extraPackages, simpleType = apiDef.schemas[i].TypeDefinition(0, false, extraPackages)
if simpleType == "" {
if len(extraPackages) > 0 {
for pckage := range extraPackages {
content += "import " + pckage + ";\n"
}
content += "\n"
}
content += typeContent
utils.WriteStringToFile(content, filepath.Join(apiDef.PackagePath, apiDef.schemas[i].TypeName+".java"))
} else {
apiDef.schemas[i].TypeName = simpleType
}
}
}