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