go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/cmd/svcdec/main.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 // Command svcdec stands for 'service decorator'. It reads a service interface 16 // XYZServer generated by protoc and generates DecoratedXYZ with this structure: 17 // 18 // type DecoratedXYZ struct { 19 // Service XYZServer 20 // Prelude func(ctx context.Context, methodName string, req proto.Message) (context.Context, error) 21 // } 22 // 23 // DecoratedXYZ has the same methods as XYZServer: they call Prelude before 24 // forwarding the call to the corresponding XYZServer method. 25 // 26 // svcdec is designed to be run through go generate: 27 // 28 // //go:generate svcdec -type GreetServer 29 package main 30 31 import ( 32 "context" 33 "os" 34 "strings" 35 36 "go.chromium.org/luci/grpc/internal/svctool" 37 ) 38 39 func generate(ctx context.Context, a *svctool.GeneratorArgs) error { 40 args := templateArgs{ 41 PackageName: a.PackageName, 42 ExtraImports: a.ExtraImports, 43 } 44 for _, svc := range a.Services { 45 args.Services = append(args.Services, &service{ 46 Service: svc, 47 StructName: "Decorated" + strings.TrimSuffix(svc.TypeName, "Server"), 48 }) 49 } 50 51 // Execute template. 52 return tmpl.Execute(a.Out, args) 53 } 54 55 func tool() *svctool.Tool { 56 return &svctool.Tool{Name: "svcdec", OutputFilenameSuffix: "dec"} 57 } 58 59 func main() { 60 tool().Main(os.Args[1:], generate) 61 }