Module: CanvasApi::GoHelpers

Included in:
Render
Defined in:
lib/canvas_api/go_helpers.rb

Constant Summary collapse

QuizSubmissionOverrides =

HACK for canvas.instructure.com/doc/api/quiz_submissions.html update_student_question_scores_and_comments has a param with the following form “name”=>“quiz_submissions”, “type”=>“array”, “format”=>nil, “required”=>false, “deprecated”=>false, “items”=>{“$ref”=>“Hash”}

"QuizSubmissionOverrides"

Instance Method Summary collapse

Instance Method Details

#go_api_urlObject



502
503
504
505
506
507
508
# File 'lib/canvas_api/go_helpers.rb', line 502

def go_api_url
  url = @api_url
  @args.each do |arg|
    url.gsub(arg, "+\"#{go_name(arg)}\"+")
  end
  url
end

#go_comments(parameter, include_description = true) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/canvas_api/go_helpers.rb', line 216

def go_comments(parameter, include_description = true)
  out = " (#{parameter["required"] ? 'Required' : 'Optional'}) "
  if parameter["enum"]
    out << ". Must be one of #{parameter["enum"].join(', ')}"
  end
  if include_description && parameter["description"]
    out << parameter["description"].gsub("\n", "\n//    ")
  end
  out
end

#go_declaration(name, type) ⇒ Object



264
265
266
267
# File 'lib/canvas_api/go_helpers.rb', line 264

def go_declaration(name, type)
  json = name.underscore.split("[")[0].gsub("`rlid`", "rlid")
  out = "#{go_name(name)} #{type} `json:\"#{json}\" url:\"#{json},omitempty\"`"
end

#go_do_final_return_statement(operation, nickname) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/canvas_api/go_helpers.rb', line 538

def go_do_final_return_statement(operation, nickname)
  if nickname == "assign_unassigned_members"
    "return &groupMembership, &progress, nil"
  elsif go_return_type(operation)
    if is_paged?(operation)
      "return ret, pagedResource, nil"
    elsif operation["type"] == "boolean" || operation["type"] == "string" || operation["type"] == "integer"
      "return ret, nil"
    else
      "return &ret, nil"
    end
  else
    "return nil"
  end
end

#go_do_return_statement(operation, nickname) ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/canvas_api/go_helpers.rb', line 520

def go_do_return_statement(operation, nickname)
  if nickname == "assign_unassigned_members" || is_paged?(operation)
    "return nil, nil, err"
  elsif type = go_return_type(operation)
    if type == "bool"
      "return false, err"
    elsif type == "string"
      'return "", err'
    elsif type == "integer"
      "return 0, err"
    else
      "return nil, err"
    end
  else
    "return err"
  end
end

#go_do_return_value(operation, nickname) ⇒ Object



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/canvas_api/go_helpers.rb', line 554

def go_do_return_value(operation, nickname)
  if nickname == "assign_unassigned_members"
    # HACK. harded coded because Assign unassigned members returns different values based on input
    # see https://canvas.instructure.com/doc/api/group_categories.html#method.group_categories.assign_unassigned_members
    "(*models.GroupMembership, *models.Progress, error)"
  elsif type = go_return_type(operation)
    if is_paged?(operation)
      "(#{type}, *canvasapi.PagedResource, error)"
    else
      "(#{type}, error)"
    end
  else
    "error"
  end
end

#go_field_validation(model) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/canvas_api/go_helpers.rb', line 451

def go_field_validation(model)
  return nil unless model["properties"]
  allowable = {}
  model["properties"].each do |name, property|
    if property["allowableValues"]
      values = property["allowableValues"]["values"].map do |value|
        "\"#{value}\""
      end
      allowable[name] = {
        values: values,
        type: property["type"],
      }
    end
  end
  allowable
end

#go_form_params(params) ⇒ Object



489
490
491
# File 'lib/canvas_api/go_helpers.rb', line 489

def go_form_params(params)
  select_params("form", params)
end

#go_name(name) ⇒ Object



269
270
271
272
273
274
275
276
277
278
# File 'lib/canvas_api/go_helpers.rb', line 269

