go.uber.org/yarpc@v1.72.1/encoding/thrift/thriftrw-plugin-yarpc/template.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 main 22 23 import ( 24 "fmt" 25 "strings" 26 27 "go.uber.org/thriftrw/plugin" 28 "go.uber.org/thriftrw/plugin/api" 29 ) 30 31 // Svc is a Thrift service. 32 type Svc struct { 33 *api.Service 34 35 Module *api.Module 36 37 // Ordered list of parents of this service. If the list is non-empty, the 38 // immediate parent of this service is the first item in the list, its 39 // parent service is next, and so on. 40 Parents []*Svc 41 } 42 43 // AllFunctions returns a list of all functions for this service including 44 // inherited functions. 45 func (s *Svc) AllFunctions() []*api.Function { 46 var ( 47 functions []*api.Function 48 added = make(map[string]struct{}) 49 services = append([]*Svc{s}, s.Parents...) 50 ) 51 52 for _, s := range services { 53 for _, f := range s.Functions { 54 if _, taken := added[f.ThriftName]; taken { 55 continue 56 } 57 58 functions = append(functions, f) 59 } 60 } 61 62 return functions 63 } 64 65 // Parent returns the immediate parent of this service or nil if it doesn't 66 // have any. 67 func (s *Svc) Parent() *api.Service { 68 if len(s.Parents) > 0 { 69 return s.Parents[0].Service 70 } 71 return nil 72 } 73 74 // ServerPackagePath returns the import path to the server package for this 75 // service. 76 func (s *Svc) ServerPackagePath() string { 77 return fmt.Sprintf("%s/%sserver", s.Module.ImportPath, strings.ToLower(s.Name)) 78 } 79 80 // ClientPackagePath returns the import path to the server package for this 81 // service. 82 func (s *Svc) ClientPackagePath() string { 83 return fmt.Sprintf("%s/%sclient", s.Module.ImportPath, strings.ToLower(s.Name)) 84 } 85 86 // TestPackagePath returns the import path to the testpackage for this 87 // service. 88 func (s *Svc) TestPackagePath() string { 89 return fmt.Sprintf("%s/%stest", s.Module.ImportPath, strings.ToLower(s.Name)) 90 } 91 92 // FxPackagePath returns the import path to the Fx package for this service. 93 func (s *Svc) FxPackagePath() string { 94 return fmt.Sprintf("%s/%sfx", s.Module.ImportPath, strings.ToLower(s.Name)) 95 } 96 97 // serviceTemplateData contains the data for code gen templates that operate on 98 // a Thrift service. 99 type serviceTemplateData struct { 100 *Svc 101 102 ContextImportPath string 103 UnaryWrapperImport string 104 UnaryWrapperFunc string 105 OnewayWrapperImport string 106 OnewayWrapperFunc string 107 SanitizeTChannel bool 108 } 109 110 // moduleTemplateData contains the data for code gen templates. This should be 111 // used by templates that operate on types 112 // 113 // use serviceTemplateData for generators that rely on service definitions 114 type moduleTemplateData struct { 115 Module *api.Module 116 117 ContextImportPath string 118 } 119 120 // ParentServerPackagePath returns the import path for the immediate parent 121 // service's YARPC server package or an empty string if this service doesn't 122 // extend another service. 123 func (d *serviceTemplateData) ParentServerPackagePath() string { 124 if len(d.Parents) == 0 { 125 return "" 126 } 127 return d.Parents[0].ServerPackagePath() 128 } 129 130 // ParentClientPackagePath returns the import path for the immediate parent 131 // service's YARPC client package or an empty string if this service doesn't 132 // extend another service. 133 func (d *serviceTemplateData) ParentClientPackagePath() string { 134 if len(d.Parents) == 0 { 135 return "" 136 } 137 return d.Parents[0].ClientPackagePath() 138 } 139 140 // moduleGenFunc is a function that generates some part of the code needed by the 141 // plugin. 142 type moduleGenFunc func(*moduleTemplateData, map[string][]byte) error 143 144 // serviceGenFunc is a function that generates some part of the code needed by the 145 // plugin. 146 type serviceGenFunc func(*serviceTemplateData, map[string][]byte) error 147 148 // Default options for the template 149 var templateOptions = []plugin.TemplateOption{ 150 plugin.TemplateFunc("lower", strings.ToLower), 151 }