/
transforms.py
151 lines (103 loc) · 4.58 KB
/
transforms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright 2017 Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Helpful constants to use for Google Cloud Firestore."""
class Sentinel(object):
"""Sentinel objects used to signal special handling."""
__slots__ = ("description",)
def __init__(self, description) -> None:
self.description = description
def __repr__(self):
return "Sentinel: {}".format(self.description)
DELETE_FIELD = Sentinel("Value used to delete a field in a document.")
SERVER_TIMESTAMP = Sentinel(
"Value used to set a document field to the server timestamp."
)
class _ValueList(object):
"""Read-only list of values.
Args:
values (List | Tuple): values held in the helper.
"""
slots = ("_values",)
def __init__(self, values) -> None:
if not isinstance(values, (list, tuple)):
raise ValueError("'values' must be a list or tuple.")
if len(values) == 0:
raise ValueError("'values' must be non-empty.")
self._values = list(values)
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self._values == other._values
@property
def values(self):
"""Values to append.
Returns (List):
values to be appended by the transform.
"""
return self._values
class ArrayUnion(_ValueList):
"""Field transform: appends missing values to an array field.
See:
https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.append_missing_elements
Args:
values (List | Tuple): values to append.
"""
class ArrayRemove(_ValueList):
"""Field transform: remove values from an array field.
See:
https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.remove_all_from_array
Args:
values (List | Tuple): values to remove.
"""
class _NumericValue(object):
"""Hold a single integer / float value.
Args:
value (int | float): value held in the helper.
"""
def __init__(self, value) -> None:
if not isinstance(value, (int, float)):
raise ValueError("Pass an integer / float value.")
self._value = value
@property
def value(self):
"""Value used by the transform.
Returns:
(Integer | Float) value passed in the constructor.
"""
return self._value
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self._value == other._value
class Increment(_NumericValue):
"""Field transform: increment a numeric field with specified value.
See:
https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.increment
Args:
value (int | float): value used to increment the field.
"""
class Maximum(_NumericValue):
"""Field transform: bound numeric field with specified value.
See:
https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.maximum
Args:
value (int | float): value used to bound the field.
"""
class Minimum(_NumericValue):
"""Field transform: bound numeric field with specified value.
See:
https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#google.firestore.v1.DocumentTransform.FieldTransform.FIELDS.google.firestore.v1.ArrayValue.google.firestore.v1.DocumentTransform.FieldTransform.minimum
Args:
value (int | float): value used to bound the field.
"""