|
5 | 5 | # --------------------------------------------------------------------------------------
|
6 | 6 |
|
7 | 7 |
|
8 |
| -from base64 import b64decode |
| 8 | +from datetime import datetime |
| 9 | +from base64 import b64encode, b64decode |
| 10 | +from google.protobuf.json_format import MessageToDict |
9 | 11 | from google.cloud.firestore_v1._helpers import GeoPoint
|
10 | 12 | from google.api_core.datetime_helpers import DatetimeWithNanoseconds
|
11 | 13 |
|
@@ -92,3 +94,96 @@ def _decode_datastore(value):
|
92 | 94 |
|
93 | 95 | else:
|
94 | 96 | raise TypeError("Cannot convert to a Python Value", value, "Invalid type", type(value))
|
| 97 | + |
| 98 | + |
| 99 | +def _to_datastore(data): |
| 100 | + """ Converts a Python dictionary ``data``-s to map of Firestore. |
| 101 | +
|
| 102 | +
|
| 103 | + :type data: dict |
| 104 | + :param data: A Python dictionary containing data. |
| 105 | +
|
| 106 | +
|
| 107 | + :return: A map of Firebase values converted from the ``data``. |
| 108 | + :rtype: dict |
| 109 | +
|
| 110 | + :raises ValueError: Raised when a key in the python dictionary is |
| 111 | + Non-alphanum char without *`* (ticks) at start and end. |
| 112 | + """ |
| 113 | + |
| 114 | + restructured_data = {} |
| 115 | + |
| 116 | + for key, val in data.items(): |
| 117 | + |
| 118 | + if ' ' in key and (not key.startswith('`') or not key.endswith('`')): |
| 119 | + raise ValueError(f'Non-alphanum char in element with leading alpha: {key}') |
| 120 | + |
| 121 | + key = str(key) |
| 122 | + |
| 123 | + if isinstance(val, dict): |
| 124 | + restructured_data[key] = {'mapValue': _to_datastore(val)} |
| 125 | + |
| 126 | + elif isinstance(val, list): |
| 127 | + arr = [] |
| 128 | + |
| 129 | + for x in val: |
| 130 | + arr.append(_encode_datastore_value(x)) |
| 131 | + |
| 132 | + restructured_data[key] = {'arrayValue': {'values': arr}} |
| 133 | + |
| 134 | + else: |
| 135 | + restructured_data[key] = _encode_datastore_value(val) |
| 136 | + |
| 137 | + return {'fields': restructured_data} |
| 138 | + |
| 139 | + |
| 140 | +def _encode_datastore_value(value): |
| 141 | + """ Converts a Python ``value`` to a Firebase value. |
| 142 | +
|
| 143 | +
|
| 144 | + :type value: :data:`None` or :class:`bool` or :class:`bytes` |
| 145 | + or :class:`int` or :class:`float` or :class:`str` or |
| 146 | + :class:`dict` or :class:`~datetime.datetime` or |
| 147 | + :class:`~google.api_core.datetime_helpers.DatetimeWithNanoseconds` |
| 148 | + or :class:`~google.cloud.firestore_v1._helpers.GeoPoint`. |
| 149 | + :param value: A Python data to be encoded/converted to Firebase. |
| 150 | +
|
| 151 | +
|
| 152 | + :return: A Firebase value converted from ``value``. |
| 153 | + :rtype: dict |
| 154 | +
|
| 155 | + :raises TypeError: Raised when unsupported data type given. |
| 156 | + """ |
| 157 | + |
| 158 | + if value is None: |
| 159 | + return {'nullValue': value} |
| 160 | + |
| 161 | + elif isinstance(value, bytes): |
| 162 | + return {'bytesValue': b64encode(value).decode('utf-8')} |
| 163 | + |
| 164 | + elif isinstance(value, bool): |
| 165 | + return {'booleanValue': value} |
| 166 | + |
| 167 | + elif isinstance(value, int): |
| 168 | + return {'integerValue': value} |
| 169 | + |
| 170 | + elif isinstance(value, float): |
| 171 | + return {'doubleValue': value} |
| 172 | + |
| 173 | + elif isinstance(value, str): |
| 174 | + return {'stringValue': value} |
| 175 | + |
| 176 | + elif isinstance(value, dict): |
| 177 | + return {'mapValue': _to_datastore(value)} |
| 178 | + |
| 179 | + elif isinstance(value, datetime): |
| 180 | + return {'timestampValue': value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")} |
| 181 | + |
| 182 | + elif isinstance(value, DatetimeWithNanoseconds): |
| 183 | + return {'timestampValue': value.rfc3339()} |
| 184 | + |
| 185 | + elif isinstance(value, GeoPoint): |
| 186 | + return {'geoPointValue': MessageToDict(value.to_protobuf())} |
| 187 | + else: |
| 188 | + |
| 189 | + raise TypeError("Cannot convert to a Firestore Value", value, "Invalid type", type(value)) |
0 commit comments