github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/protoc-gen-toolkit/gentoolkit/template.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package gentoolkit
    18  
    19  import (
    20  	"bytes"
    21  	"text/template"
    22  
    23  	"github.com/lastbackend/toolkit/pkg/util/converter"
    24  	"github.com/lastbackend/toolkit/pkg/util/strings"
    25  	"github.com/lastbackend/toolkit/protoc-gen-toolkit/descriptor"
    26  	"github.com/lastbackend/toolkit/protoc-gen-toolkit/gentoolkit/templates"
    27  )
    28  
    29  var (
    30  	funcMap = template.FuncMap{
    31  		"ToUpper":                       strings.ToUpper,
    32  		"ToLower":                       strings.ToLower,
    33  		"ToCapitalize":                  strings.Title,
    34  		"ToCamel":                       strings.ToCamel,
    35  		"ToTrimRegexFromQueryParameter": converter.ToTrimRegexFromQueryParameter,
    36  		"dehyphen":                      strings.DeHyphenFunc,
    37  		"lowerhyphen":                   strings.LowerHyphenFunc,
    38  		"tohyphen":                      strings.ToHyphen,
    39  		"inc": func(n int) int {
    40  			return n + 1
    41  		},
    42  	}
    43  )
    44  
    45  var (
    46  	messageTemplate  = template.Must(template.New("message").Parse(templates.MessageTpl))
    47  	headerTemplate   = template.Must(template.New("header").Parse(templates.HeaderTpl))
    48  	clientTemplate   = template.Must(template.New("client-content").Funcs(funcMap).Parse(templates.ClientTpl))
    49  	testStubTemplate = template.Must(template.New("stub-content-mockery").Parse(templates.TestTpl))
    50  	serviceTemplate  = template.Must(template.New("content").Funcs(funcMap).Parse(templates.ServiceContentTpl))
    51  	_                = template.Must(serviceTemplate.New("grpc-service-define").Parse(templates.ServerGRPCDefineTpl))
    52  	_                = template.Must(serviceTemplate.New("http-service-define").Parse(templates.ServiceInterfaceDefineTpl))
    53  	_                = template.Must(serviceTemplate.New("http-handler-define").Parse(templates.ServerHTTPDefineTpl))
    54  	_                = template.Must(serviceTemplate.New("plugin-define").Parse(templates.PluginDefineTpl))
    55  	_                = template.Must(serviceTemplate.New("plugin-init").Parse(templates.PluginInitializeTpl))
    56  	_                = template.Must(serviceTemplate.New("plugin-register").Parse(templates.PluginRegisterTpl))
    57  )
    58  
    59  type Client struct {
    60  	Service string
    61  	Pkg     string
    62  }
    63  
    64  type contentServiceParams struct {
    65  	DefinitionPlugins map[string][]*descriptor.Plugin
    66  	Plugins           map[string][]*descriptor.Plugin
    67  	Services          []*descriptor.Service
    68  	Clients           map[string]*Client
    69  }
    70  
    71  type tplServiceOptions struct {
    72  	*descriptor.File
    73  	Imports           []descriptor.GoPackage
    74  	Plugins           map[string][]*descriptor.Plugin
    75  	DefinitionPlugins map[string][]*descriptor.Plugin
    76  	Clients           map[string]*Client
    77  }
    78  
    79  func applyServiceTemplate(to tplServiceOptions) (string, error) {
    80  	w := bytes.NewBuffer(nil)
    81  
    82  	if err := headerTemplate.Execute(w, to); err != nil {
    83  		return "", err
    84  	}
    85  
    86  	var targetServices = make([]*descriptor.Service, 0)
    87  	for _, msg := range to.Messages {
    88  		msgName := camel(*msg.Name)
    89  		msg.Name = &msgName
    90  	}
    91  
    92  	for _, svc := range to.Services {
    93  		svcName := camel(*svc.Name)
    94  		svc.Name = &svcName
    95  		targetServices = append(targetServices, svc)
    96  	}
    97  
    98  	tp := contentServiceParams{
    99  		Plugins:           to.Plugins,
   100  		DefinitionPlugins: to.DefinitionPlugins,
   101  		Clients:           to.Clients,
   102  		Services:          targetServices,
   103  	}
   104  
   105  	if err := serviceTemplate.Execute(w, tp); err != nil {
   106  		return "", err
   107  	}
   108  
   109  	return w.String(), nil
   110  }
   111  
   112  type tplClientOptions struct {
   113  	*descriptor.File
   114  	Imports []descriptor.GoPackage
   115  	Clients map[string]*Client
   116  }
   117  
   118  type contentClientParams struct {
   119  	HasNotServiceGenerate bool
   120  	Clients               map[string]*Client
   121  	Services              []*descriptor.Service
   122  }
   123  
   124  func applyClientTemplate(to tplClientOptions) (string, error) {
   125  	w := bytes.NewBuffer(nil)
   126  
   127  	if err := headerTemplate.Execute(w, to); err != nil {
   128  		return "", err
   129  	}
   130  
   131  	var targetServices = make([]*descriptor.Service, 0)
   132  	for _, msg := range to.Messages {
   133  		msgName := camel(*msg.Name)
   134  		msg.Name = &msgName
   135  	}
   136  
   137  	for _, svc := range to.Services {
   138  		svcName := camel(*svc.Name)
   139  		svc.Name = &svcName
   140  		targetServices = append(targetServices, svc)
   141  	}
   142  
   143  	tp := contentClientParams{
   144  		Clients:  to.Clients,
   145  		Services: targetServices,
   146  	}
   147  
   148  	if err := clientTemplate.Execute(w, tp); err != nil {
   149  		return "", err
   150  	}
   151  
   152  	return w.String(), nil
   153  }
   154  
   155  type tplMockeryTestOptions struct {
   156  	*descriptor.File
   157  	Imports []descriptor.GoPackage
   158  }
   159  
   160  func applyTestTemplate(to tplMockeryTestOptions) (string, error) {
   161  	w := bytes.NewBuffer(nil)
   162  
   163  	if err := headerTemplate.Execute(w, to); err != nil {
   164  		return "", err
   165  	}
   166  
   167  	if err := testStubTemplate.Execute(w, to); err != nil {
   168  		return "", err
   169  	}
   170  
   171  	return w.String(), nil
   172  }
   173  
   174  type tplMessageOptions struct {
   175  	*descriptor.File
   176  	Message string
   177  }
   178  
   179  func applyTemplateWithMessage(to tplMessageOptions) (string, error) {
   180  	w := bytes.NewBuffer(nil)
   181  
   182  	if err := messageTemplate.Execute(w, to); err != nil {
   183  		return "", err
   184  	}
   185  
   186  	return w.String(), nil
   187  }