-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
25 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,32 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"github.com/icinga/icinga-go-library/types" | ||
) | ||
|
||
type contextKey string // Private type to prevent collisions with other context keys | ||
|
||
// clusterContextKey is the key for Cluster values in contexts. | ||
var clusterContextKey = contextKey("cluster") | ||
|
||
type Cluster struct { | ||
Uuid types.UUID | ||
Name string | ||
} | ||
|
||
// NewClusterContext returns a new Context that carries this Cluster as value. | ||
func (c *Cluster) NewClusterContext(parent context.Context) context.Context { | ||
return context.WithValue(parent, clusterContextKey, c) | ||
} | ||
|
||
// ClusterFromContext returns the Cluster value stored in ctx, if any: | ||
// | ||
// e, ok := ClusterFromContext(ctx) | ||
// if !ok { | ||
// // Error handling. | ||
// } | ||
func ClusterFromContext(ctx context.Context) (*Cluster, bool) { | ||
c, ok := ctx.Value(clusterContextKey).(*Cluster) | ||
return c, ok | ||
} |