30 """Clears all non-duration submessages that are not in one_ofs.
32 A message is considered `empty` if:
33 * every non-optional scalar fields has its default value,
34 * every optional scalar field is unset,
35 * every repeated/map fields is empty
36 * every oneof is unset,
37 * every duration field is unset
38 * all other message fields (singular, not oneof, not duration) are `empty`.
39 This function clears all `empty` fields from `message`.
41 This is useful for testing.
44 protobuf_message: The Message object to clear.
46 for field, value
in protobuf_message.ListFields():
47 if field.type != field.TYPE_MESSAGE:
49 if field.label == field.LABEL_REPEATED:
56 field.message_type.has_options
57 and field.message_type.GetOptions().map_entry
60 field.message_type.fields_by_number[2].type
61 == descriptor.FieldDescriptor.TYPE_MESSAGE
63 for item
in value.values():
74 not value.ListFields()
75 and field.message_type != duration_pb2.Duration.DESCRIPTOR
76 and field.containing_oneof
is None
78 protobuf_message.ClearField(field.name)