I have such a Document
message that is being stored in Google Cloud Firestore:
message Document {
string resource_name = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
string title = 2;
}
Setting such a Document to a firestore.DocumentRef
directly results in both fields being stored as "ResourceName" and "Title" respectively.
I would like to store only "Title" and explicitly populate "resource_name" in my responses using the ID
implicit in a firestore.DocumentRef
.
I expected the OUTPUT_ONLY
behaviour to be honoured in some way but it isn't.
Is there a known way to do this?
Here is a more detailed explanation:
When a document is stored to Firestore, a uid can be automatically assigned to the document:
document := Document{}
dRef := someCollection.NewDoc()
_, err = dRef.Set(ctx, &document) // stores 'ResourceName'!
Later I can retrieve that document by its uid as:
dRef := someCollection.Doc(uid)
dSnap, err := dRef.Get(ctx)
I would like to return that uid to the caller in the resource_name
field (something like "resourceName": "document/26b38366-1c57-4aa5-a6c1-bbff6a5119a5"
).
It could be as easy as:
document := Document{}
dSnap.DataTo(&document)
document.ResourceName = "document/" + dSnap.Ref.ID
Since the document uid is implicit in the firestore.DocumentRef
I don't want to store the field ResourceName
, which I personally find redundant.
I have tried marshalling the Document first using protojson
, but still, the field gets serialized too.
Alternatively, I tried marshalling the Document first using protojson
, then unmarshalling the resulting []byte
to a map[string]interface{}
that I can access to remove the resourceName
.
I wonder if there is a way to do this in a less "hacky" way.