mirror of
				https://github.com/go-micro/go-micro.git
				synced 2025-10-30 23:27:41 +02:00 
			
		
		
		
	FromContext/NewContext methods
This commit is contained in:
		
							
								
								
									
										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) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user