Skip to content

Commit

Permalink
Format "generation" field in Pubsub notification as a string (#972)
Browse files Browse the repository at this point in the history
As documented in the storage objects JSON object representation, all
fields are "string formatted as the specified value type", including
numbers.

https://cloud.google.com/storage/docs/json_api/v1/objects#resource-representations

`strconv.FormatInt` was used for the "size" field, but this wasn't done
for generation.

We noticed this when doing local testing, and the messages failed JSON
unmarshalling from fake-gcs-server, but were properly unmarshalling from
real Google Cloud Storage pubsub messages.
  • Loading branch information
danmcgee-soda authored Oct 31, 2022
1 parent bc65d6a commit 66fa719
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions internal/notification/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ func (m *PubsubEventManager) publish(o *backend.StreamingObject, eventType Event
return nil
}

// gcsEvent is the payload of a GCS event. The description of the full object
// can be found here:
// gcsEvent is the payload of a GCS event. Note that all properties are string-quoted.
// The description of the full object can be found here:
// https://cloud.google.com/storage/docs/json_api/v1/objects#resource-representations.
type gcsEvent struct {
Kind string `json:"kind"`
ID string `json:"id"`
Name string `json:"name"`
Bucket string `json:"bucket"`
Generation int64 `json:"generation,omitempty"`
Generation int64 `json:"generation,string,omitempty"`
ContentType string `json:"contentType"`
ContentEncoding string `json:"contentEncoding,omitempty"`
Created string `json:"timeCreated,omitempty"`
Updated string `json:"updated,omitempty"`
StorageClass string `json:"storageClass"`
Size string `json:"size"`
Size int64 `json:"size,string"`
MD5Hash string `json:"md5Hash,omitempty"`
CRC32c string `json:"crc32c,omitempty"`
MetaData map[string]string `json:"metadata,omitempty"`
Expand All @@ -187,7 +187,7 @@ func generateEvent(o *backend.StreamingObject, eventType EventType, eventTime st
Created: o.Created,
Updated: o.Updated,
StorageClass: "STANDARD",
Size: strconv.FormatInt(o.Size, 10),
Size: o.Size,
MD5Hash: o.Md5Hash,
CRC32c: o.Crc32c,
MetaData: o.Metadata,
Expand Down
2 changes: 1 addition & 1 deletion internal/notification/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestPubsubEventManager_Trigger(t *testing.T) {
if obj.Name != receivedEvent.Name {
t.Errorf("wrong object name\nwant %q\ngot %q", obj.Name, receivedEvent.Name)
}
if strconv.Itoa(len(bufferedObj.Content)) != receivedEvent.Size {
if int64(len(bufferedObj.Content)) != receivedEvent.Size {
t.Errorf("wrong object size\nwant %q\ngot %q", strconv.Itoa(len(bufferedObj.Content)), receivedEvent.Size)
}
if !reflect.DeepEqual(test.metadata, receivedEvent.MetaData) {
Expand Down

0 comments on commit 66fa719

Please sign in to comment.