-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move context functions from middleware package to dynoid package.
Consistent naming of parameters.
- Loading branch information
Showing
4 changed files
with
107 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dynoid | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrTokenNotSet = errors.New("token not set") // returned when neither a token nor an error is set | ||
) | ||
|
||
type dynoidCtxKey uint | ||
|
||
const ( | ||
tokenCtxKey dynoidCtxKey = iota | ||
) | ||
|
||
type maybeToken struct { | ||
*Token | ||
error | ||
} | ||
|
||
// ContextWithToken adds the given Token to the Context to be retrieved later | ||
// by calling FromContext | ||
func ContextWithToken(ctx context.Context, t *Token) context.Context { | ||
return context.WithValue(ctx, tokenCtxKey, &maybeToken{Token: t}) | ||
} | ||
|
||
// ContextWithError adds the given error to the Context to be retrieved later | ||
// by calling FromContext | ||
func ContextWithError(ctx context.Context, err error) context.Context { | ||
return context.WithValue(ctx, tokenCtxKey, &maybeToken{error: err}) | ||
} | ||
|
||
// FromContext returns the Token or error associated with the given Context | ||
func FromContext(ctx context.Context) (*Token, error) { | ||
if v, ok := ctx.Value(tokenCtxKey).(*maybeToken); ok { | ||
return v.Token, v.error | ||
} | ||
|
||
return nil, ErrTokenNotSet | ||
} |
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