-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
2fc551b
commit 5828e97
Showing
7 changed files
with
227 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package camera | ||
|
||
import "context" | ||
|
||
// Extra is the type of value stored in the Contexts. | ||
type ( | ||
Extra map[string]interface{} | ||
key int | ||
) | ||
|
||
var extraKey key | ||
|
||
// NewContext returns a new Context that carries value Extra. | ||
func NewContext(ctx context.Context, e Extra) context.Context { | ||
return context.WithValue(ctx, extraKey, e) | ||
} | ||
|
||
// FromContext returns the Extra value stored in ctx, if any. | ||
func FromContext(ctx context.Context) (Extra, bool) { | ||
ext, ok := ctx.Value(extraKey).(Extra) | ||
return ext, ok | ||
} |
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 camera | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"go.viam.com/test" | ||
) | ||
|
||
func TestExtraEmpty(t *testing.T) { | ||
ctx := context.Background() | ||
_, ok := FromContext(ctx) | ||
test.That(t, ok, test.ShouldBeFalse) | ||
} | ||
|
||
func TestExtraRoundtrip(t *testing.T) { | ||
ctx := context.Background() | ||
expected := Extra{ | ||
"It goes one": "by one", | ||
"even two": "by two", | ||
} | ||
|
||
ctx = NewContext(ctx, expected) | ||
actual, ok := FromContext(ctx) | ||
test.That(t, ok, test.ShouldBeTrue) | ||
test.That(t, actual, test.ShouldEqual, expected) | ||
} |
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
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