FromContext/NewContext methods
This commit is contained in:
parent
d306559d8f
commit
1cc621e2d6
16
client/context.go
Normal file
16
client/context.go
Normal file
@ -0,0 +1,16 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type clientKey struct{}
|
||||
|
||||
func FromContext(ctx context.Context) (Client, bool) {
|
||||
c, ok := ctx.Value(clientKey{}).(Client)
|
||||
return c, ok
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, c Client) context.Context {
|
||||
return context.WithValue(ctx, clientKey{}, c)
|
||||
}
|
13
go-micro.go
13
go-micro.go
@ -22,8 +22,12 @@ package micro
|
||||
import (
|
||||
"github.com/micro/go-micro/client"
|
||||
"github.com/micro/go-micro/server"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type serviceKey struct{}
|
||||
|
||||
// Service is an interface that wraps the lower level libraries
|
||||
// within go-micro. Its a convenience method for building
|
||||
// and initialising services.
|
||||
@ -45,3 +49,12 @@ var (
|
||||
func NewService(opts ...Option) Service {
|
||||
return newService(opts...)
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) (Service, bool) {
|
||||
s, ok := ctx.Value(serviceKey{}).(Service)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, s Service) context.Context {
|
||||
return context.WithValue(ctx, serviceKey{}, s)
|
||||
}
|
||||
|
16
server/context.go
Normal file
16
server/context.go
Normal file
@ -0,0 +1,16 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type serverKey struct{}
|
||||
|
||||
func FromContext(ctx context.Context) (Server, bool) {
|
||||
c, ok := ctx.Value(serverKey{}).(Server)
|
||||
return c, ok
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, s Server) context.Context {
|
||||
return context.WithValue(ctx, serverKey{}, s)
|
||||
}
|
Loading…
Reference in New Issue
Block a user