go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/cmd/svcmux/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 svcmux reads a service interface XYZServer generated by protoc
    16  // and generates VersionedXYZV struct that stores a map version->implementation,
    17  // and a default version. Each method forwards a request to the implementation
    18  // of the requested version.
    19  //
    20  // Example of usage:
    21  //
    22  //	//go:generate svcmux -type GreetServer
    23  package main
    24  
    25  import (
    26  	"context"
    27  	"os"
    28  	"strings"
    29  
    30  	"go.chromium.org/luci/grpc/internal/svctool"
    31  	"go.chromium.org/luci/grpc/svcmux"
    32  )
    33  
    34  func generate(ctx context.Context, a *svctool.GeneratorArgs) error {
    35  	args := templateArgs{
    36  		PackageName:        a.PackageName,
    37  		VersionMetadataKey: svcmux.VersionMetadataKey,
    38  		ExtraImports:       a.ExtraImports,
    39  	}
    40  	for _, svc := range a.Services {
    41  		args.Services = append(args.Services, &service{
    42  			Service:    svc,
    43  			StructName: "Versioned" + strings.TrimSuffix(svc.TypeName, "Server"),
    44  		})
    45  	}
    46  
    47  	// Execute template.
    48  	return tmpl.Execute(a.Out, args)
    49  }
    50  
    51  func tool() *svctool.Tool {
    52  	return &svctool.Tool{Name: "svcmux", OutputFilenameSuffix: "mux"}
    53  }
    54  
    55  func main() {
    56  	tool().Main(os.Args[1:], generate)
    57  }