github.com/s7techlab/cckit@v0.10.5/gateway/protoc-gen-cc-gateway/generator/template_func.go (about)

     1  package generator
     2  
     3  import (
     4  	"strings"
     5  	"text/template"
     6  
     7  	"github.com/golang/protobuf/protoc-gen-go/generator"
     8  	"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
     9  )
    10  
    11  var (
    12  	funcMap = template.FuncMap{
    13  		"goTypeName":      goTypeName,
    14  		"hasBindings":     hasBindings,
    15  		"hasGetBinding":   hasGetBinding,
    16  		"removeExtension": removeExtension,
    17  	}
    18  )
    19  
    20  func hasBindings(service *descriptor.Service) bool {
    21  	for _, m := range service.Methods {
    22  		if len(m.Bindings) > 0 {
    23  			return true
    24  		}
    25  	}
    26  	return false
    27  }
    28  
    29  func hasGetBinding(method *descriptor.Method) bool {
    30  	for _, b := range method.Bindings {
    31  		if b.HTTPMethod == "GET" {
    32  			return true
    33  		}
    34  	}
    35  	return false
    36  }
    37  
    38  func removeExtension(fileName string) string {
    39  	startPos := 0
    40  	slashPos := strings.LastIndex(fileName, `/`)
    41  
    42  	if slashPos != -1 {
    43  		startPos = slashPos + 1
    44  	}
    45  	return fileName[startPos:strings.LastIndex(fileName, `.`)]
    46  }
    47  
    48  func goTypeName(s string) string {
    49  	toks := strings.Split(s, ".")
    50  	i := 0
    51  	if len(toks) > 1 {
    52  		i = 1
    53  	}
    54  	for pos := range toks[i:] {
    55  		toks[pos+i] = generator.CamelCase(toks[pos+i])
    56  	}
    57  	return strings.Join(toks, ".")
    58  }