github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/api/resolver/options.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/api/resolver/options.go
    16  
    17  package resolver
    18  
    19  import (
    20  	"github.com/tickoalcantara12/micro/v3/service/registry"
    21  )
    22  
    23  type Options struct {
    24  	Handler       string
    25  	ServicePrefix string
    26  }
    27  
    28  type Option func(o *Options)
    29  
    30  // WithHandler sets the handler being used
    31  func WithHandler(h string) Option {
    32  	return func(o *Options) {
    33  		o.Handler = h
    34  	}
    35  }
    36  
    37  // WithServicePrefix sets the ServicePrefix option
    38  func WithServicePrefix(p string) Option {
    39  	return func(o *Options) {
    40  		o.ServicePrefix = p
    41  	}
    42  }
    43  
    44  // NewOptions returns new initialised options
    45  func NewOptions(opts ...Option) Options {
    46  	var options Options
    47  	for _, o := range opts {
    48  		o(&options)
    49  	}
    50  	return options
    51  }
    52  
    53  // ResolveOptions are used when resolving a request
    54  type ResolveOptions struct {
    55  	Domain string
    56  }
    57  
    58  // ResolveOption sets an option
    59  type ResolveOption func(*ResolveOptions)
    60  
    61  // Domain sets the resolve Domain option
    62  func Domain(n string) ResolveOption {
    63  	return func(o *ResolveOptions) {
    64  		o.Domain = n
    65  	}
    66  }
    67  
    68  // NewResolveOptions returns new initialised resolve options
    69  func NewResolveOptions(opts ...ResolveOption) ResolveOptions {
    70  	var options ResolveOptions
    71  	for _, o := range opts {
    72  		o(&options)
    73  	}
    74  	if len(options.Domain) == 0 {
    75  		options.Domain = registry.DefaultDomain
    76  	}
    77  
    78  	return options
    79  }