Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions firestore/google/cloud/firestore_v1beta1/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
grpc.StatusCode.ALREADY_EXISTS: exceptions.Conflict,
grpc.StatusCode.NOT_FOUND: exceptions.NotFound,
}
_UNESCAPED_FIELD_NAME_RE = re.compile('^[_a-zA-Z][_a-zA-Z0-9]*$')


class GeoPoint(object):
Expand Down Expand Up @@ -123,7 +122,7 @@ class FieldPath(object):
parts: (one or more strings)
Indicating path of the key to be used.
"""
simple_field_name = re.compile(r'[A-Za-z_][A-Za-z_0-9]*')
simple_field_name = re.compile('^[_a-zA-Z][_a-zA-Z0-9]*$')

def __init__(self, *parts):
for part in parts:
Expand Down Expand Up @@ -161,15 +160,14 @@ def to_api_repr(self):
within this FieldPath conforming to the Firestore API
specification
"""
ans = []
api_repr = []
for part in self.parts:
match = re.match(self.simple_field_name, part)
if match:
ans.append(part)
if re.match(self.simple_field_name, part):
api_repr.append(part)
else:
replaced = part.replace('\\', '\\\\').replace('`', '\\`')
ans.append('`' + replaced + '`')
return '.'.join(ans)
api_repr.append('`' + replaced + '`')
return '.'.join(api_repr)

def __hash__(self):
return hash(self.to_api_repr())
Expand Down Expand Up @@ -899,7 +897,7 @@ def pbs_for_set(document_path, document_data, option):


def canonicalize_field_paths(field_paths):
"""Converts simple field path with integer beginnings to quoted field path
"""Converts non-simple field paths to quoted field paths

Args:
field_paths (Sequence[str]): A list of field paths
Expand All @@ -914,20 +912,7 @@ def canonicalize_field_paths(field_paths):

.. _Document: https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.Document # NOQA
"""
canonical_strings = []
for field_path in field_paths:
escaped_names = []
field_names = field_path.split('.')
for field_name in field_names:
if re.match(_UNESCAPED_FIELD_NAME_RE, field_name):
escaped_name = field_name
else:
escaped_name = u"`{}`".format(
field_name.replace('\\', '\\\\').replace('`', '``'))
escaped_names.append(escaped_name)
new_field_path = '.'.join(escaped_names)
canonical_strings.append(new_field_path)
return canonical_strings
return [FieldPath.from_string(path).to_api_repr() for path in field_paths]


def pbs_for_update(client, document_path, field_updates, option):
Expand Down