Skip to content
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

feat(functions/metadata): Special-case marshaling #2669

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions functions/metadata/metadata.go
Expand Up @@ -77,6 +77,24 @@ func (r *Resource) UnmarshalJSON(data []byte) error {
return nil
}

// MarshalJSON specializes the Resource marshalling to handle the case where the
// value is a string instead of a map. See the comment above on RawPath for why this
// needs to be handled.
func (r *Resource) MarshalJSON() ([]byte, error) {
roffjulie marked this conversation as resolved.
Show resolved Hide resolved
// If RawPath is set, use that as the whole value.
if r.RawPath != "" {
return []byte(fmt.Sprintf("%q", r.RawPath)), nil
}

// Otherwise, accept whatever the result of the normal marshal would be.
res := *r
b, err := json.Marshal(res)
if err != nil {
return nil, err
}
return b, nil
}

type contextKey string

// GCFContextKey satisfies an interface to be able to use contextKey to read
Expand Down
52 changes: 52 additions & 0 deletions functions/metadata/metadata_test.go
Expand Up @@ -120,3 +120,55 @@ func TestUnmarshalJSON(t *testing.T) {
}
}
}

func TestMarshalJSON(t *testing.T) {
ts, err := time.Parse("2006-01-02T15:04:05Z07:00", "2019-11-04T23:01:10.112Z")
if err != nil {
t.Fatalf("Error parsing time: %v.", err)
}
var tests = []struct {
name string
metadata Metadata
want []byte
}{
{
name: "MetadataWithResource",
metadata: Metadata{
EventID: "1234567",
Timestamp: ts,
EventType: "google.pubsub.topic.publish",
Resource: &Resource{
Service: "pubsub.googleapis.com",
Name: "mytopic",
Type: "type.googleapis.com/google.pubsub.v1.PubsubMessage",
},
},
},
{
name: "MetadataWithString",
metadata: Metadata{
EventID: "1234567",
Timestamp: ts,
EventType: "google.pubsub.topic.publish",
Resource: &Resource{
RawPath: "projects/myproject/mytopic",
},
},
},
}

for _, tc := range tests {
b, err := json.Marshal(&tc.metadata)
roffjulie marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Errorf("MarshalJSON(%s) error: %v", tc.name, err)
}

var m Metadata
if err := json.Unmarshal(b, &m); err != nil {
t.Errorf("MarshalJSON(%s) error: %v", tc.name, err)
}
if !cmp.Equal(m, tc.metadata) {
t.Errorf("MarshalJSON(%s) error: got %v, want %v", tc.name, m, tc.metadata)
}
}
}