def go_name(name)
  parts = name.split("[")
  parts[0].camelize.gsub("-", "").gsub("_", "")
    .gsub("`rlid`", "RLID")
    .gsub("Id", "ID")
    .gsub("url", "URL")
    .gsub("Sis", "SIS")
    .gsub("MediaTrackk", "MediaTrack")
    .gsub("Https:::::Canvas.instructure.com::Lti::Submission", "CanvasLTISubmission")
end

#go_param_empty_value(parameter) ⇒ Object



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
# File 'lib/canvas_api/go_helpers.rb', line 234

def go_param_empty_value(parameter)
  name = parameter["name"]
  if is_x_param?(name)
    return "nil"
  end
  type = go_type(name, parameter)
  case type
  when "int64"
  when "int"
  when "float64"
    "0"
  when "string"
    '""'
  when "time.Time"
    "nil"
  when "map[string](interface{})"
    "nil"
  else
    if type.include?("[]")
      "nil"
    else
      "need empty value for #{type}"
    end
  end
end

#go_param_kind(parmeter) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/canvas_api/go_helpers.rb', line 468

def go_param_kind(parmeter)
  case parmeter["paramType"]
  when "path"
    "Path"
  when "query"
    "Query"
  when "form"
    "Form"
  else
    "Unknown paramType"
  end
end

#go_param_path(param) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/canvas_api/go_helpers.rb', line 173

def go_param_path(param)
  if is_x_param?(param["name"])
    "#{go_param_kind(param)}.#{go_name(param["name"])}"
  elsif is_nested?(param)
    structs, name = split_nested(param)
    "#{go_param_kind(param)}.#{structs.map{|s| struct_name(s)}.join('.')}.#{go_name(name)}"
  else
    "#{go_param_kind(param)}.#{go_name(param["name"])}"
  end
end

#go_param_to_field(parameter, name = nil) ⇒ Object



210
211
212
213
214
# File 'lib/canvas_api/go_helpers.rb', line 210

def go_param_to_field(parameter, name = nil)
  name ||= parameter["name"]
  type = go_type(name, parameter)
  go_declaration(name, type) + " // " + go_comments(parameter, false)
end

#go_parameter_doc(parameter) ⇒ Object



227
228
229
230
231
232
# File 'lib/canvas_api/go_helpers.rb', line 227

def go_parameter_doc(parameter)
  name = parameter["name"]
  out = "# #{go_param_path(parameter)}"
  out << go_comments(parameter)
  out
end

#go_path_params(params) ⇒ Object



481
482
483
# File 'lib/canvas_api/go_helpers.rb', line 481

def go_path_params(params)
  select_params("path", params)
end

#go_primitive(name, type, format) ⇒ Object



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
# File 'lib/canvas_api/go_helpers.rb', line 422

def go_primitive(name, type, format)
  case type
  when "integer"
    if name.end_with?("_ids")
      "[]int64"
    else
      "int64"
    end
  when "number"
    if format == "Float"
      "float64"
    else
      # TODO many of the LMS types with 'number' don't indicate a type so we have to guess
      # Hopefully that changes. For now we go with float
      "float64"
    end
  when "string"
    "string"
  when "boolean"
    "bool"
  when "datetime"
    "time.Time"
  when "date"
    "time.Time"
  else
    raise "Unable to match requested primitive '#{type}' to Go Type."
  end
end

#go_property_type(name, property, return_type = false, model = nil, namespace = "models.") ⇒ Object



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
# File 'lib/canvas_api/go_helpers.rb', line 309

