This is not general, but works for us
(schema)
| 420 | |
| 421 | |
| 422 | def generate_return_value(schema): |
| 423 | """This is not general, but works for us""" |
| 424 | schema_type = get_schema_type(schema) |
| 425 | if schema.get('rpc'): |
| 426 | output_title('RETURN VALUE') |
| 427 | elif schema.get('notification'): |
| 428 | output_title('NOTIFICATION PAYLOAD') |
| 429 | elif schema.get('hook'): |
| 430 | output_title('HOOK RETURN') |
| 431 | |
| 432 | response = schema.get('response') |
| 433 | if response is None: |
| 434 | output(f'This {schema_type} does not have a response.\n\n') |
| 435 | return |
| 436 | |
| 437 | if 'pre_return_value_notes' in response: |
| 438 | outputs(response['pre_return_value_notes'], '\n') |
| 439 | output('\n') |
| 440 | |
| 441 | toplevels = [] |
| 442 | warnings = [] |
| 443 | props = response['properties'] |
| 444 | |
| 445 | # We handle warnings on top-level objects with a separate section, |
| 446 | # so collect them now and remove them |
| 447 | for toplevel in list(props.keys()): |
| 448 | if toplevel.startswith('warning'): |
| 449 | warnings.append((toplevel, props[toplevel]['description'])) |
| 450 | del props[toplevel] |
| 451 | else: |
| 452 | toplevels.append(toplevel) |
| 453 | |
| 454 | # No properties -> empty object. |
| 455 | if toplevels == []: |
| 456 | # Use pre/post_return_value_notes with empty properties when dynamic generation of the return value section is not required. |
| 457 | # But to add a custom return value section instead. Example: `commando` commands. |
| 458 | if "pre_return_value_notes" not in response and "post_return_value_notes" not in response: |
| 459 | if schema.get('rpc'): |
| 460 | output('On success, an empty object is returned.\n') |
| 461 | sub = schema |
| 462 | elif len(toplevels) == 1 and props[toplevels[0]]['type'] == 'object': |
| 463 | if schema.get('rpc'): |
| 464 | output('On success, an object containing {} is returned. It is an object containing:\n\n'.format(fmt_propname(toplevels[0]))) |
| 465 | # Don't have a description field here, it's not used. |
| 466 | assert 'description' not in toplevels[0] |
| 467 | sub = props[toplevels[0]] |
| 468 | elif len(toplevels) == 1 and props[toplevels[0]]['type'] == 'array' and props[toplevels[0]]['items']['type'] == 'object': |
| 469 | if schema.get('rpc'): |
| 470 | output('On success, an object containing {} is returned. It is an array of objects, where each object contains:\n\n'.format(fmt_propname(toplevels[0]))) |
| 471 | # Don't have a description field here, it's not used. |
| 472 | assert 'description' not in toplevels[0] |
| 473 | sub = props[toplevels[0]]['items'] |
| 474 | else: |
| 475 | if schema.get('rpc'): |
| 476 | output('On success, an object is returned, containing:\n\n') |
| 477 | sub = response |
| 478 | |
| 479 | output_members(sub) |
no test coverage detected