go.uber.org/yarpc@v1.72.1/encoding/thrift/internal/observabilitytest/test/testservicefx/client.go (about)

     1  // Code generated by thriftrw-plugin-yarpc
     2  // @generated
     3  
     4  // Copyright (c) 2022 Uber Technologies, Inc.
     5  //
     6  // Permission is hereby granted, free of charge, to any person obtaining a copy
     7  // of this software and associated documentation files (the "Software"), to deal
     8  // in the Software without restriction, including without limitation the rights
     9  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  // copies of the Software, and to permit persons to whom the Software is
    11  // furnished to do so, subject to the following conditions:
    12  //
    13  // The above copyright notice and this permission notice shall be included in
    14  // all copies or substantial portions of the Software.
    15  //
    16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  // THE SOFTWARE.
    23  
    24  package testservicefx
    25  
    26  import (
    27  	fx "go.uber.org/fx"
    28  	yarpc "go.uber.org/yarpc"
    29  	transport "go.uber.org/yarpc/api/transport"
    30  	restriction "go.uber.org/yarpc/api/x/restriction"
    31  	thrift "go.uber.org/yarpc/encoding/thrift"
    32  	testserviceclient "go.uber.org/yarpc/encoding/thrift/internal/observabilitytest/test/testserviceclient"
    33  )
    34  
    35  // Params defines the dependencies for the TestService client.
    36  type Params struct {
    37  	fx.In
    38  
    39  	Provider    yarpc.ClientConfig
    40  	Restriction restriction.Checker `optional:"true"`
    41  }
    42  
    43  // Result defines the output of the TestService client module. It provides a
    44  // TestService client to an Fx application.
    45  type Result struct {
    46  	fx.Out
    47  
    48  	Client testserviceclient.Interface
    49  
    50  	// We are using an fx.Out struct here instead of just returning a client
    51  	// so that we can add more values or add named versions of the client in
    52  	// the future without breaking any existing code.
    53  }
    54  
    55  // Client provides a TestService client to an Fx application using the given name
    56  // for routing.
    57  //
    58  // 	fx.Provide(
    59  // 		testservicefx.Client("..."),
    60  // 		newHandler,
    61  // 	)
    62  func Client(name string, opts ...thrift.ClientOption) interface{} {
    63  	return func(p Params) Result {
    64  		cc := p.Provider.ClientConfig(name)
    65  		if namer, ok := cc.GetUnaryOutbound().(transport.Namer); ok && p.Restriction != nil {
    66  			if err := p.Restriction.Check(thrift.Encoding, namer.TransportName()); err != nil {
    67  				panic(err.Error())
    68  			}
    69  		}
    70  		client := testserviceclient.New(cc, opts...)
    71  		return Result{Client: client}
    72  	}
    73  }