def go_property_type(name, property, return_type = false, model = nil, namespace = "models.")
  return property["type"] if property["keep_type"]
  return property[:array_of] if property[:array_of]

  # Canvas API docs are wrong for these so we HACK in the right type
  return "float64" if name.downcase == "points_possible"

  type = property["type"].downcase
  case type
  when "{success: true}"
    "canvasapi.SuccessResponse"
  when "integer", "string", "boolean", "datetime", "number", "date"
    go_primitive(name, type, property["format"])
  when "void"
    "bool" # TODO this doesn't seem right?
  when "array"
    go_ref_property_type(property, namespace)
  when "object"
    puts "Using string type for '#{name}' ('#{property}') of type object."
    "map[string](interface{})"
  else
    if property["type"] == "array of outcome ids"
      "[]string"
    elsif property["type"] == "list of content items"
      # HACK There's no list of content items object so we return an array of string
      "[]string"
    elsif property["type"].include?('{ "unread_count": "integer" }')
      "canvasapi.UnreadCount"
    elsif return_type
      "*#{namespace}#{struct_name(property["type"])}"
    elsif property["type"] == "Hash"
      "map[string](interface{})"
    elsif property["type"] == "String[]"
      "[]string"
    elsif property["type"] == "[Answer]"
      "[]*models.Answer"
    elsif property["type"] == "QuizUserConversation"
      "canvasapi.QuizUserConversation"
    elsif [
      "Assignment",
      "BlueprintRestriction",
      "RubricAssessment",
    ].include?(property["type"])
      "*models.#{property["type"]}"
    elsif property["type"] == "multiple BlueprintRestrictions"
      "[]*models.BlueprintRestriction"
    elsif property["type"] == "File"
      # This won't work. If we ever need to use this type we'll need to do some refactoring
      "string"
    elsif property["type"] == "Deprecated"
      "string"
    elsif property["type"] == "SerializedHash"
      # Not sure this will work
      "map[string](interface{})"
    elsif property["type"].downcase == "json"
      "map[string](interface{})"
    elsif ["Numeric", "float"].include?(property["type"])
      "float64"
    elsif property["type"] == "GroupMembership | Progress"
      "no-op" # this is handled further up the stack
    elsif property["type"] == "URL"
      "string"
    elsif property["type"] == "uuid"
      "string"
    else
      raise "Unable to match '#{name}' requested property '#{property}' to Go Type."
    end
  end
end

#go_query_params(params) ⇒ Object



485
486
487
# File 'lib/canvas_api/go_helpers.rb', line 485

def go_query_params(params)
  select_params("query", params)
end

#go_ref_property_type(property, namespace) ⇒ Object



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
# File 'lib/canvas_api/go_helpers.rb', line 379

def go_ref_property_type(property, namespace)
  ref_type = property.dig("items", "$ref")
  if ref_type == nil
    if property["type"] == "array"
      "[]string"
    else
      "string"
    end
  elsif ref_type == "Hash"
    # HACK for https://canvas.instructure.com/doc/api/quiz_submissions.html
    if property["name"] == "quiz_submissions[questions]"
      "map[string]QuizSubmissionOverrides"
    else
      raise "No type available for #{property}"
    end
  elsif ref_type == "[Integer]"
    "[]int"
  elsif ref_type == "Array"
    "[]string"
  elsif ref_type == "[String]"
    "[]string"
  elsif ref_type == "DateTime" || ref_type == "Date"
    "[]time.Time"
  elsif ref_type == "object"
    "map[string](interface{})"
  elsif ref_type
    # HACK on https://canvas.instructure.com/doc/api/submissions.json
    # the ref value is set to a full sentence rather than a
    # simple type, so we look for that specific value
    if ref_type.include?("UserDisplay if anonymous grading is not enabled")
      "[]*#{namespace}UserDisplay"
    elsif ref_type.include?("Url String The url to the result that was created")
      "string"
    else
      "[]*#{namespace}#{struct_name(ref_type)}"
    end
  else
    "[]#{go_primitive(name, property["items"]["type"].downcase, property["items"]["format"])}"
  end
rescue
  raise "Unable to discover Go list type for '#{name}' ('#{property}')."
end

#go_render_child_structsObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/canvas_api/go_helpers.rb', line 91

def go_render_child_structs
  out = ""
  @child_structs&.each do |name, params|
    out << "\ntype #{struct_name(name)} struct {"
    params.each do |n, p|
      out << "\n" + go_param_to_field(p, n)
    end
    out << "\n}\n"
  end
  out
end

#go_render_params(nested) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/canvas_api/go_helpers.rb', line 73

def go_render_params(nested)
  out = ""
  nested.each do |name, val|
    if val["paramType"]
      out << "\n" + go_param_to_field(val, name)
    elsif val[:array_of]
      out << "\n#{struct_name(name)} #{val[:array_of]}"
    elsif val[:map_of]
      out << "\n#{struct_name(name)} #{val[:map_of]}"
    else
      out << "\n#{struct_name(name)} struct {"
      out << go_render_params(val)
      out << "\n} `json:\"#{name.underscore.gsub("`", "")}\" url:\"#{name.underscore.gsub("`", "")},omitempty\"`\n"
    end
  end
  out
