trpc.group/trpc-go/trpc-go@v1.0.3/naming/servicerouter/options.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package servicerouter 15 16 import ( 17 "context" 18 ) 19 20 // Options defines the call options. 21 type Options struct { 22 Ctx context.Context 23 SourceSetName string 24 DestinationSetName string 25 DisableServiceRouter bool 26 Namespace string 27 SourceNamespace string 28 SourceServiceName string 29 SourceEnvName string 30 DestinationEnvName string 31 EnvTransfer string 32 EnvKey string 33 SourceMetadata map[string]string 34 DestinationMetadata map[string]string 35 } 36 37 // Option modifies the Options. 38 type Option func(*Options) 39 40 // WithContext returns an Option which sets request context. 41 func WithContext(ctx context.Context) Option { 42 return func(o *Options) { 43 o.Ctx = ctx 44 } 45 } 46 47 // WithNamespace returns an Option which sets namespace. 48 func WithNamespace(namespace string) Option { 49 return func(opts *Options) { 50 opts.Namespace = namespace 51 } 52 } 53 54 // WithDisableServiceRouter returns an Option which disables service router. 55 func WithDisableServiceRouter() Option { 56 return func(o *Options) { 57 o.DisableServiceRouter = true 58 } 59 } 60 61 // WithSourceNamespace returns an Option which sets caller namespace. 62 func WithSourceNamespace(namespace string) Option { 63 return func(o *Options) { 64 o.SourceNamespace = namespace 65 } 66 } 67 68 // WithSourceServiceName returns an Option which sets caller service name. 69 func WithSourceServiceName(serviceName string) Option { 70 return func(o *Options) { 71 o.SourceServiceName = serviceName 72 } 73 } 74 75 // WithSourceEnvName returns an Option which sets caller environment name. 76 func WithSourceEnvName(envName string) Option { 77 return func(o *Options) { 78 o.SourceEnvName = envName 79 } 80 } 81 82 // WithDestinationEnvName returns an Option which sets callee environment name. 83 func WithDestinationEnvName(envName string) Option { 84 return func(o *Options) { 85 o.DestinationEnvName = envName 86 } 87 } 88 89 // WithEnvTransfer returns an Option which sets transparent environment information. 90 func WithEnvTransfer(envTransfer string) Option { 91 return func(o *Options) { 92 o.EnvTransfer = envTransfer 93 } 94 } 95 96 // WithEnvKey returns an Option which sets environment key. 97 func WithEnvKey(key string) Option { 98 return func(o *Options) { 99 o.EnvKey = key 100 } 101 } 102 103 // WithSourceSetName returns an Option which sets caller set name. 104 func WithSourceSetName(sourceSetName string) Option { 105 return func(o *Options) { 106 o.SourceSetName = sourceSetName 107 } 108 } 109 110 // WithDestinationSetName returns an Option which sets callee set name. 111 func WithDestinationSetName(destinationSetName string) Option { 112 return func(o *Options) { 113 o.DestinationSetName = destinationSetName 114 } 115 } 116 117 // WithSourceMetadata returns an Option which sets caller metadata. 118 func WithSourceMetadata(key string, val string) Option { 119 return func(o *Options) { 120 if o.SourceMetadata == nil { 121 o.SourceMetadata = make(map[string]string) 122 } 123 o.SourceMetadata[key] = val 124 } 125 } 126 127 // WithDestinationMetadata returns an Option which sets callee metadata. 128 func WithDestinationMetadata(key string, val string) Option { 129 return func(o *Options) { 130 if o.DestinationMetadata == nil { 131 o.DestinationMetadata = make(map[string]string) 132 } 133 o.DestinationMetadata[key] = val 134 } 135 }