Skip to content

Commit

Permalink
fix(firestore): Use client instead of service in DocumentReference::List
Browse files Browse the repository at this point in the history
pr: #4561
fixes: #4511
  • Loading branch information
quartzmo authored Jan 8, 2020
1 parent ba5a0c7 commit 3eafb75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
doc_ref = root_col.doc # no id, will create random id instead

doc_ref.must_be_kind_of Google::Cloud::Firestore::DocumentReference
doc_ref.client.must_be_kind_of Google::Cloud::Firestore::Client
doc_ref.document_id.length.must_equal 20 # random doc id

doc_ref.parent.collection_path.must_equal root_col.collection_path
Expand All @@ -35,6 +36,7 @@
it "has add method" do
doc_ref = root_col.add({ foo: "hello world" })
doc_ref.must_be_kind_of Google::Cloud::Firestore::DocumentReference
doc_ref.client.must_be_kind_of Google::Cloud::Firestore::Client

doc_snp = doc_ref.get
doc_snp.must_be_kind_of Google::Cloud::Firestore::DocumentSnapshot
Expand All @@ -51,8 +53,12 @@
docs.must_be_kind_of Array
docs.size.must_be :>, 1
docs.first.must_be_kind_of Google::Cloud::Firestore::DocumentReference
docs.first.client.must_be_kind_of Google::Cloud::Firestore::Client

docs_max_1 = rand_col.list_documents max: 1
docs_max_1.size.must_equal 1

rand_col.list_documents.map(&:delete)
rand_col.list_documents.must_be :empty?
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def list_documents parent, collection_id, token: nil, max: nil
ensure_service!
grpc = service.list_documents \
parent, collection_id, token: token, max: max
DocumentReference::List.from_grpc grpc, service, parent, collection_id
DocumentReference::List.from_grpc grpc, self, parent, collection_id
end

protected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def next?
# end
def next
return nil unless next?
ensure_service!
ensure_client!
options = { token: token, max: @max }
grpc = @service.list_documents @parent, @collection_id, options
self.class.from_grpc grpc, @service, @parent, @collection_id, @max
grpc = @client.service.list_documents @parent, @collection_id, options
self.class.from_grpc grpc, @client, @parent, @collection_id, @max
end

##
Expand Down Expand Up @@ -163,26 +163,26 @@ def all request_limit: nil
##
# @private New DocumentReference::List from a
# Google::Firestore::V1::ListDocumentsResponse object.
def self.from_grpc grpc, service, parent, collection_id, max = nil
def self.from_grpc grpc, client, parent, collection_id, max = nil
documents = List.new(Array(grpc.documents).map do |document|
DocumentReference.from_path document.name, service
DocumentReference.from_path document.name, client
end)
documents.instance_variable_set :@parent, parent
documents.instance_variable_set :@collection_id, collection_id
token = grpc.next_page_token
token = nil if token == "".freeze
documents.instance_variable_set :@token, token
documents.instance_variable_set :@service, service
documents.instance_variable_set :@client, client
documents.instance_variable_set :@max, max
documents
end

protected

##
# Raise an error unless an active service is available.
def ensure_service!
raise "Must have active connection" unless @service
# Raise an error unless an active client is available.
def ensure_client!
raise "Must have active connection" unless @client
end
end
end
Expand Down

0 comments on commit 3eafb75

Please sign in to comment.