go.uber.org/yarpc@v1.72.1/internal/protoplugin/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 protoplugin
    22  
    23  import (
    24  	"strings"
    25  	"text/template"
    26  
    27  	"github.com/gogo/protobuf/proto"
    28  	"github.com/gogo/protobuf/protoc-gen-gogo/plugin"
    29  )
    30  
    31  type runner struct {
    32  	tmpl                 *template.Template
    33  	templateInfoChecker  func(*TemplateInfo) error
    34  	baseImports          []string
    35  	fileToOutputFilename func(*File) (string, error)
    36  	unknownFlagHandler   func(key string, value string) error
    37  }
    38  
    39  func newRunner(
    40  	tmpl *template.Template,
    41  	templateInfoChecker func(*TemplateInfo) error,
    42  	baseImports []string,
    43  	fileToOutputFilename func(*File) (string, error),
    44  	unknownFlagHandler func(key string, value string) error,
    45  ) *runner {
    46  	return &runner{
    47  		tmpl:                 tmpl,
    48  		templateInfoChecker:  templateInfoChecker,
    49  		baseImports:          baseImports,
    50  		fileToOutputFilename: fileToOutputFilename,
    51  		unknownFlagHandler:   unknownFlagHandler,
    52  	}
    53  }
    54  
    55  func (r *runner) Run(request *plugin_go.CodeGeneratorRequest) *plugin_go.CodeGeneratorResponse {
    56  	registry := newRegistry()
    57  	if request.Parameter != nil {
    58  		for _, p := range strings.Split(request.GetParameter(), ",") {
    59  			spec := strings.SplitN(p, "=", 2)
    60  			if len(spec) == 1 {
    61  				continue
    62  			}
    63  			name, value := spec[0], spec[1]
    64  			switch {
    65  			case name == "import_prefix":
    66  				registry.SetPrefix(value)
    67  			case strings.HasPrefix(name, "M"):
    68  				registry.AddPackageMap(name[1:], value)
    69  			default:
    70  				if r.unknownFlagHandler != nil {
    71  					if err := r.unknownFlagHandler(name, value); err != nil {
    72  						return newResponseError(err)
    73  					}
    74  				}
    75  			}
    76  		}
    77  	}
    78  
    79  	generator := newGenerator(
    80  		registry,
    81  		r.tmpl,
    82  		r.templateInfoChecker,
    83  		r.baseImports,
    84  		r.fileToOutputFilename,
    85  	)
    86  	if err := registry.Load(request); err != nil {
    87  		return newResponseError(err)
    88  	}
    89  
    90  	var targets []*File
    91  	for _, target := range request.FileToGenerate {
    92  		file, err := registry.LookupFile(target)
    93  		if err != nil {
    94  			return newResponseError(err)
    95  		}
    96  		targets = append(targets, file)
    97  	}
    98  
    99  	out, err := generator.Generate(targets)
   100  	if err != nil {
   101  		return newResponseError(err)
   102  	}
   103  	return newResponseFiles(out)
   104  }
   105  
   106  func newResponseFiles(files []*plugin_go.CodeGeneratorResponse_File) *plugin_go.CodeGeneratorResponse {
   107  	return &plugin_go.CodeGeneratorResponse{File: files}
   108  }
   109  
   110  func newResponseError(err error) *plugin_go.CodeGeneratorResponse {
   111  	return &plugin_go.CodeGeneratorResponse{Error: proto.String(err.Error())}
   112  }