go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/cmd/svcdec/template.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  package main
    16  
    17  import (
    18  	"text/template"
    19  
    20  	"go.chromium.org/luci/grpc/internal/svctool"
    21  )
    22  
    23  var (
    24  	tmpl = template.Must(template.New("").Parse(
    25  		`// Code generated by svcdec; DO NOT EDIT.
    26  
    27  package {{.PackageName}}
    28  
    29  import (
    30  	"context"
    31  
    32  	proto "github.com/golang/protobuf/proto"
    33  
    34  	{{range .ExtraImports}}
    35  	{{.Name}} "{{.Path}}"{{end}}
    36  )
    37  
    38  {{range .Services}}
    39  {{$StructName := .StructName}}
    40  type {{$StructName}} struct {
    41  	// Service is the service to decorate.
    42  	Service {{.Service.TypeName}}
    43  	// Prelude is called for each method before forwarding the call to Service.
    44  	// If Prelude returns an error, then the call is skipped and the error is
    45  	// processed via the Postlude (if one is defined), or it is returned directly.
    46  	Prelude func(ctx context.Context, methodName string, req proto.Message) (context.Context, error)
    47  	// Postlude is called for each method after Service has processed the call, or
    48  	// after the Prelude has returned an error. This takes the Service's
    49  	// response proto (which may be nil) and/or any error. The decorated
    50  	// service will return the response (possibly mutated) and error that Postlude
    51  	// returns.
    52  	Postlude func(ctx context.Context, methodName string, rsp proto.Message, err error) error
    53  }
    54  
    55  {{range .Methods}}
    56  func (s *{{$StructName}}) {{.Name}}(ctx context.Context, req {{.InputType}}) (rsp {{.OutputType}}, err error) {
    57  	if s.Prelude != nil {
    58  		var newCtx context.Context
    59  		newCtx, err = s.Prelude(ctx, "{{.Name}}", req)
    60  		if err == nil {
    61  			ctx = newCtx
    62  		}
    63  	}
    64  	if err == nil {
    65  		rsp, err = s.Service.{{.Name}}(ctx, req)
    66  	}
    67  	if s.Postlude != nil {
    68  		err = s.Postlude(ctx, "{{.Name}}", rsp, err)
    69  	}
    70  	return
    71  }
    72  {{end}}
    73  {{end}}
    74  `))
    75  )
    76  
    77  type (
    78  	templateArgs struct {
    79  		PackageName  string
    80  		Services     []*service
    81  		ExtraImports []svctool.Import
    82  	}
    83  
    84  	service struct {
    85  		*svctool.Service
    86  		StructName string
    87  	}
    88  )