google-cloud-firestore 2.13.1 → 2.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30cb17b1e946e584cbe565b74143e0051f93bc4666f11a183a0f3f0a541b8a9d
4
- data.tar.gz: 18b71ed2191e4f15e6bc667f8e6825e1c33d66dc3f98a7753536cd87a6dff177
3
+ metadata.gz: 16163a09797c60820b760bb85283dc3ab8532aacc329dbdf0e8672ab7b8e4066
4
+ data.tar.gz: a502101923eaeffb2b8ca9112134ffc14e8971970509cb9f34e5263c5889a791
5
5
  SHA512:
6
- metadata.gz: 539e3c8a868b4ece85550090a53829d615ba519bf4e0eaaea4e7ac5601977be51c5978fc8eb5ac12d2ea4e3e8c4b803c0a6a406111c381d45983516accafb3c0
7
- data.tar.gz: a95df5fec2c114cbdde41bd78b6835664865d09a900b445f2b4deb595a4ba6cd761f777c9ce08746604b7289727d9f36b218033e3bde6261365f6835d4ab3df8
6
+ metadata.gz: 06e1cb717e9ccdb4560f66d38af4845610aaed131557956d5dea7c86a97169c7f7af979e956ef53a7ac3ce36f19ade87c3a00a7f568bcdc2ea82aaf77dfafb77
7
+ data.tar.gz: 388eb390a647550643bdce6c1a0b57bcb98fa9ad63fdb24808971dafeac3342ac6a0943bc86103d892b9a4e572746ec01ddf167f9629ff29df768bedcc606c59
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Release History
2
2
 
