go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/cmd/svcmux/template.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "text/template" 19 20 "go.chromium.org/luci/grpc/internal/svctool" 21 ) 22 23 var ( 24 tmpl = template.Must(template.New("").Parse( 25 `// Code generated by svcmux; DO NOT EDIT 26 27 package {{.PackageName}} 28 29 import ( 30 "context" 31 32 svcmux "go.chromium.org/luci/server/svcmux" 33 34 {{range .ExtraImports}} 35 {{.Name}} "{{.Path}}"{{end}} 36 ) 37 38 {{range .Services}} 39 {{$StructName := .StructName}} 40 type {{$StructName}} struct { 41 // Default is the version used if {{$.VersionMetadataKey}} metadata 42 // is not present. 43 Default string 44 // Impls maps versions to service implementations. 45 Impls map[string]{{.Service.TypeName}} 46 } 47 48 {{range .Methods}} 49 func (s *{{$StructName}}) {{.Name}}(c context.Context, req {{.InputType}}) ({{.OutputType}}, error) { 50 ver := svcmux.ServiceVersion(c, s.Default) 51 impl := s.Impls[ver] 52 if impl == nil { 53 return nil, svcmux.NoImplementation(ver) 54 } 55 return impl.{{.Name}}(c, req) 56 } 57 {{end}} 58 {{end}} 59 `)) 60 ) 61 62 type ( 63 templateArgs struct { 64 PackageName string 65 Services []*service 66 VersionMetadataKey string 67 ExtraImports []svctool.Import 68 } 69 70 service struct { 71 *svctool.Service 72 StructName string 73 } 74 )