Prints a Javascript class for the given proto message. This method emits a Javascript class (Closure-style) for the given proto message to sys.stdout. Args: descriptor: The descriptor module from the protobuf package, e.g. google.protobuf.descriptor. msg_desc: The descriptor
(descriptor, msg_desc, out)
| 372 | |
| 373 | |
| 374 | def PrintClassFor(descriptor, msg_desc, out): |
| 375 | """Prints a Javascript class for the given proto message. |
| 376 | |
| 377 | This method emits a Javascript class (Closure-style) for the given |
| 378 | proto message to sys.stdout. |
| 379 | |
| 380 | Args: |
| 381 | descriptor: The descriptor module from the protobuf package, e.g. |
| 382 | google.protobuf.descriptor. |
| 383 | msg_desc: The descriptor for a particular message type. |
| 384 | out: a list of lines to output (without the newline characters) wrapped as |
| 385 | an OutputFormatter instance, to which this function will append. |
| 386 | """ |
| 387 | constructor_arg_fields = [] |
| 388 | constructor_arg_field_names = {} |
| 389 | for field in msg_desc.fields: |
| 390 | if field.full_name in CONSTRUCTOR_ARG_FIELDS: |
| 391 | constructor_arg_fields.append(field) |
| 392 | constructor_arg_field_names[field.name] = 1 |
| 393 | out.Line('/**') |
| 394 | for field in constructor_arg_fields: |
| 395 | out.Line(' * @param {%s} %s' % |
| 396 | (LocalModuleName(FieldTypeFor(descriptor, field, nullable=False)), |
| 397 | UnderscoreToCamelCase(field.name))) |
| 398 | out.Line(' * @constructor') |
| 399 | out.Line(' * @struct') |
| 400 | out.Line(' */') |
| 401 | arguments = ','.join( |
| 402 | [UnderscoreToCamelCase(f.name) for f in constructor_arg_fields]) |
| 403 | out.Line('const %s = function(%s) {' % |
| 404 | (LocalModuleName(msg_desc.full_name), arguments)) |
| 405 | out.PushIndent(2) |
| 406 | |
| 407 | for field in msg_desc.fields: |
| 408 | # We generate ValidatorRules.directAttrLists, ValidatorRules.globalAttrs, |
| 409 | # and validator.ampLayoutAttrs instead. |
| 410 | if field.full_name == 'amp.validator.ValidatorRules.attr_lists': |
| 411 | continue |
| 412 | assigned_value = 'null' |
| 413 | if field.name in constructor_arg_field_names: |
| 414 | # field.name is also the parameter name. |
| 415 | assigned_value = UnderscoreToCamelCase(field.name) |
| 416 | elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED: |
| 417 | # ValidationResult instances may be mutated by validator.js, |
| 418 | # so we can't share the empty arrays. But for all other |
| 419 | # instances, we do share. |
| 420 | if msg_desc.full_name == 'amp.validator.ValidationResult': |
| 421 | assigned_value = '[]' |
| 422 | else: |
| 423 | assigned_value = 'EMPTY_%s_ARRAY' % ( |
| 424 | ElementTypeFor(descriptor, field).replace('.', '_')) |
| 425 | elif field.type == descriptor.FieldDescriptor.TYPE_BOOL: |
| 426 | assigned_value = str(field.default_value).lower() |
| 427 | elif field.type == descriptor.FieldDescriptor.TYPE_INT32: |
| 428 | assigned_value = str(field.default_value) |
| 429 | # TODO(johannes): Increase coverage for default values, e.g. enums. |
| 430 | type_name = FieldTypeFor( |
| 431 | descriptor, field, nullable=assigned_value == 'null') |
no test coverage detected