go.uber.org/yarpc@v1.72.1/api/encoding/call_option.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 encoding 22 23 // CallOption defines options that may be passed in at call sites to other 24 // services. 25 // 26 // Encoding authors should accept yarpc.CallOptions and convert them to 27 // encoding.CallOptions to use with NewOutboundCall. This will keep the 28 // API for service authors simple. 29 type CallOption struct { 30 opt callOption 31 } 32 33 type callOption interface { 34 apply(*OutboundCall) 35 } 36 37 type responseHeadersOptions map[string]string 38 39 func (r *responseHeadersOptions) apply(call *OutboundCall) { 40 call.responseHeaders = (*map[string]string)(r) 41 } 42 43 // ResponseHeaders specifies that headers received in response to this request 44 // should replace the given map. 45 func ResponseHeaders(h *map[string]string) CallOption { 46 return CallOption{(*responseHeadersOptions)(h)} 47 } 48 49 type headerOption keyValuePair 50 51 func (r headerOption) apply(call *OutboundCall) { 52 call.headers = append(call.headers, keyValuePair(r)) 53 } 54 55 // WithHeader adds a new header to the request. 56 func WithHeader(k, v string) CallOption { 57 return CallOption{headerOption(keyValuePair{k: k, v: v})} 58 } 59 60 type shardKeyOption string 61 62 func (r shardKeyOption) apply(call *OutboundCall) { 63 x := string(r) 64 call.shardKey = &x 65 } 66 67 // WithShardKey sets the shard key for the request. 68 func WithShardKey(sk string) CallOption { 69 return CallOption{shardKeyOption(sk)} 70 } 71 72 type routingKeyOption string 73 74 func (r routingKeyOption) apply(call *OutboundCall) { 75 x := string(r) 76 call.routingKey = &x 77 } 78 79 // WithRoutingKey sets the routing key for the request. 80 func WithRoutingKey(rk string) CallOption { 81 return CallOption{routingKeyOption(rk)} 82 } 83 84 type routingDelegateOption string 85 86 func (r routingDelegateOption) apply(call *OutboundCall) { 87 x := string(r) 88 call.routingDelegate = &x 89 } 90 91 // WithRoutingDelegate sets the routing delegate for the request. 92 func WithRoutingDelegate(rd string) CallOption { 93 return CallOption{routingDelegateOption(rd)} 94 }