add selector code annotation (#1275)

pull/1289/head
zwhyb 3 years ago committed by GitHub
parent bc35f20228
commit 6aba247990
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      middleware/selector/selector.go

@ -15,14 +15,17 @@ type (
) )
var ( var (
// serverTransporter is get server transport.Transporter from ctx
serverTransporter transporter = func(ctx context.Context) (transport.Transporter, bool) { serverTransporter transporter = func(ctx context.Context) (transport.Transporter, bool) {
return transport.FromServerContext(ctx) return transport.FromServerContext(ctx)
} }
// clientTransporter is get client transport.Transporter from ctx
clientTransporter transporter = func(ctx context.Context) (transport.Transporter, bool) { clientTransporter transporter = func(ctx context.Context) (transport.Transporter, bool) {
return transport.FromClientContext(ctx) return transport.FromClientContext(ctx)
} }
) )
// Builder is a selector builder
type Builder struct { type Builder struct {
client bool client bool
@ -33,28 +36,35 @@ type Builder struct {
ms []middleware.Middleware ms []middleware.Middleware
} }
// Server selector middleware
func Server(ms ...middleware.Middleware) *Builder { func Server(ms ...middleware.Middleware) *Builder {
return &Builder{ms: ms} return &Builder{ms: ms}
} }
// Client selector middleware
func Client(ms ...middleware.Middleware) *Builder { func Client(ms ...middleware.Middleware) *Builder {
return &Builder{client: true, ms: ms} return &Builder{client: true, ms: ms}
} }
// Prefix is with Builder's prefix
func (b *Builder) Prefix(prefix ...string) *Builder { func (b *Builder) Prefix(prefix ...string) *Builder {
b.prefix = prefix b.prefix = prefix
return b return b
} }
// Regex is with Builder's regex
func (b *Builder) Regex(regex ...string) *Builder { func (b *Builder) Regex(regex ...string) *Builder {
b.regex = regex b.regex = regex
return b return b
} }
// Path is with Builder's path
func (b *Builder) Path(path ...string) *Builder { func (b *Builder) Path(path ...string) *Builder {
b.path = path b.path = path
return b return b
} }
// Build is Builder's Build, for example: Server().Path(m1,m2).Build()
func (b *Builder) Build() middleware.Middleware { func (b *Builder) Build() middleware.Middleware {
var transporter func(ctx context.Context) (transport.Transporter, bool) var transporter func(ctx context.Context) (transport.Transporter, bool)
if b.client { if b.client {
@ -65,6 +75,7 @@ func (b *Builder) Build() middleware.Middleware {
return selector(transporter, b.match, b.ms...) return selector(transporter, b.match, b.ms...)
} }
// match is match operation compliance Builder
func (b *Builder) match(operation string) bool { func (b *Builder) match(operation string) bool {
for _, prefix := range b.prefix { for _, prefix := range b.prefix {
if prefixMatch(prefix, operation) { if prefixMatch(prefix, operation) {
@ -84,6 +95,7 @@ func (b *Builder) match(operation string) bool {
return false return false
} }
// selector middleware
func selector(transporter transporter, match match, ms ...middleware.Middleware) middleware.Middleware { func selector(transporter transporter, match match, ms ...middleware.Middleware) middleware.Middleware {
return func(handler middleware.Handler) middleware.Handler { return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { return func(ctx context.Context, req interface{}) (reply interface{}, err error) {

Loading…
Cancel
Save