MCPcopy Create free account
hub / github.com/ampproject/amphtml / PrintObject

Function PrintObject

validator/validator_gen_js.py:605–675  ·  view source on GitHub ↗

Prints an object, by recursively constructing it. This routine emits Javascript which will construct an object modeling the provided message (in practice the ValidatorRules message). It references the classes and enums enitted by PrintClassFor and PrintEnumFor. Args: descriptor: The de

(descriptor, msg, registry, out)

Source from the content-addressed store, hash-verified

603
604
605def PrintObject(descriptor, msg, registry, out):
606 """Prints an object, by recursively constructing it.
607
608 This routine emits Javascript which will construct an object modeling
609 the provided message (in practice the ValidatorRules message).
610 It references the classes and enums enitted by PrintClassFor and PrintEnumFor.
611
612 Args:
613 descriptor: The descriptor module from the protobuf package, e.g.
614 google.protobuf.descriptor.
615 msg: A protocol message instance.
616 registry: an instance of MessageRegistry, used for mapping from
617 messages to message keys.
618 out: a list of lines to output (without the newline characters) wrapped as
619 an OutputFormatter instance, to which this function will append.
620 Returns:
621 This object's object id, that is, the consumed variable for creating
622 objects.
623 """
624 this_message_key = MessageKey(msg)
625 registry.MarkPrinted(this_message_key)
626
627 field_and_assigned_values = []
628 for (field_desc, field_val) in msg.ListFields():
629 # We generate ValidatorRules.directAttrLists, ValidatorRules.globalAttrs,
630 # and validator.ampLayoutAttrs instead.
631 if field_desc.full_name == 'amp.validator.ValidatorRules.attr_lists':
632 continue
633 field_and_assigned_values.append(
634 (field_desc,
635 AssignedValueFor(descriptor, field_desc, field_val, registry, out)))
636
637 # Constructor with the appropriate arguments.
638 constructor_arg_values = [
639 value for (field, value) in field_and_assigned_values
640 if field.full_name in CONSTRUCTOR_ARG_FIELDS
641 ]
642
643 this_message_reference = registry.MessageReferenceForKey(this_message_key)
644
645 # Construct object field values.
646 fields = []
647 fields_string = ''
648 for (field, value) in field_and_assigned_values:
649 if field.full_name in CONSTRUCTOR_ARG_FIELDS:
650 continue
651 fields.append('%s : %s' % (UnderscoreToCamelCase(field.name), value))
652
653 # Construct the object with object literal field assignment. Rather than
654 # assignment via dot notation, this is more concise and helps reduce the size
655 # of the binary. We also use Object.assign as to not blow away fields that are
656 # set constructor instantiation.
657 if fields:
658 fields_string = '{' + ','.join(fields) + '}'
659 out.Line(
660 'let %s = /** @type {!%s} */ (oa(new %s(%s), %s));' %
661 (this_message_reference, ImportModuleName(msg.DESCRIPTOR.full_name),
662 ImportModuleName(msg.DESCRIPTOR.full_name),

Callers 2

MaybePrintMessageValueFunction · 0.85

Calls 9

MessageKeyClass · 0.85
AssignedValueForFunction · 0.85
UnderscoreToCamelCaseFunction · 0.85
ImportModuleNameFunction · 0.85
MarkPrintedMethod · 0.80
LineMethod · 0.80
InternStringMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected