github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/protobuf/protoc-gen-go-grain/template.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	_ "embed"
     6  	"strings"
     7  	"text/template"
     8  
     9  	"github.com/asynkron/protoactor-go/protobuf/protoc-gen-go-grain/options"
    10  )
    11  
    12  //go:embed templates/grain.tmpl
    13  var grainTemplate string
    14  
    15  //go:embed templates/error.tmpl
    16  var errorTemplate string
    17  
    18  type serviceDesc struct {
    19  	Name    string // Greeter
    20  	Methods []*methodDesc
    21  }
    22  
    23  type methodDesc struct {
    24  	Name    string
    25  	Input   string
    26  	Output  string
    27  	Index   int
    28  	Options *options.MethodOptions
    29  }
    30  
    31  type errorDesc struct {
    32  	Name       string
    33  	Value      string
    34  	CamelValue string
    35  	Comment    string
    36  	HasComment bool
    37  }
    38  
    39  type errorsWrapper struct {
    40  	Errors []*errorDesc
    41  }
    42  
    43  func (es *errorsWrapper) execute() string {
    44  	buf := new(bytes.Buffer)
    45  	tmpl, err := template.New("error").Parse(strings.TrimSpace(errorTemplate))
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	if err := tmpl.Execute(buf, es); err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	return strings.Trim(buf.String(), "\r\n")
    54  }
    55  
    56  func (s *serviceDesc) execute() string {
    57  	buf := new(bytes.Buffer)
    58  	tmpl, err := template.New("grain").Parse(strings.TrimSpace(grainTemplate))
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  	if err := tmpl.Execute(buf, s); err != nil {
    63  		panic(err)
    64  	}
    65  
    66  	return strings.Trim(buf.String(), "\r\n")
    67  }