This repository has been archived by the owner on May 20, 2024. It is now read-only.
/
gen.rb
executable file
·538 lines (484 loc) · 20.5 KB
/
gen.rb
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/env ruby
require 'erubis'
require 'multi_json'
RESOURCE_TEMPLATE = <<-RESOURCE_TEMPLATE
// WARNING: This code is auto-generated from the Heroku Platform API JSON Schema
// by a Ruby script (gen/gen.rb). Changes should be made to the generation
// script rather than the generated files.
package heroku
<%- if schemas[key]['properties'] && schemas[key]['properties'].any?{|p, v| resolve_typedef(v).end_with?("time.Time") } %>
import (
"time"
)
<%- end %>
<%- if definition['properties'] %>
<%- description = markdown_free(definition["description"] || "") %>
<%- word_wrap(description, line_width: 77).split("\n").each do |line| %>
// <%= line %>
<%- end %>
type <%= resource_class %> struct {
<%- definition['properties'].each do |propname, propdef| %>
<%- resolved_propdef = resolve_propdef(propdef) %>
// <%= resolved_propdef["description"] %>
<%- type = resolve_typedef(resolved_propdef) %>
<%- if type =~ /\\*?struct/ %>
<%= titlecase(propname) %> <%= type %> {
<%- resolved_propdef["properties"].each do |subpropname, subpropdef| %>
<%= titlecase(subpropname) %> <%= resolve_typedef(subpropdef) %> `json:"<%= subpropname %>"`
<%- end %>
} `json:"<%= propname %>"`
<%- else %>
<%= titlecase(propname) %> <%= resolve_typedef(propdef) %> `json:"<%= propname %>"`
<%- end %>
<%- end %>
}
<%- end %>
<%- definition["links"].each do |link| %>
<%- func_name = titlecase(key.downcase. + "-" + link["title"]) %>
<%- func_args = [] %>
<%- func_args << (variablecase(parent_resource_instance) + 'Identity string') if parent_resource_instance %>
<%- func_args += func_args_from_model_and_link(definition, key, link) %>
<%- return_values = return_values_from_link(key, link) %>
<%- path = link['href'].gsub("{(%23%2Fdefinitions%2F\#{key}%2Fdefinitions%2Fidentity)}", '"+' + variablecase(resource_instance) + 'Identity') %>
<%- if parent_resource_instance %>
<%- path = path.gsub("{(%23%2Fdefinitions%2F" + parent_resource_instance + "%2Fdefinitions%2Fidentity)}", '" + ' + variablecase(parent_resource_instance) + 'Identity + "') %>
<%- end %>
<%- path = ensure_balanced_end_quote(ensure_open_quote(path)) %>
<%- word_wrap(markdown_free(link["description"]), line_width: 77).split("\n").each do |line| %>
// <%= line %>
<%- end %>
<%- func_arg_comments = [] %>
<%- func_arg_comments << (variablecase(parent_resource_instance) + "Identity is the unique identifier of the " + key + "'s " + parent_resource_instance + ".") if parent_resource_instance %>
<%- func_arg_comments += func_arg_comments_from_model_and_link(definition, key, link) %>
//
<%- word_wrap(func_arg_comments.join(" "), line_width: 77).split("\n").each do |comment| %>
// <%= comment %>
<%- end %>
<%- flat_postval = link["schema"] && link["schema"]["additionalProperties"] == false %>
<%- required = (link["schema"] && link["schema"]["required"]) || [] %>
<%- optional = ((link["schema"] && link["schema"]["properties"]) || {}).keys - required %>
<%- postval = if flat_postval %>
<%- "options" %>
<%- elsif required.empty? && optional.empty? %>
<%- "nil" %>
<%- elsif required.empty? %>
<%- "options" %>
<%- else %>
<%- "params" %>
<%- end %>
<%- hasCustomType = !schemas[key]["properties"].nil? %>
func (c *Client) <%= func_name + "(" + func_args.join(', ') %>) <%= return_values %> {
<%- case link["rel"] %>
<%- when "create" %>
<%- if !required.empty? %>
<%= Erubis::Eruby.new(LINK_PARAMS_TEMPLATE).result({modelname: key, link: link, required: required, optional: optional}).strip %>
<%- end %>
var <%= variablecase(key + '-res') %> <%= titlecase(key) %>
return &<%= variablecase(key + '-res') %>, c.Post(&<%= variablecase(key + '-res') %>, <%= path %>, <%= postval %>)
<%- when "self" %>
var <%= variablecase(key) %> <%= hasCustomType ? titlecase(key) : "map[string]string" %>
return <%= "&" if hasCustomType%><%= variablecase(key) %>, c.Get(&<%= variablecase(key) %>, <%= path %>)
<%- when "destroy" %>
return c.Delete(<%= path %>)
<%- when "update" %>
<%- if !required.empty? %>
<%= Erubis::Eruby.new(LINK_PARAMS_TEMPLATE).result({modelname: key, link: link, required: required, optional: optional}).strip %>
<%- end %>
<%- if link["title"].include?("Batch") %>
var <%= variablecase(key + 's-res') %> []<%= titlecase(key) %>
return <%= variablecase(key + 's-res') %>, c.Patch(&<%= variablecase(key + 's-res') %>, <%= path %>, <%= postval %>)
<%- else %>
var <%= variablecase(key + '-res') %> <%= hasCustomType ? titlecase(key) : "map[string]string" %>
return <%= "&" if hasCustomType%><%= variablecase(key + '-res') %>, c.Patch(&<%= variablecase(key + '-res') %>, <%= path %>, <%= postval %>)
<%- end %>
<%- when "instances" %>
req, err := c.NewRequest("GET", <%= path %>, nil)
if err != nil {
return nil, err
}
if lr != nil {
lr.SetHeader(req)
}
var <%= variablecase(key + 's-res') %> []<%= titlecase(key) %>
return <%= variablecase(key + 's-res') %>, c.DoReq(req, &<%= variablecase(key + 's-res') %>)
<%- end %>
}
<%- if %w{create update}.include?(link["rel"]) && link["schema"] && link["schema"]["properties"] %>
<%- if !required.empty? %>
<%- structs = required.select {|p| resolve_typedef(link["schema"]["properties"][p]) == "struct" } %>
<%- structs.each do |propname| %>
<%- typename = titlecase([key, link["title"], propname].join("-")) %>
// <%= typename %> used in <%= func_name %> as the <%= definition["properties"][propname]["description"] %>
type <%= typename %> struct {
<%- link["schema"]["properties"][propname]["properties"].each do |subpropname, subval| %>
<%- propdef = definition["properties"][propname]["properties"][subpropname] %>
<%- description = resolve_propdef(propdef)["description"] %>
<%- word_wrap(description, line_width: 77).split("\n").each do |line| %>
// <%= line %>
<%- end %>
<%= titlecase(subpropname) %> <%= resolve_typedef(subval) %> `json:"<%= subpropname %>"`
<%- end %>
}
<%- end %>
<%- arr_structs = required.select {|p| resolve_typedef(link["schema"]["properties"][p]) == "[]struct" } %>
<%- arr_structs.each do |propname| %>
<%- # special case for arrays of structs (like FormationBulkUpdate) %>
<%- typename = titlecase([key, link["title"], "opts"].join("-")) %>
<%- typedef = resolve_propdef(link["schema"]["properties"][propname]["items"]) %>
type <%= typename %> struct {
<%- typedef["properties"].each do |subpropname, subref| %>
<%- propdef = resolve_propdef(subref) %>
<%- description = resolve_propdef(propdef)["description"] %>
<%- is_required = typedef["required"].include?(subpropname) %>
<%- word_wrap(description, line_width: 77).split("\n").each do |line| %>
// <%= line %>
<%- end %>
<%= titlecase(subpropname) %> <%= "*" unless is_required %><%= resolve_typedef(propdef) %> `json:"<%= subpropname %><%= ",omitempty" unless is_required %>"`
<%- end %>
}
<%- end %>
<%- end %>
<%- if !optional.empty? %>
// <%= func_name %>Opts holds the optional parameters for <%= func_name %>
type <%= func_name %>Opts struct {
<%- optional.each do |propname| %>
<%- if definition['properties'][propname] && definition['properties'][propname]['description'] %>
// <%= definition['properties'][propname]['description'] %>
<%- elsif definition["definitions"][propname] %>
// <%= definition["definitions"][propname]["description"] %>
<%- elsif link["schema"]["properties"][propname]["$ref"] %>
// <%= resolve_propdef(link["schema"]["properties"][propname])["description"] %>
<%- else %>
// <%= link["schema"]["properties"][propname]["description"] %>
<%- end %>
<%= titlecase(propname) %> <%= type_for_link_opts_field(link, propname) %> `json:"<%= propname %>,omitempty"`
<%- end %>
}
<%- end %>
<%- end %>
<%- end %>
RESOURCE_TEMPLATE
LINK_PARAMS_TEMPLATE = <<-LINK_PARAMS_TEMPLATE
params := struct {
<%- required.each do |propname| %>
<%- type = resolve_typedef(link["schema"]["properties"][propname]) %>
<%- if type == "[]struct" %>
<%- type = type.gsub("struct", titlecase([modelname, link["title"], "opts"].join("-"))) %>
<%- elsif type == "struct" %>
<%- type = titlecase([modelname, link["title"], propname].join("-")) %>
<%- end %>
<%= titlecase(propname) %> <%= type %> `json:"<%= propname %>"`
<%- end %>
<%- optional.each do |propname| %>
<%= titlecase(propname) %> <%= type_for_link_opts_field(link, propname) %> `json:"<%= propname %>,omitempty"`
<%- end %>
}{
<%- required.each do |propname| %>
<%= titlecase(propname) %>: <%= variablecase(propname) %>,
<%- end %>
}
<%- if optional.count > 0 %>
if options != nil {
<%- optional.each do |propname| %>
params.<%= titlecase(propname) %> = options.<%= titlecase(propname) %>
<%- end %>
}
<%- end %>
LINK_PARAMS_TEMPLATE
# definition: data,
# key: modelname,
# parent_resource_class: parent_resource_class,
# parent_resource_identity: parent_resource_identity,
# parent_resource_instance: parent_resource_instance,
# resource_class: resource_class,
# resource_instance: resource_instance,
# resource_proxy_class: resource_proxy_class,
# resource_proxy_instance: resource_proxy_instance
module Generator
extend self
def ensure_open_quote(str)
str[0] == '"' ? str : "\"#{str}"
end
def ensure_balanced_end_quote(str)
(str.count('"') % 2) == 1 ? "#{str}\"" : str
end
def must_end_with(str, ending)
str.end_with?(ending) ? str : "#{str}#{ending}"
end
def word_wrap(text, options = {})
line_width = options.fetch(:line_width, 80)
text.split("\n").collect do |line|
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
end * "\n"
end
def markdown_free(text)
text.gsub(/\[(?<linktext>[^\]]*)\](?<linkurl>\(.*\))/, '\k<linktext>').
gsub(/`(?<rawtext>[^\]]*)`/, '\k<rawtext>').gsub("NULL", "nil")
end
def variablecase(str)
words = str.gsub('_','-').gsub(' ','-').split('-')
(words[0...1] + words[1..-1].map {|k| k[0...1].upcase + k[1..-1]}).join
end
def titlecase(str)
str.gsub('_','-').gsub(' ','-').split('-').map do |k|
# special case so Url becomes URL, Ssl becomes SSL
if %w{url ssl}.include?(k.downcase)
k.upcase
elsif k.downcase == "oauth" # special case so Oauth becomes OAuth
"OAuth"
else
k[0...1].upcase + k[1..-1]
end
end.join
end
def resolve_typedef(propdef)
if types = propdef["type"]
null = types.include?("null")
tname = case (types - ["null"]).first
when "boolean"
"bool"
when "integer"
"int"
when "string"
format = propdef["format"]
format && format == "date-time" ? "time.Time" : "string"
when "object"
if propdef["additionalProperties"] == false
if propdef["patternProperties"]
"map[string]string"
else
# special case for arrays of structs (like FormationBulkUpdate)
"struct"
end
else
"struct"
end
when "array"
arraytype = if propdef["items"]["$ref"]
resolve_typedef(propdef["items"])
else
puts "propdef[items][type]: #{propdef["items"]["type"].inspect}"
propdef["items"]["type"]
end
"[]#{arraytype}"
else
types.first
end
null ? "*#{tname}" : tname
elsif propdef["anyOf"]
# identity cross-reference, cheat because these are always strings atm
"string"
elsif propdef["additionalProperties"] == false
# inline object
propdef
elsif ref = propdef["$ref"]
matches = ref.match(/\/definitions\/([\w-]+)\/definitions\/([\w-]+)/)
schemaname, fieldname = matches[1..2]
resolve_typedef(schemas[schemaname]["definitions"][fieldname])
else
raise "WTF #{propdef}"
end
end
def type_for_link_opts_field(link, propname, nullable = true)
resulttype = resolve_typedef(link["schema"]["properties"][propname])
if nullable && !resulttype.start_with?("*")
resulttype = "*#{resulttype}"
elsif !nullable
resulttype = resulttype.gsub("*", "")
end
resulttype
end
def type_from_types_and_format(types, format)
case types.first
when "boolean"
"bool"
when "integer"
"int"
when "string"
format && format == "date-time" ? "time.Time" : "string"
else
types.first
end
end
def return_values_from_link(modelname, link)
if !schemas[modelname]["properties"]
# structless type like ConfigVar
"(map[string]string, error)"
else
case link["rel"]
when "destroy"
"error"
when "instances"
"([]#{titlecase(modelname)}, error)"
else
if link["title"].include?("Batch")
"([]#{titlecase(modelname)}, error)"
else
"(*#{titlecase(modelname)}, error)"
end
end
end
end
def func_args_from_model_and_link(definition, modelname, link)
args = []
required = (link["schema"] && link["schema"]["required"]) || []
optional = ((link["schema"] && link["schema"]["properties"]) || {}).keys - required
# check if this link's href requires the model's identity
match = link["href"].match(%r{%23%2Fdefinitions%2F#{modelname}%2Fdefinitions%2Fidentity})
if %w{update destroy self}.include?(link["rel"]) && match
args << "#{variablecase(modelname)}Identity string"
end
if %w{create update}.include?(link["rel"])
if link["schema"]["additionalProperties"] == false
# handle ConfigVar update
args << "options map[string]*string"
else
required.each do |propname|
type = type_for_link_opts_field(link, propname, false)
if type == "[]struct"
type = type.gsub("struct", titlecase([modelname, link["title"], "Opts"].join("-")))
elsif type == "struct"
type = type.gsub("struct", titlecase([modelname, link["title"], propname].join("-")))
end
args << "#{variablecase(propname)} #{type}"
end
end
args << "options *#{titlecase(modelname)}#{link["rel"].capitalize}Opts" unless optional.empty?
end
if "instances" == link["rel"]
args << "lr *ListRange"
end
args
end
def resolve_propdef(propdef)
resolve_all_propdefs(propdef).first
end
def resolve_all_propdefs(propdef)
if propdef["description"]
[propdef]
elsif ref = propdef["$ref"]
matches = ref.match(/#\/definitions\/([\w-]+)\/definitions\/([\w-]+)/)
schemaname, fieldname = matches[1..2]
resolve_all_propdefs(schemas[schemaname]["definitions"][fieldname])
elsif anyof = propdef["anyOf"]
# Identity
anyof.map do |refhash|
matches = refhash["$ref"].match(/#\/definitions\/([\w-]+)\/definitions\/([\w-]+)/)
schemaname, fieldname = matches[1..2]
resolve_all_propdefs(schemas[schemaname]["definitions"][fieldname])
end.flatten
elsif propdef["type"] && propdef["type"].is_a?(Array) && propdef["type"].first == "object"
# special case for params which are nested objects, like oauth-grant
[propdef]
else
raise "WTF #{propdef}"
end
end
def func_arg_comments_from_model_and_link(definition, modelname, link)
args = []
flat_postval = link["schema"] && link["schema"]["additionalProperties"] == false
properties = (link["schema"] && link["schema"]["properties"]) || {}
required_keys = (link["schema"] && link["schema"]["required"]) || []
optional_keys = properties.keys - required_keys
if %w{update destroy self}.include?(link["rel"])
if flat_postval
# special case for ConfigVar update w/ flat param struct
desc = markdown_free(link["schema"]["description"])
args << "options is the #{desc}."
else
args << "#{variablecase(modelname)}Identity is the unique identifier of the #{titlecase(modelname)}."
end
end
if %w{create update}.include?(link["rel"])
required_keys.each do |propname|
rpresults = resolve_all_propdefs(link["schema"]["properties"][propname])
if rpresults.size == 1
if rpresults.first["properties"]
# special case for things like OAuthToken with nested objects
rpresults = resolve_all_propdefs(definition["properties"][propname])
end
args << "#{variablecase(propname)} is the #{must_end_with(rpresults.first["description"] || "", ".")}"
elsif rpresults.size == 2
args << "#{variablecase(propname)} is the #{rpresults.first["description"]} or #{must_end_with(rpresults.last["description"] || "", ".")}"
else
raise "Didn't expect 3 rpresults"
end
end
args << "options is the struct of optional parameters for this action." unless optional_keys.empty?
end
if "instances" == link["rel"]
args << "lr is an optional ListRange that sets the Range options for the paginated list of results."
end
case link["rel"]
when "create"
["options is the struct of optional parameters for this action."]
when "update"
["#{variablecase(modelname)}Identity is the unique identifier of the #{titlecase(modelname)}.",
"options is the struct of optional parameters for this action."]
when "destroy", "self"
["#{variablecase(modelname)}Identity is the unique identifier of the #{titlecase(modelname)}."]
when "instances"
["lr is an optional ListRange that sets the Range options for the paginated list of results."]
else
[]
end
args
end
def resource_instance_from_model(modelname)
modelname.downcase.split('-').join('_')
end
def schemas
@@schemas ||= {}
end
def load_schema
schema_path = File.expand_path("./schema.json")
schema = MultiJson.load(File.read(schema_path))
schema["definitions"].each do |modelname, val|
schemas[modelname] = val
end
end
def generate_model(modelname)
if !schemas[modelname]
puts "no schema for #{modelname}" && return
end
if schemas[modelname]['links'].empty?
puts "no links for #{modelname}"
end
resource_class = titlecase(modelname)
resource_instance = resource_instance_from_model(modelname)
resource_proxy_class = resource_class + 's'
resource_proxy_instance = resource_instance + 's'
parent_resource_class, parent_resource_identity, parent_resource_instance = if schemas[modelname]['links'].all? {|link| link['href'].include?('{(%23%2Fdefinitions%2Fapp%2Fdefinitions%2Fidentity)}')}
['App', 'app_identity', 'app']
elsif schemas[modelname]['links'].all? {|link| link['href'].include?('{(%23%2Fdefinitions%2Faddon-service%2Fdefinitions%2Fidentity)}')}
['AddonService', 'addon_service', 'addon-service']
end
data = Erubis::Eruby.new(RESOURCE_TEMPLATE).result({
definition: schemas[modelname],
key: modelname,
parent_resource_class: parent_resource_class,
parent_resource_identity: parent_resource_identity,
parent_resource_instance: parent_resource_instance,
resource_class: resource_class,
resource_instance: resource_instance,
resource_proxy_class: resource_proxy_class,
resource_proxy_instance: resource_proxy_instance
})
path = File.expand_path(File.join(File.dirname(__FILE__), '..', "#{modelname.gsub('-', '_')}.go"))
File.open(path, 'w') do |file|
file.write(data)
end
%x( go fmt #{path} )
end
end
include Generator
puts "Loading schema..."
Generator.load_schema
schemas.keys.each do |modelname|
puts "Generating #{modelname}..."
if (Generator.schemas[modelname]["links"] || []).empty? && Generator.schemas[modelname]["properties"].empty?
puts "-- skipping #{modelname} because it has no links or properties"
else
Generator.generate_model(modelname)
end
end