github.com/annwntech/go-micro/v2@v2.9.5/router/query.go (about)

     1  package router
     2  
     3  // QueryOption sets routing table query options
     4  type QueryOption func(*QueryOptions)
     5  
     6  // QueryOptions are routing table query options
     7  type QueryOptions struct {
     8  	// Service is destination service name
     9  	Service string
    10  	// Address of the service
    11  	Address string
    12  	// Gateway is route gateway
    13  	Gateway string
    14  	// Network is network address
    15  	Network string
    16  	// Router is router id
    17  	Router string
    18  	// Strategy is routing strategy
    19  	Strategy Strategy
    20  }
    21  
    22  // QueryService sets service to query
    23  func QueryService(s string) QueryOption {
    24  	return func(o *QueryOptions) {
    25  		o.Service = s
    26  	}
    27  }
    28  
    29  // QueryAddress sets service to query
    30  func QueryAddress(a string) QueryOption {
    31  	return func(o *QueryOptions) {
    32  		o.Address = a
    33  	}
    34  }
    35  
    36  // QueryGateway sets gateway address to query
    37  func QueryGateway(g string) QueryOption {
    38  	return func(o *QueryOptions) {
    39  		o.Gateway = g
    40  	}
    41  }
    42  
    43  // QueryNetwork sets network name to query
    44  func QueryNetwork(n string) QueryOption {
    45  	return func(o *QueryOptions) {
    46  		o.Network = n
    47  	}
    48  }
    49  
    50  // QueryRouter sets router id to query
    51  func QueryRouter(r string) QueryOption {
    52  	return func(o *QueryOptions) {
    53  		o.Router = r
    54  	}
    55  }
    56  
    57  // QueryStrategy sets strategy to query
    58  func QueryStrategy(s Strategy) QueryOption {
    59  	return func(o *QueryOptions) {
    60  		o.Strategy = s
    61  	}
    62  }
    63  
    64  // NewQuery creates new query and returns it
    65  func NewQuery(opts ...QueryOption) QueryOptions {
    66  	// default options
    67  	qopts := QueryOptions{
    68  		Service:  "*",
    69  		Address:  "*",
    70  		Gateway:  "*",
    71  		Network:  "*",
    72  		Router:   "*",
    73  		Strategy: AdvertiseAll,
    74  	}
    75  
    76  	for _, o := range opts {
    77  		o(&qopts)
    78  	}
    79  
    80  	return qopts
    81  }