end

#go_require_models(parameters, nickname, return_type) ⇒ Object



286
287
288
289
290
291
292
293
294
295
# File 'lib/canvas_api/go_helpers.rb', line 286

def go_require_models(parameters, nickname, return_type)
  parameters.any? { |p| go_type(p["name"], p).include?("models") } ||
  ["assign_unassigned_members"].include?(@nickname) ||
  (return_type &&
    return_type != "bool" &&
    !return_type.include?("string") &&
    !return_type.include?("SuccessResponse") &&
    !return_type.include?("UnreadCount")
  )
end

#go_return_type(operation, is_decl = false) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/canvas_api/go_helpers.rb', line 570

def go_return_type(operation, is_decl = false)
  prefix = is_decl ? "" : "*"
  suffix = is_decl ? "{}" : ""
  if operation["type"] == "void"
    nil
  elsif is_paged?(operation)
    model = operation.dig("items", "$ref")
    if model.include?(" ")
      # Handle cases with spaces using go_property_type
      type = go_property_type(operation["nickname"], operation)
      if type == "string"
        type = "[]#{type}"
      end
      "#{type}#{suffix}"
    else
      "[]*models.#{go_name(model)}#{suffix}"
    end
  elsif operation["type"] == "boolean"
    "bool"
  elsif operation["type"] == "integer"
    "int64"
  elsif model = operation["type"]
    if model.include?(" ")
      # Handle cases with spaces using go_property_type
      type = go_property_type(operation["nickname"], operation)
      if type == "string"
        type
      else
        "#{prefix}#{type}#{suffix}"
      end
    else
      "#{prefix}models.#{go_name(model)}#{suffix}"
    end
  else
    raise "No return type found for #{operation}"
  end
end

#go_struct_fields(nickname, params) ⇒ Object

