go.uber.org/yarpc@v1.72.1/internal/prototest/exampleutil/exampleutil.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package exampleutil
    22  
    23  import (
    24  	"go.uber.org/yarpc/api/transport"
    25  	"go.uber.org/yarpc/encoding/protobuf"
    26  	"go.uber.org/yarpc/internal/grpcctx"
    27  	"go.uber.org/yarpc/internal/prototest/examplepb"
    28  	"go.uber.org/yarpc/internal/testutils"
    29  	"go.uber.org/zap"
    30  )
    31  
    32  // Clients holds all clients.
    33  type Clients struct {
    34  	KeyValueYARPCClient     examplepb.KeyValueYARPCClient
    35  	FooYARPCClient          examplepb.FooYARPCClient
    36  	KeyValueYARPCJSONClient examplepb.KeyValueYARPCClient
    37  	FooYARPCJSONClient      examplepb.FooYARPCClient
    38  	KeyValueGRPCClient      examplepb.KeyValueClient
    39  	FooGRPCClient           examplepb.FooClient
    40  	ContextWrapper          *grpcctx.ContextWrapper
    41  }
    42  
    43  // WithClients calls f on the Clients.
    44  func WithClients(
    45  	transportType testutils.TransportType,
    46  	keyValueYARPCServer examplepb.KeyValueYARPCServer,
    47  	fooYARPCServer examplepb.FooYARPCServer,
    48  	logger *zap.Logger,
    49  	f func(*Clients) error,
    50  ) error {
    51  	var procedures []transport.Procedure
    52  	if keyValueYARPCServer != nil {
    53  		procedures = append(procedures, examplepb.BuildKeyValueYARPCProcedures(keyValueYARPCServer)...)
    54  	}
    55  	if fooYARPCServer != nil {
    56  		procedures = append(procedures, examplepb.BuildFooYARPCProcedures(fooYARPCServer)...)
    57  	}
    58  	return testutils.WithClientInfo(
    59  		"example",
    60  		procedures,
    61  		transportType,
    62  		logger,
    63  		func(clientInfo *testutils.ClientInfo) error {
    64  			return f(
    65  				&Clients{
    66  					examplepb.NewKeyValueYARPCClient(clientInfo.ClientConfig),
    67  					examplepb.NewFooYARPCClient(clientInfo.ClientConfig),
    68  					examplepb.NewKeyValueYARPCClient(clientInfo.ClientConfig, protobuf.UseJSON),
    69  					examplepb.NewFooYARPCClient(clientInfo.ClientConfig, protobuf.UseJSON),
    70  					examplepb.NewKeyValueClient(clientInfo.GRPCClientConn),
    71  					examplepb.NewFooClient(clientInfo.GRPCClientConn),
    72  					clientInfo.ContextWrapper,
    73  				},
    74  			)
    75  		},
    76  	)
    77  }