@@ -115,6 +115,37 @@ def _get_meaning(value_pb, is_list=False):
115115 return meaning
116116
117117
118+ def _new_value_pb (entity_pb , name ):
119+ """Add (by name) a new ``Value`` protobuf to an entity protobuf.
120+
121+ :type entity_pb: :class:`gcloud.datastore._generated.entity_pb2.Entity`
122+ :param entity_pb: An entity protobuf to add a new property to.
123+
124+ :type name: string
125+ :param name: The name of the new property.
126+
127+ :rtype: :class:`gcloud.datastore._generated.entity_pb2.Value`
128+ :returns: The new ``Value`` protobuf that was added to the entity.
129+ """
130+ property_pb = entity_pb .property .add ()
131+ property_pb .name = name
132+ return property_pb .value
133+
134+
135+ def _property_tuples (entity_pb ):
136+ """Iterator of name, ``Value`` tuples from entity properties.
137+
138+ :type entity_pb: :class:`gcloud.datastore._generated.entity_pb2.Entity`
139+ :param entity_pb: An entity protobuf to add a new property to.
140+
141+ :rtype: :class:`generator`
142+ :returns: An iterator that yields tuples of a name and ``Value``
143+ corresponding to properties on the entity.
144+ """
145+ for property_pb in entity_pb .property :
146+ yield property_pb .name , property_pb .value
147+
148+
118149def entity_from_protobuf (pb ):
119150 """Factory method for creating an entity based on a protobuf.
120151
@@ -135,30 +166,29 @@ def entity_from_protobuf(pb):
135166 entity_meanings = {}
136167 exclude_from_indexes = []
137168
138- for property_pb in pb .property :
139- value = _get_value_from_value_pb (property_pb .value )
140- prop_name = property_pb .name
169+ for prop_name , value_pb in _property_tuples (pb ):
170+ value = _get_value_from_value_pb (value_pb )
141171 entity_props [prop_name ] = value
142172
143173 # Check if the property has an associated meaning.
144- meaning = _get_meaning ( property_pb . value ,
145- is_list = isinstance ( value , list ) )
174+ is_list = isinstance ( value , list )
175+ meaning = _get_meaning ( value_pb , is_list = is_list )
146176 if meaning is not None :
147177 entity_meanings [prop_name ] = (meaning , value )
148178
149- # Check if property_pb.value was indexed. Lists need to be
150- # special-cased and we require all `indexed` values in a list agree.
151- if isinstance ( value , list ) :
179+ # Check if ``value_pb`` was indexed. Lists need to be special-cased
180+ # and we require all `` indexed` ` values in a list agree.
181+ if is_list :
152182 indexed_values = set (value_pb .indexed
153- for value_pb in property_pb . value .list_value )
183+ for value_pb in value_pb .list_value )
154184 if len (indexed_values ) != 1 :
155185 raise ValueError ('For a list_value, subvalues must either all '
156186 'be indexed or all excluded from indexes.' )
157187
158188 if not indexed_values .pop ():
159189 exclude_from_indexes .append (prop_name )
160190 else :
161- if not property_pb . value .indexed :
191+ if not value_pb .indexed :
162192 exclude_from_indexes .append (prop_name )
163193
164194 entity = Entity (key = key , exclude_from_indexes = exclude_from_indexes )
@@ -186,19 +216,16 @@ def entity_to_protobuf(entity):
186216 if value_is_list and len (value ) == 0 :
187217 continue
188218
189- prop = entity_pb .property .add ()
190- # Set the name of the property.
191- prop .name = name
192-
219+ value_pb = _new_value_pb (entity_pb , name )
193220 # Set the appropriate value.
194- _set_protobuf_value (prop . value , value )
221+ _set_protobuf_value (value_pb , value )
195222
196223 # Add index information to protobuf.
197224 if name in entity .exclude_from_indexes :
198225 if not value_is_list :
199- prop . value .indexed = False
226+ value_pb .indexed = False
200227
201- for sub_value in prop . value .list_value :
228+ for sub_value in value_pb .list_value :
202229 sub_value .indexed = False
203230
204231 # Add meaning information to protobuf.
@@ -209,10 +236,10 @@ def entity_to_protobuf(entity):
209236 if orig_value is value :
210237 # For lists, we set meaning on each sub-element.
211238 if value_is_list :
212- for sub_value_pb in prop . value .list_value :
239+ for sub_value_pb in value_pb .list_value :
213240 sub_value_pb .meaning = meaning
214241 else :
215- prop . value .meaning = meaning
242+ value_pb .meaning = meaning
216243
217244 return entity_pb
218245
0 commit comments