New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
firestore: Set method zeros out serverTimestamp fields unexpectedly #1171
Comments
I think this is WAI. See https://godoc.org/cloud.google.com/go/firestore:
Note that When it is set, though, its behavior seems to be to unset the value in the backend. I'm not entirely sure if this is WAI as a way to unset the field (since the zero value sets it)... @jba do you know? |
@jadekler I don't think firestore should unset the value in this case. The documentation says My understanding of firestore's lower level API, has no concept of a "server timestamp field". Rather, server timestamp is a type of transform that can be applied to any timestamp field as part of the write transaction. My theory is that this behavior is due to a possible bug in
I'm not sure what you mean by this. I'm not referencing |
Sorry, I wasn't very clear. Above I had meant a field that has the ServerTimestamp declaration in the struct tags. So, in your example, |
tl;dr This is working as intended. Admittedly, the behavior is subtle, and I think we should add some documentation. Let's go through your code piece by piece.
This behaves as you'd expect. You created a document with one field, which holds the time that you wrote the document. Now it's important to keep in mind that Firestore documents are JSON-like sets of key-value pairs. The Go client maps between the actual documents and Go structs as a convenience. Also, as the doc that @jadekler quoted above says, a struct field marked with Proceeding with your code:
Since you didn't specify a merge option to
So the document you are writing is the empty document,
you can observe that's it's empty by printing Now you do
Since you are populating a struct with an empty document, nothing in the struct gets changed. Here I think we should improve our documentation to make this clear. I've created https://code-review.googlesource.com/c/gocloud/+/34170. Let me know if that would have helped. |
Thanks for the investigation and clarification, @jba. The documentation update in 34170 LGTM. We are finding this behavior a bit unintuitive, so we are going to stop using For context, we are writing to firestore documents that are read by Pring in iOS. This library makes the strong assumption that |
@CaptainCodeman It plainly states in the Set documentation that it overwrites the document. If you have a suggestion for how we can make it more clear, please file a new issue as this one is quite old. |
I'm running into an issue with the go firestore client, where
Set
is not doing what I'd expect.When we call set with a struct that has a
serverTimestamp
field, that field is zeroed out, rather than ignored. I'd expect that field to update to the value I set, or if I set the value to zero, for that field to be server updated.(It looks like the issue lies in the docref.go code which adds a write operation without checking if the current value of the serverTimestamp field is time zero.)
Here's a test to demonstrate:
Output on my end:
The text was updated successfully, but these errors were encountered: