Skip to content

Commit

Permalink
fix: make metadata.FromContext work again (#103)
Browse files Browse the repository at this point in the history
FF Go used to have a dependency on the
"cloud.google.com/go/functions/metadata" package, which defined an
unexported context key and used it to add a value to the context. This
is the recommended way to add values to a context using
[WithValue](https://pkg.go.dev/context#WithValue).

FF Go has to continue using the `metadata.NewContext` call from
"cloud.google.com/go/functions/metadata" to have `metadata.FromContext`
from the same package work in customer's function code. This is due to
the fact that Golang purposely differentiates context keys from
different packages as different keys to prevent collisions, even if the
keys have the same string-name.

In the future, FF Go can move onto using its own `metadata` package to
avoid pulling in all the dependencies of
"cloud.google.com/go/functions/metadata", which contains other unrelated
GCF client code too. But it will have to be done with a breaking change
major version update.
  • Loading branch information
anniefu authored Nov 24, 2021
1 parent d441889 commit 2714714
Show file tree
Hide file tree
Showing 10 changed files with 532 additions and 331 deletions.
2 changes: 1 addition & 1 deletion funcframework/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"regexp"
"strings"

"cloud.google.com/go/functions/metadata"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/events/pubsub"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/fftypes"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/metadata"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion funcframework/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/GoogleCloudPlatform/functions-framework-go/internal/metadata"
"cloud.google.com/go/functions/metadata"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/GoogleCloudPlatform/functions-framework-go
go 1.11

require (
cloud.google.com/go/functions v1.0.0
github.com/cloudevents/sdk-go/v2 v2.6.1
github.com/google/go-cmp v0.5.6
)
529 changes: 525 additions & 4 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/events/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"regexp"
"time"

"cloud.google.com/go/functions/metadata"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/fftypes"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/metadata"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/events/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
"time"

"cloud.google.com/go/functions/metadata"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/fftypes"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/metadata"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/fftypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package fftypes

import (
"github.com/GoogleCloudPlatform/functions-framework-go/internal/metadata"
"cloud.google.com/go/functions/metadata"
)

// BackgroundEvent is the incoming payload to functions framework to trigger
Expand Down
147 changes: 0 additions & 147 deletions internal/metadata/metadata.go

This file was deleted.

174 changes: 0 additions & 174 deletions internal/metadata/metadata_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion testdata/conformance/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"log"
"net/http"

"cloud.google.com/go/functions/metadata"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/GoogleCloudPlatform/functions-framework-go/internal/metadata"
cloudevents "github.com/cloudevents/sdk-go/v2"
)

Expand Down

0 comments on commit 2714714

Please sign in to comment.