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