Helper function for PrintObject: computes / assigns a value for a field. Note that if the field is a complex field (a message), this function may print the message and then reference it via a variable name. Args: descriptor: The descriptor module from the protobuf package, e.g. g
(descriptor, field_desc, field_val, registry, out)
| 553 | |
| 554 | |
| 555 | def AssignedValueFor(descriptor, field_desc, field_val, registry, out): |
| 556 | """Helper function for PrintObject: computes / assigns a value for a field. |
| 557 | |
| 558 | Note that if the field is a complex field (a message), this function |
| 559 | may print the message and then reference it via a variable name. |
| 560 | |
| 561 | Args: |
| 562 | descriptor: The descriptor module from the protobuf package, e.g. |
| 563 | google.protobuf.descriptor. |
| 564 | field_desc: The descriptor for a particular field. |
| 565 | field_val: The value for a particular field. |
| 566 | registry: an instance of MessageRegistry, used for mapping from |
| 567 | messages to message keys. |
| 568 | out: a list of lines to output (without the newline characters) wrapped as |
| 569 | an OutputFormatter instance, to which this function will append. |
| 570 | Returns: |
| 571 | The rendered field value to assign. |
| 572 | """ |
| 573 | # First we establish how an individual value for this field is going |
| 574 | # to be rendered, that is, converted into a string. |
| 575 | render_value = lambda: None |
| 576 | if field_desc.full_name in TAG_SPEC_NAME_REFERENCE_FIELD: |
| 577 | render_value = lambda v: str(registry.MessageIdForTagSpecName(v)) |
| 578 | elif field_desc.full_name in ATTR_LIST_NAME_REFERENCE_FIELD: |
| 579 | render_value = lambda v: str(registry.MessageIdForAttrListName(v)) |
| 580 | elif field_desc.full_name in SYNTHETIC_REFERENCE_FIELD: |
| 581 | |
| 582 | def InternOrReference(value): |
| 583 | if field_desc.type == descriptor.FieldDescriptor.TYPE_STRING: |
| 584 | return str(registry.InternString(value)) |
| 585 | if IsTrivialAttrSpec(value): |
| 586 | return str(registry.InternString(value.name)) |
| 587 | return str(registry.MessageIdForKey(MessageKey(value))) |
| 588 | |
| 589 | render_value = InternOrReference |
| 590 | elif field_desc.type == descriptor.FieldDescriptor.TYPE_MESSAGE: |
| 591 | render_value = ( |
| 592 | lambda v: MaybePrintMessageValue(descriptor, v, registry, out)) |
| 593 | else: |
| 594 | render_value = ( |
| 595 | lambda v: ImportModuleName(ValueToString(descriptor, field_desc, v))) # pylint: disable=cell-var-from-loop |
| 596 | |
| 597 | # Then we iterate over the field if it's repeated, or else just |
| 598 | # call the render function once. |
| 599 | if field_desc.label == descriptor.FieldDescriptor.LABEL_REPEATED: |
| 600 | elements = [render_value(v) for v in field_val] |
| 601 | return '[%s]' % ','.join(elements) |
| 602 | return render_value(field_val) |
| 603 | |
| 604 | |
| 605 | def PrintObject(descriptor, msg, registry, out): |
no test coverage detected