go.uber.org/yarpc@v1.72.1/internal/protoplugin-v2/multi_runner.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 protopluginv2 22 23 import ( 24 "errors" 25 "fmt" 26 "sort" 27 28 "github.com/golang/protobuf/protoc-gen-go/plugin" 29 "go.uber.org/multierr" 30 ) 31 32 var errNoFileName = errors.New("no name on CodeGeneratorResponse_File") 33 34 type multiRunner struct { 35 runners []Runner 36 } 37 38 func newMultiRunner(runners ...Runner) *multiRunner { 39 return &multiRunner{runners: runners} 40 } 41 42 func (m *multiRunner) Run(request *plugin_go.CodeGeneratorRequest) *plugin_go.CodeGeneratorResponse { 43 nameToFile := make(map[string]*plugin_go.CodeGeneratorResponse_File) 44 var responseErr error 45 for _, runner := range m.runners { 46 response := runner.Run(request) 47 if responseErrString := response.GetError(); responseErrString != "" { 48 responseErr = multierr.Append(responseErr, errors.New(responseErrString)) 49 continue 50 } 51 for _, file := range response.GetFile() { 52 name := file.GetName() 53 if name == "" { 54 return newResponseError(errNoFileName) 55 } 56 if _, ok := nameToFile[name]; ok { 57 return newResponseError(newErrorDuplicateFileName(name)) 58 } 59 nameToFile[name] = file 60 } 61 } 62 if responseErr != nil { 63 return newResponseError(responseErr) 64 } 65 files := make([]*plugin_go.CodeGeneratorResponse_File, 0, len(nameToFile)) 66 for _, file := range nameToFile { 67 files = append(files, file) 68 } 69 sort.Slice(files, func(i int, j int) bool { return files[i].GetName() < files[j].GetName() }) 70 return newResponseFiles(files) 71 } 72 73 func newErrorDuplicateFileName(name string) error { 74 return fmt.Errorf("duplicate name for CodeGeneratorResponse_File: %s", name) 75 }