0

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.

lubumbax
  • 255
  • 2
  • 9
  • From what you explained, it’s similar to [this](https://stackoverflow.com/questions/47114024/) past thread, is that right? For Go, you could use a plain `struct` and tag the `ResourceName` field with [`firestore:”-”`](https://pkg.go.dev/cloud.google.com/go/firestore#DocumentRef.Create) to make the Firebase SDK ignore it when storing documents. Is it one of your requirements using Protoc to generate the Struct? – ErnestoC Sep 26 '22 at 22:08
  • Thanks. It's similar. I read about the `firestore:"-"` field, though our project requires that the data models/structs are generated by protoc. – lubumbax Oct 11 '22 at 16:54

0 Answers0