Subject string ‘json:“subject”` // (Required) Message string `json:“message”` // (Required) StartAt time.Time `json:“start_at”` // (Required) EndAt time.Time `json:“end_at”` // (Required) Icon string `json:“icon”` // (Optional) . Must be one of warning, information, question, error, calendar } `json:“account_notification”`



64
65
66
67
68
69
70
71
# File 'lib/canvas_api/go_helpers.rb', line 64

def go_struct_fields(nickname, params)
  nested = {}
  params.each do |p|
    structs, name = split_nested(p)
    go_to_tree(nickname, nested, structs, name, p)
  end
  go_render_params(nested)
end

#go_to_tree(nickname, nested, structs, name, param) ⇒ Object



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
# File 'lib/canvas_api/go_helpers.rb', line 108

def go_to_tree(nickname, nested, structs, name, param)
  @child_structs ||= {}

  # HACK for https://canvas.instructure.com/doc/api/quiz_submissions.html
  if nickname == "update_student_question_scores_and_comments"
    @child_structs[QuizSubmissionOverrides] = {
      "score" => {"name"=>"score", "type"=>"float"},
      "comment" => {"name"=>"comment", "type"=>"string"},
    }
  end

  if structs.length > 0
    struct, rest = structs.first, structs[1..-1]
    nested[struct] ||= {}
    if rest
      if is_x_param?(rest[1])
        type = go_property_type(name, param)
        child_name = rest[0]
        child_struct = "#{struct_name(nickname)}#{struct_name(child_name)}"
        @child_structs[child_struct] ||= {}
        @child_structs[child_struct][name] = param
        nested[struct][child_name] = {
          "name" => "#{struct}[#{child_name}]",
          "type" => "map[string]#{child_struct}",
          "paramType" => param["paramType"],
          "keep_type" => true,
        }
        rest.shift
        rest.shift
      elsif is_x_param?(rest[0])
        if rest[0] == structs[1]
          child_name = structs[0]
          child_struct = "#{struct_name(nickname)}#{struct_name(child_name)}"
          nested[struct][:map_of] = "map[string]#{child_struct}"
          @child_structs[child_struct] ||= {}
          @child_structs[child_struct][name] = param
        else
          type = go_property_type(name, param)
          nested[struct][:map_of] = "map[string]#{type}"
        end
        rest.shift
      end
    end

    if rest && rest.length > 0
      go_to_tree(nickname, nested[struct], rest, name, param)
    else
      if @child_structs && child_struct = nested[struct][:array_of]
        @child_structs[child_struct][name] = param
      else
        nested[struct][name] = param
      end
    end
  else
    nested[name] = param
    if param["type"] == "array" && ["events"].include?(param["name"])
      child_struct = "#{struct_name(nickname)}#{struct_name(param["name"])}"
      @child_structs ||= {}
      @child_structs[child_struct] ||= {}
      nested[name][:array_of] = child_struct
      puts "******** Using custom struct #{child_struct}"
    end
  end
end

#go_type(name, property, return_type = false, model = nil, namespace = "models.") ⇒ Object



301
302
303
304
305
306
307
# File 'lib/canvas_api/go_helpers.rb', line 301

def go_type(name, property, return_type = false, model = nil, namespace = "models.")
  if property["$ref"]
    "*#{namespace}#{struct_name(property['$ref'])}"
  else
    go_property_type(name, property, return_type, model, namespace)
  end
end

#is_array(param) ⇒ Object



198
199
200
# File 'lib/canvas_api/go_helpers.rb', line 198

def is_array(param)
  param["type"] == "array"
end

#is_nested?(param) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/canvas_api/go_helpers.rb', line 194

def is_nested?(param)
  param["name"].include?("[")
end

#is_paged?(operation) ⇒ Boolean

Returns:

  • (Boolean)


510
511
512
# File 'lib/canvas_api/go_helpers.rb', line 510

def is_paged?(operation)
  operation["type"] == "array"
end

#is_required_field(parameter) ⇒ Object



260
261
262
# File 'lib/canvas_api/go_helpers.rb', line 260

def is_required_field(parameter)
  parameter["required"] && !["bool", "int64", "int", "float64"].include?(go_type(parameter["name"], parameter))
end

#is_x_param?(name) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
# File 'lib/canvas_api/go_helpers.rb', line 184

def is_x_param?(name)
  if name
    name.include?("[<X>]") ||
    name.include?("<X>") ||
    name.include?("X") ||
    name.include?("<student_id>") ||
    name.include?("0")
  end
end

#next_param(operation) ⇒ Object



514
515
516
517
518
# File 'lib/canvas_api/go_helpers.rb', line 514

def next_param(operation)
  if is_paged?(operation)
    ", next *url.URL"
  end
end

#select_params(type, parameters) ⇒ Object



493
494
495
496
497
498
499
500
# File 'lib/canvas_api/go_helpers.rb', line 493

def select_params(type, parameters)
  params = parameters.select{|p| p["paramType"] == type}
  if params && !params.nil? && params.length > 0
    params
  else
    nil
  end
end

#split_nested(param) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/canvas_api/go_helpers.rb', line 202

def split_nested(param)
  parts = param["name"].split("[").map{|p| p.gsub("]", "")}
  [
    parts[0...-1],
    parts.last,
  ]
end

#struct_fields(model, resource_name) ⇒ Object



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
# File 'lib/canvas_api/go_helpers.rb', line 3

def struct_fields(model, resource_name)
  if !model["properties"]
    puts "NO properties for #{resource_name} !!!!!!!!!!!!!!!!!!!!!"
    return []
  end

  time_required = false
  fields = model["properties"].map do |name, property|
    description = ""
    description << "#{safe_rb(property['description'].gsub("\n", "\n //"))}." if property["description"].present?
    description << "Example: #{safe_rb(property['example'])}".gsub("..", "").gsub("\n", " ") if property["example"].present?

    # clean up name
    name = nested_arg(name)

    if type = go_type(name, property, false, model, "")
      if type.include? "time.Time"
        time_required = true
      end
      go_declaration(name, type) + " // #{description}"
    else
      raise "Unable to determine type for #{name}"
    end
  end.compact

  [fields, time_required]
end

#struct_name(type) ⇒ Object



280
281
282
283
284
# File 'lib/canvas_api/go_helpers.rb', line 280

def struct_name(type)
  # Remove chars and fix spelling errors
  cleaned = type.split('|').first.strip.gsub(" ", "_")
  go_name(cleaned)
end

#time_required?(parameters) ⇒ Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/canvas_api/go_helpers.rb', line 297

def time_required?(parameters)
  parameters.any? { |p| go_type(p["name"], p).include?("time.Time") }
end