diff --git a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java index af549f21b6ca..325d7ee3b0fb 100644 --- a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java +++ b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java @@ -195,11 +195,6 @@ public boolean exists() { return fields != null; } - /** Checks whether this DocumentSnapshot contains any fields. */ - boolean isEmpty() { - return fields == null || fields.isEmpty(); - } - /** * Returns the fields of the document as a Map or null if the document doesn't exist. Field values * will be converted to their native Java representation. @@ -428,6 +423,15 @@ public DocumentReference getReference() { return docRef; } + /** Checks whether this DocumentSnapshot contains any fields. */ + boolean isEmpty() { + return fields == null || fields.isEmpty(); + } + + Map getProtoFields() { + return fields; + } + Write.Builder toPb() { Preconditions.checkState(exists(), "Can't call toDocument() on a document that doesn't exist"); Write.Builder write = Write.newBuilder(); diff --git a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/Internal.java b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/Internal.java new file mode 100644 index 000000000000..ba9ed85655f1 --- /dev/null +++ b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/Internal.java @@ -0,0 +1,62 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.google.cloud.firestore; + +import static com.google.cloud.firestore.UserDataConverter.NO_DELETES; + +import com.google.api.core.InternalApi; +import com.google.cloud.Timestamp; +import com.google.common.base.Preconditions; +import com.google.firestore.v1.Document; +import com.google.firestore.v1.Value; +import java.util.Map; + +@InternalApi +public class Internal { + private FirestoreImpl firestore; + + @InternalApi + public Internal(FirestoreImpl firestore) { + this.firestore = firestore; + } + + @InternalApi + public DocumentSnapshot snapshotFromObject(String documentPath, Object pojo) { + DocumentReference documentReference = firestore.document(documentPath); + Object data = CustomClassMapper.convertToPlainJavaTypes(pojo); + Preconditions.checkArgument( + data instanceof Map, "Can't create a document from an array or primitive"); + return DocumentSnapshot.fromObject( + firestore, documentReference, (Map) data, NO_DELETES); + } + + @InternalApi + public DocumentSnapshot snapshotFromMap(String documentPath, Map data) { + DocumentReference documentReference = firestore.document(documentPath); + return DocumentSnapshot.fromObject(firestore, documentReference, data, NO_DELETES); + } + + @InternalApi + public DocumentSnapshot snapshotFromProto(Timestamp readTime, Document document) { + return DocumentSnapshot.fromDocument(firestore, readTime, document); + } + + @InternalApi + public Map protoFromSnapshot(DocumentSnapshot snapshot) { + return snapshot.getProtoFields(); + } +}