3
+ ### 2.15.0 (2024-03-07)
4
+
5
+ #### Features
6
+
7
+ * Update minimum supported Ruby version to 2.7 ([#25298](https://github.com/googleapis/google-cloud-ruby/issues/25298))
8
+
9
+ ### 2.14.0 (2024-01-31)
10
+
11
+ #### Features
12
+
13
+ * Support sum & avg aggregate functions ([#23446](https://github.com/googleapis/google-cloud-ruby/issues/23446))
14
+
3
15
  ### 2.13.1 (2023-06-16)
4
16
 
5
17
  #### Documentation
@@ -67,26 +67,39 @@ module Google
67
67
  attr_reader :parent_path
68
68
 
69
69
  ##
70
- # @private The Google::Cloud::Firestore::V1::StructuredQuery object.
70
+ # @private The Google::Cloud::Firestore::Query object.
71
71
  attr_reader :query
72
72
 
73
73
  ##
74
- # @private Array of Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation objects
75
- attr_reader :aggregates
74
+ # @private Object of type
75
+ # Google::Cloud::Firestore::V1::StructuredAggregationQuery
76
+ attr_reader :grpc
77
+
78
+ # @private
79
+ DEFAULT_COUNT_ALIAS = "count".freeze
80
+
81
+ # @private
82
+ DEFAULT_SUM_ALIAS = "sum".freeze
83
+
84
+ # @private
85
+ DEFAULT_AVG_ALIAS = "avg".freeze
76
86
 
77
87
  ##
78
88
  # @private Creates a new AggregateQuery
79
- def initialize query, parent_path, client, aggregates: []
89
+ def initialize query, parent_path, client
80
90
  @query = query
81
91
  @parent_path = parent_path
82
- @aggregates = aggregates
83
92
  @client = client
93
+ @grpc = Google::Cloud::Firestore::V1::StructuredAggregationQuery.new(
94
+ structured_query: @query.query,
95
+ aggregations: []
96
+ )
84
97
  end
85
98
 
86
99
  ##
87
100
  # Adds a count aggregate.
88
101
  #
89
- # @param [aggregate_alias] Alias to refer to the aggregate. Optional
102
+ # @param aggregate_alias [String] Alias to refer to the aggregate. Optional
90
103
  #
91
104
  # @return [AggregateQuery] A new aggregate query with the added count aggregate.
92
105
  #
@@ -106,13 +119,102 @@ module Google
106
119
  # end
107
120
  #
108
121
  def add_count aggregate_alias: nil
109
- aggregate_alias ||= ALIASES[:count]
110
- new_aggregates = @aggregates.dup
111
- new_aggregates << StructuredAggregationQuery::Aggregation.new(
112
- count: StructuredAggregationQuery::Aggregation::Count.new,
122
+ aggregate_alias ||= DEFAULT_COUNT_ALIAS
123
+ new_aggregate = Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
124
+ count: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Count.new,
125
+ alias: aggregate_alias
126
+ )
127
+
128
+ start new_aggregate
129
+ end
130
+
131
+ ##
132
+ # Adds a sum aggregate.
133
+ #
134
+ # @param field [String] The field to sum by
135
+ # @param aggregate_alias [String] Alias to refer to the aggregate
136
+ #
137
+ # @return [AggregateQuery] A new aggregate query with the added sum aggregate.
138
+ #
139
+ # @example
140
+ # require "google/cloud/firestore"
141
+ #
142
+ # firestore = Google::Cloud::Firestore.new
143
+ #
144
+ # query = firestore.col "cities"
145
+ #
146
+ # # Create an aggregate query
147
+ # aggregate_query = query.aggregate_query
148
+ # .add_sum("population")
149
+ #
150
+ # aggregate_query.get do |aggregate_snapshot|
151
+ # puts aggregate_snapshot.get
152
+ # end
153
+ #
154
+ def add_sum field, aggregate_alias: nil
155
+ aggregate_alias ||= DEFAULT_SUM_ALIAS
156
+ field = FieldPath.parse field unless field.is_a? FieldPath
157
+ new_aggregate = Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
158
+ sum: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Sum.new(
159
+ field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(
160
+ field_path: field.formatted_string
161
+ )
162
+ ),
113
163
  alias: aggregate_alias
114
164
  )
115
- AggregateQuery.start query, new_aggregates, parent_path, client
165
+
166
+ start new_aggregate
167
+ end
168
+
169
+ ##
170
+ # Adds an average aggregate.
171
+ #
172
+ # @param field [String] The field to apply average on
173
+ # @param aggregate_alias [String] Alias to refer to the aggregate
174
+ #
175
+ # @return [AggregateQuery] A new aggregate query with the added average aggregate.
176
+ #
177
+ # @example
178
+ # require "google/cloud/firestore"
179
+ #
180
+ # firestore = Google::Cloud::Firestore.new
181
+ #
182
+ # query = firestore.col "cities"
183
+ #
184
+ # # Create an aggregate query
185
+ # aggregate_query = query.aggregate_query
186
+ # .add_avg("population")
187
+ #
188
+ # aggregate_query.get do |aggregate_snapshot|
189
+ # puts aggregate_snapshot.get
190
+ # end
191
+ #
192
+ def add_avg field, aggregate_alias: nil
193
+ aggregate_alias ||= DEFAULT_AVG_ALIAS
194
+ field = FieldPath.parse field unless field.is_a? FieldPath
195
+ new_aggregate = Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
196
+ avg: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Avg.new(
197
+ field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(
198
+ field_path: field.formatted_string
199
+ )
200
+ ),
201
+ alias: aggregate_alias
202
+ )
203
+
204
+ start new_aggregate
205
+ end
206
+
207
+ ##
208
+ # @private
209
+ def start new_aggregate
210
+ combined_aggregates = [].concat(grpc.aggregations).push(new_aggregate)
211
+ new_grpc = Google::Cloud::Firestore::V1::StructuredAggregationQuery.new(
212
+ structured_query: @query.query,
213
+ aggregations: combined_aggregates
214
+ )
215
+ self.class.new(@query, @parent_path, @client).tap do |aq|
216
+ aq.instance_variable_set :@grpc, new_grpc
217
+ end
116
218
  end
117
219
 
118
220
  ##
@@ -143,7 +245,7 @@ module Google
143
245
 
144
246
  return enum_for :get unless block_given?
145
247
 
146
- responses = service.run_aggregate_query @parent_path, structured_aggregation_query
248
+ responses = service.run_aggregate_query @parent_path, @grpc
147
249
  responses.each do |response|
148
250
  next if response.result.nil?
149
251
  yield AggregateQuerySnapshot.from_run_aggregate_query_response response
@@ -151,32 +253,13 @@ module Google
151
253
  end
152
254
 
153
255
  ##
154
- # @private Creates a Google::Cloud::Firestore::V1::StructuredAggregationQuery object
155
- def structured_aggregation_query
156
- StructuredAggregationQuery.new(
157
- structured_query: @query,
158
- aggregations: @aggregates
159
- )
160
- end
161
-
162
- ##
163
- # @private Start a new AggregateQuery.
164
- def self.start query, aggregates, parent_path, client
165
- new query, parent_path, client, aggregates: aggregates
256
+ # @private
257
+ def to_grpc
258
+ @grpc
166
259
  end
167
260
 
168
261
  protected
169
262
 
170
- ##
171
- # @private
172
- StructuredAggregationQuery = Google::Cloud::Firestore::V1::StructuredAggregationQuery
173
-
174
- ##
175
- # @private
176
- ALIASES = {
177
- count: "count"
178
- }.freeze
179
-
180
263
  ##
181
264
  # @private Raise an error unless a database is available.
182
265
  def ensure_client!
@@ -53,6 +53,19 @@ module Google
53
53
  # puts aggregate_snapshot.get('total')
54
54
  # end
55
55
  class AggregateQuerySnapshot
56
+ ##
57
+ # @private Object of type [Hash{String => Object}]
58
+ #
59
+ # String can have the following values:
60
+ # - an aggregate literal "sum", "avg", or "count"
61
+ # - a custom aggregate alias
62
+ # Object can have the following types:
63
+ # - Integer
64
+ # - Float
65
+ # - nil
66
+ # - NaN
67
+ attr_reader :aggregate_fields
68
+
56
69
  ##
57
70
  # Retrieves the aggregate data.
58
71
  #
@@ -60,7 +73,9 @@ module Google
60
73
  # the aggregate value. For an AggregateQuery with a
61
74
  # single aggregate field, this parameter can be omitted.
62
75
  #
63
- # @return [Integer] The aggregate value.
76
+ # @return [Integer, Float, nil, NaN] The aggregate value.
77
+ # Returns `nil` if the aggregate_alias does not exist.
78
+ # Returns `NaN` if the aggregate field contains one or more NaN values.
64
79
  #
65
80
  # @example
66
81
  # require "google/cloud/firestore"
@@ -104,11 +119,21 @@ module Google
104
119
  # @private New AggregateQuerySnapshot from a
105
120
  # Google::Cloud::Firestore::V1::RunAggregationQueryResponse object.
106
121
  def self.from_run_aggregate_query_response response
122
+ # rubocop:disable Style/MapToHash
107
123
  aggregate_fields = response
108
124
  .result
109
125
  .aggregate_fields
110
- .to_h # convert from protobuf to ruby map
111
- .transform_values { |v| v[:integer_value] }
126
+ .map do |aggregate_alias, value| # convert from protobuf to ruby map
127
+ if value.has_integer_value?
128
+ [aggregate_alias, value.integer_value]
129
+ elsif value.has_double_value?
130
+ [aggregate_alias, value.double_value]
131
+ elsif value.has_null_value?
132
+ [aggregate_alias, nil]
133
+ end
134
+ end
135
+ .to_h
136
+ # rubocop:enable Style/MapToHash
112
137
 
113
138
  new.tap do |s|
114
139
  s.instance_variable_set :@aggregate_fields, aggregate_fields
@@ -381,12 +381,12 @@ module Google
381
381
  return val if val
382
382
  end
383
383
  when Hash
384
- obj.each do |_k, v|
384
+ obj.each_value do |v|
385
385
  val = field_value_nested? v, field_value_type
386
386
  return val if val
387
387
  end
388
388
  end
389
- nil
389
+ nil # rubocop:disable Style/ReturnNilInPredicateMethodDefinition
390
390
  end
391
391
 
392
392
  def remove_field_value_from obj, field_value_type = nil
@@ -535,13 +535,13 @@ module Google
535
535
  ESCAPED_FIELD_PATH = /\A`(.*)`\z/.freeze
536
536
 
537
537
  def build_hash_from_field_paths_and_values pairs
538
- pairs.each do |field_path, _value|
539
- raise ArgumentError unless field_path.is_a? FieldPath
538
+ pairs.each do |pair|
539
+ raise ArgumentError unless pair.first.is_a? FieldPath
540
540
  end
541
541
 
542
542
  dup_hash = {}
543
543
 
544
- pairs.each do |field_path, value|
544
+ pairs.each do |(field_path, value)|
545
545
  tmp_dup = dup_hash
546
546
  last_field = nil
547
547
  field_path.fields.map(&:to_sym).each do |field|
@@ -318,7 +318,7 @@ module Google
318
318
  ##
319
319
  # @private
320
320
  def eql? other
321
- return nil unless other.is_a? DocumentSnapshot
321
+ return false unless other.is_a? DocumentSnapshot
322
322
  return data.eql? other.data if path == other.path
323
323
  path.eql? other.path
324
324
  end
@@ -1018,7 +1018,7 @@ module Google
1018
1018
  # aggregate_query = query.aggregate_query
1019
1019
  #
1020
1020
  def aggregate_query
1021
- AggregateQuery.new query, parent_path, client
1021
+ AggregateQuery.new self, parent_path, client
1022
1022
  end
1023
1023
 
1024
1024
  ##
@@ -305,7 +305,7 @@ module Google
305
305
  return enum_for :get_aggregate, aggregate_query unless block_given?
306
306
 
307
307
  results = service.run_aggregate_query aggregate_query.parent_path,
308
- aggregate_query.structured_aggregation_query,
308
+ aggregate_query.to_grpc,
309
309
  transaction: transaction_or_create
310
310
  results.each do |result|
311
311
  extract_transaction_from_result! result
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Firestore
19
- VERSION = "2.13.1".freeze
19
+ VERSION = "2.15.0".freeze
20
20
  end
21
21
  end
22
22
  end
@@ -84,7 +84,7 @@ module Google
84
84
  transport: nil,
85
85
  project: nil,
86
86
  keyfile: nil
87
- project_id ||= (project || default_project_id)
87
+ project_id ||= project || default_project_id
88
88
  scope ||= configure.scope
89
89
  timeout ||= configure.timeout
90
90
  endpoint ||= configure.endpoint
@@ -101,7 +101,7 @@ module Google
101
101
  return Firestore::Client.new service
102
102
  end
103
103
 
104
- credentials ||= (keyfile || default_credentials(scope: scope))
104
+ credentials ||= keyfile || default_credentials(scope: scope)
105
105
  unless credentials.is_a? Google::Auth::Credentials
106
106
  credentials = Firestore::Credentials.new credentials, scope: scope
107
107
  end
@@ -74,9 +74,10 @@ module Google
74
74
  database_id: nil,
75
75
  transport: nil
76
76
  transport ||= Google::Cloud.configure.firestore.transport
77
+ timeout ||= @timeout
77
78
  Google::Cloud.firestore @project, @keyfile,
78
79
  scope: scope,
79
- timeout: (timeout || @timeout),
80
+ timeout: timeout,
80
81
  database_id: database_id,
81
82
  transport: transport
82
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-firestore
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.13.1
4
+ version: 2.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-16 00:00:00.000000000 Z
11
+ date: 2024-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-core
@@ -66,146 +66,6 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.4.2
69
- - !ruby/object:Gem::Dependency
70
- name: google-style
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.26.1
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.26.1
83
- - !ruby/object:Gem::Dependency
84
- name: minitest
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '5.16'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '5.16'
97
- - !ruby/object:Gem::Dependency
98
- name: minitest-autotest
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '1.0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '1.0'
111
- - !ruby/object:Gem::Dependency
112
- name: minitest-focus
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '1.1'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '1.1'
125
- - !ruby/object:Gem::Dependency
126
- name: minitest-rg
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '5.2'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '5.2'
139
- - !ruby/object:Gem::Dependency
140
- name: autotest-suffix
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - "~>"
144
- - !ruby/object:Gem::Version
145
- version: '1.1'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - "~>"
151
- - !ruby/object:Gem::Version
152
- version: '1.1'
153
- - !ruby/object:Gem::Dependency
154
- name: redcarpet
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - "~>"
158
- - !ruby/object:Gem::Version
159
- version: '3.0'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - "~>"
165
- - !ruby/object:Gem::Version
166
- version: '3.0'
167
- - !ruby/object:Gem::Dependency
168
- name: simplecov
169
- requirement: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - "~>"
172
- - !ruby/object:Gem::Version
173
- version: '0.9'
174
- type: :development
175
- prerelease: false
176
- version_requirements: !ruby/object:Gem::Requirement
177
- requirements:
178
- - - "~>"
179
- - !ruby/object:Gem::Version
180
- version: '0.9'
181
- - !ruby/object:Gem::Dependency
182
- name: yard
183
- requirement: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - "~>"
186
- - !ruby/object:Gem::Version
187
- version: '0.9'
188
- type: :development
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: '0.9'
195
- - !ruby/object:Gem::Dependency
196
- name: yard-doctest
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - "~>"
200
- - !ruby/object:Gem::Version
201
- version: 0.1.13
202
- type: :development
203
- prerelease: false
204
- version_requirements: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - "~>"
207
- - !ruby/object:Gem::Version
208
- version: 0.1.13
209
69
  description: google-cloud-firestore is the official library for Google Cloud Firestore
210
70
  API.
211
71
  email: googleapis-packages@google.com
@@ -276,14 +136,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
276
136
  requirements:
277
137
  - - ">="
278
138
  - !ruby/object:Gem::Version
279
- version: '2.6'
139
+ version: '2.7'
280
140
  required_rubygems_version: !ruby/object:Gem::Requirement
281
141
  requirements:
282
142
  - - ">="
283
143
  - !ruby/object:Gem::Version
284
144
  version: '0'
285
145
  requirements: []
286
- rubygems_version: 3.4.2
146
+ rubygems_version: 3.5.6
287
147
  signing_key:
288
148
  specification_version: 4
289
149
  summary: API Client library for Google Cloud Firestore API