-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for setting metadata through context
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package typhon | ||
|
||
import "context" | ||
|
||
type metadataKey struct{} | ||
|
||
// Metadata provides a transport agnostic way to pass metadata with Typhon. | ||
// It aligns to the interface of Go's default HTTP header type for convenience. | ||
type Metadata map[string][]string | ||
|
||
// NewMetadata creates a metadata struct from a map of strings. | ||
func NewMetadata(data map[string]string) Metadata { | ||
meta := make(Metadata, len(data)) | ||
for k, v := range data { | ||
meta[k] = []string{v} | ||
} | ||
return meta | ||
} | ||
|
||
// AppendMetadataToContext sets the metadata on the context. | ||
func AppendMetadataToContext(ctx context.Context, md Metadata) context.Context { | ||
return context.WithValue(ctx, metadataKey{}, md) | ||
} | ||
|
||
// MetadataFromContext retrieves the metadata from the context. | ||
func MetadataFromContext(ctx context.Context) Metadata { | ||
meta, ok := ctx.Value(metadataKey{}).(Metadata) | ||
if !ok { | ||
return Metadata{} | ||
} | ||
return meta | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package typhon | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMetadataRoundtrip(t *testing.T) { | ||
meta := NewMetadata(map[string]string{ | ||
"meta": "data", | ||
}) | ||
ctx := context.Background() | ||
|
||
withMeta := AppendMetadataToContext(ctx, meta) | ||
out := MetadataFromContext(withMeta) | ||
|
||
assert.Equal(t, meta, out) | ||
} | ||
|
||
func TestMetadataNotSet(t *testing.T) { | ||
meta := NewMetadata(map[string]string{}) | ||
out := MetadataFromContext(context.Background()) | ||
|
||
assert.Equal(t, meta, out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters