def _get_child(parent, desc, field_name): """Get a child expression. This will get one of the following: A regular field. An extension. An Any filtered by value. A map field. Args: parent: The parent expression. desc: The descriptor of the parent. field_name: The name of the field. Returns: The child expression, either a submessage or a leaf. """ if isinstance(field_name, path.AnonymousId): return None if parse_message_level_ex.is_any_descriptor(desc): return _get_any_child(parent, desc, field_name) if path.is_map_indexing_step(field_name): return _get_map_child(parent, desc, field_name) # Works for extensions and regular fields, but not any or map. return _get_child_helper(parent, _get_field_descriptor(desc, field_name), field_name)
def _get_child( parent: Union[_ProtoChildExpression, _ProtoRootExpression], desc: descriptor.Descriptor, field_name: path.Step, backing_str_tensor: Optional[tf.Tensor], ) -> Optional[Union[_ProtoChildExpression, _ProtoLeafExpression]]: """Get a child expression. This will get one of the following: A regular field. An extension. An Any filtered by value. A map field. Args: parent: The parent expression. desc: The descriptor of the parent. field_name: The name of the field. backing_str_tensor: a string tensor representing the root serialized proto. This is passed to keep string_views of the tensor valid for all children of the root expression Returns: The child expression, either a submessage or a leaf. """ if isinstance(field_name, path.AnonymousId): return None if parse_message_level_ex.is_any_descriptor(desc): return _get_any_child(parent, desc, field_name, backing_str_tensor) if path.is_map_indexing_step(field_name): return _get_map_child(parent, desc, field_name, backing_str_tensor) # Works for extensions and regular fields, but not any or map. return _get_child_helper(parent, _get_field_descriptor(desc, field_name), field_name, backing_str_tensor)
def _get_field_names_to_parse(desc, needed_field_names): """Gets the field names to parse from the original protobuf.""" result = set() # Set[ProtoFieldName] for x in needed_field_names: if path.is_map_indexing_step(x): map_field_name, _ = path.parse_map_indexing_step(x) result.add(map_field_name) elif path.is_extension(x) and is_any_descriptor(desc): result.add("type_url") result.add("value") else: result.add(x) return list(result)
def _get_map_parsed_fields( desc: descriptor.Descriptor, regular_fields: Mapping[StrStep, struct2tensor_ops._ParsedField], field_names: Set[StrStep], backing_str_tensor: Optional[tf.Tensor] = None ) -> Mapping[StrStep, struct2tensor_ops._ParsedField]: """Gets the map proto ParsedFields. field_names includes all the fields: map fields, any fields, and regular fields. Args: desc: the descriptor of the parent proto. regular_fields: the fields that are parsed directly from the proto. field_names: all fields needed: map fields, any fields, and regular fields. backing_str_tensor: a string tensor representing the root serialized proto. This is passed to keep string_views of the tensor valid for all children of the root expression Returns: A map from field names to ParsedFields, only for the field names of the form foo[bar]. """ maps_to_parse = collections.defaultdict(dict) for x in field_names: if path.is_map_indexing_step(x): map_field_name, key = path.parse_map_indexing_step(x) maps_to_parse[map_field_name][key] = x result_as_list = [] for map_field_name, v in maps_to_parse.items(): parsed_map_field = regular_fields[map_field_name] keys_needed = list(v.keys()) map_field_value = parsed_map_field.value map_field_index = parsed_map_field.index map_field_desc = desc.fields_by_name[map_field_name].message_type values_and_parent_indices = struct2tensor_ops.parse_proto_map( map_field_value, map_field_index, map_field_desc, keys_needed, backing_str_tensor) for map_key, [value, parent_index] in zip(keys_needed, values_and_parent_indices): result_as_list.append( struct2tensor_ops._ParsedField(field_name=v[map_key], field_descriptor=None, index=parent_index, value=value)) return {x.field_name: x for x in result_as_list}
def _get_map_parsed_fields(desc, regular_fields, field_names): """Gets the map proto ParsedFields. field_names includes all the fields: map fields, any fields, and regular fields. Args: desc: the descriptor of the parent proto. regular_fields: the fields that are parsed directly from the proto. field_names: all fields needed: map fields, any fields, and regular fields. Returns: A map from field names to ParsedFields, only for the field names of the form foo[bar]. """ maps_to_parse = collections.defaultdict(dict) for x in field_names: if path.is_map_indexing_step(x): map_field_name, key = path.parse_map_indexing_step(x) maps_to_parse[map_field_name][key] = x result_as_list = [] for map_field_name, v in maps_to_parse.items(): parsed_map_field = regular_fields[map_field_name] keys_needed = list(v.keys()) map_field_value = parsed_map_field.value map_field_index = parsed_map_field.index map_field_desc = desc.fields_by_name[map_field_name].message_type values_and_parent_indices = struct2tensor_ops.parse_proto_map( map_field_value, map_field_index, map_field_desc, keys_needed) for map_key, [value, parent_index] in zip(keys_needed, values_and_parent_indices): result_as_list.append( struct2tensor_ops._ParsedField(field_name=v[map_key], field_descriptor=None, index=parent_index, value=value)) return {x.field_name: x for x in result_as_list}