2017-05-19 10:02:40 +02:00
|
|
|
package pgghelpers
|
2016-12-15 13:28:57 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-12-22 22:41:17 +00:00
|
|
|
"fmt"
|
2018-02-10 16:52:51 +01:00
|
|
|
"reflect"
|
2017-04-13 13:15:40 +02:00
|
|
|
"regexp"
|
2016-12-16 16:08:37 +01:00
|
|
|
"strings"
|
2018-08-23 18:48:43 +02:00
|
|
|
"sync"
|
2016-12-15 13:28:57 +01:00
|
|
|
"text/template"
|
|
|
|
|
2021-01-08 22:22:43 +03:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
2016-12-22 22:41:17 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2016-12-21 09:26:26 +00:00
|
|
|
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
2017-05-18 19:44:05 +02:00
|
|
|
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
2021-01-08 22:22:43 +03:00
|
|
|
openapi_options "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options"
|
2017-05-18 19:44:05 +02:00
|
|
|
"github.com/huandu/xstrings"
|
2017-03-31 18:01:58 +02:00
|
|
|
options "google.golang.org/genproto/googleapis/api/annotations"
|
2021-01-08 22:22:43 +03:00
|
|
|
// "google.golang.org/protobuf/proto"
|
2016-12-15 13:28:57 +01:00
|
|
|
)
|
|
|
|
|
2017-12-25 09:17:09 +01:00
|
|
|
var jsReservedRe = regexp.MustCompile(`(^|[^A-Za-z])(do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)($|[^A-Za-z])`)
|
2017-04-13 13:15:40 +02:00
|
|
|
|
2017-05-19 10:02:40 +02:00
|
|
|
var (
|
|
|
|
registry *ggdescriptor.Registry // some helpers need access to registry
|
|
|
|
)
|
|
|
|
|
2021-01-08 22:22:43 +03:00
|
|
|
type HttpOption struct {
|
2021-01-30 19:34:30 +03:00
|
|
|
Path string
|
|
|
|
Method string
|
|
|
|
Body string
|
|
|
|
Additional []*HttpOption
|
2021-01-08 22:22:43 +03:00
|
|
|
}
|
|
|
|
|
2016-12-15 13:28:57 +01:00
|
|
|
var ProtoHelpersFuncMap = template.FuncMap{
|
|
|
|
"string": func(i interface {
|
|
|
|
String() string
|
|
|
|
}) string {
|
|
|
|
return i.String()
|
|
|
|
},
|
|
|
|
"json": func(v interface{}) string {
|
2017-12-25 09:17:09 +01:00
|
|
|
a, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
2016-12-15 13:28:57 +01:00
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
"prettyjson": func(v interface{}) string {
|
2017-12-25 09:17:09 +01:00
|
|
|
a, err := json.MarshalIndent(v, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
2016-12-15 13:28:57 +01:00
|
|
|
return string(a)
|
|
|
|
},
|
2021-01-12 13:39:43 +03:00
|
|
|
"splitArray": func(sep string, s string) []string {
|
|
|
|
var r []string
|
|
|
|
parts := strings.Split(s, sep)
|
|
|
|
for _, part := range parts {
|
|
|
|
if part != "" {
|
|
|
|
r = append(r, part)
|
2017-06-22 13:35:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
2016-12-16 16:08:37 +01:00
|
|
|
},
|
|
|
|
"first": func(a []string) string {
|
2016-12-15 13:28:57 +01:00
|
|
|
return a[0]
|
|
|
|
},
|
2016-12-16 16:08:37 +01:00
|
|
|
"last": func(a []string) string {
|
2016-12-15 13:28:57 +01:00
|
|
|
return a[len(a)-1]
|
|
|
|
},
|
2018-04-06 16:42:23 +02:00
|
|
|
"concat": func(a string, b ...string) string {
|
|
|
|
return strings.Join(append([]string{a}, b...), "")
|
|
|
|
},
|
|
|
|
"join": func(sep string, a ...string) string {
|
|
|
|
return strings.Join(a, sep)
|
|
|
|
},
|
2021-01-08 22:22:43 +03:00
|
|
|
"joinQuotes": func(a []string) string {
|
|
|
|
return fmt.Sprintf(`"%s"`, strings.Join(a, `", "`))
|
|
|
|
},
|
2016-12-19 15:43:38 +01:00
|
|
|
"upperFirst": func(s string) string {
|
|
|
|
return strings.ToUpper(s[:1]) + s[1:]
|
|
|
|
},
|
2016-12-16 16:08:37 +01:00
|
|
|
"lowerFirst": func(s string) string {
|
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
|
|
},
|
2016-12-20 11:31:46 +01:00
|
|
|
"camelCase": func(s string) string {
|
2017-01-03 23:41:58 +01:00
|
|
|
if len(s) > 1 {
|
|
|
|
return xstrings.ToCamelCase(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToUpper(s[:1])
|
2016-12-20 11:31:46 +01:00
|
|
|
},
|
2017-01-11 16:33:03 +01:00
|
|
|
"lowerCamelCase": func(s string) string {
|
|
|
|
if len(s) > 1 {
|
|
|
|
s = xstrings.ToCamelCase(s)
|
|
|
|
}
|
2017-01-03 23:41:58 +01:00
|
|
|
|
2017-01-11 16:33:03 +01:00
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
|
|
},
|
2020-11-03 13:39:24 -06:00
|
|
|
"upperCase": func(s string) string {
|
|
|
|
return strings.ToUpper(s)
|
|
|
|
},
|
2016-12-20 11:31:46 +01:00
|
|
|
"kebabCase": func(s string) string {
|
|
|
|
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1)
|
|
|
|
},
|
2017-06-26 11:34:02 +02:00
|
|
|
"contains": func(sub, s string) bool {
|
|
|
|
return strings.Contains(s, sub)
|
|
|
|
},
|
|
|
|
"trimstr": func(cutset, s string) string {
|
|
|
|
return strings.Trim(s, cutset)
|
|
|
|
},
|
2019-11-27 19:30:42 +03:00
|
|
|
"index": func(array interface{}, i int) interface{} {
|
2018-02-10 16:52:51 +01:00
|
|
|
slice := reflect.ValueOf(array)
|
|
|
|
if slice.Kind() != reflect.Slice {
|
|
|
|
panic("Error in index(): given a non-slice type")
|
|
|
|
}
|
2019-11-27 19:30:42 +03:00
|
|
|
if i < 0 || i >= slice.Len() {
|
2018-02-10 16:52:51 +01:00
|
|
|
panic("Error in index(): index out of bounds")
|
|
|
|
}
|
2019-11-27 19:30:42 +03:00
|
|
|
return slice.Index(i).Interface()
|
2018-02-10 16:52:51 +01:00
|
|
|
},
|
2018-04-06 16:42:23 +02:00
|
|
|
"add": func(a int, b int) int {
|
|
|
|
return a + b
|
|
|
|
},
|
|
|
|
"subtract": func(a int, b int) int {
|
|
|
|
return a - b
|
|
|
|
},
|
|
|
|
"multiply": func(a int, b int) int {
|
|
|
|
return a * b
|
|
|
|
},
|
|
|
|
"divide": func(a int, b int) int {
|
|
|
|
if b == 0 {
|
|
|
|
panic("psssst ... little help here ... you cannot divide by 0")
|
|
|
|
}
|
|
|
|
return a / b
|
|
|
|
},
|
2021-02-02 11:13:29 +03:00
|
|
|
"lenght": func(a []interface{}) int {
|
|
|
|
return len(a)
|
|
|
|
},
|
2018-02-23 16:55:45 +01:00
|
|
|
"snakeCase": xstrings.ToSnakeCase,
|
|
|
|
"getProtoFile": getProtoFile,
|
|
|
|
"getMessageType": getMessageType,
|
|
|
|
"getEnumValue": getEnumValue,
|
|
|
|
"isFieldMessage": isFieldMessage,
|
|
|
|
"isFieldMessageTimeStamp": isFieldMessageTimeStamp,
|
|
|
|
"isFieldRepeated": isFieldRepeated,
|
|
|
|
"haskellType": haskellType,
|
|
|
|
"goType": goType,
|
|
|
|
"goZeroValue": goZeroValue,
|
|
|
|
"goTypeWithPackage": goTypeWithPackage,
|
2018-08-09 10:23:10 +02:00
|
|
|
"goTypeWithGoPackage": goTypeWithGoPackage,
|
2018-02-23 16:55:45 +01:00
|
|
|
"jsType": jsType,
|
|
|
|
"jsSuffixReserved": jsSuffixReservedKeyword,
|
|
|
|
"namespacedFlowType": namespacedFlowType,
|
2021-01-08 22:22:43 +03:00
|
|
|
"openapiOption": openapiOption,
|
|
|
|
"httpOption": httpOption,
|
2018-02-23 16:55:45 +01:00
|
|
|
"httpVerb": httpVerb,
|
|
|
|
"httpPath": httpPath,
|
|
|
|
"httpPathsAdditionalBindings": httpPathsAdditionalBindings,
|
|
|
|
"httpBody": httpBody,
|
|
|
|
"shortType": shortType,
|
|
|
|
"urlHasVarsFromMessage": urlHasVarsFromMessage,
|
|
|
|
"lowerGoNormalize": lowerGoNormalize,
|
|
|
|
"goNormalize": goNormalize,
|
|
|
|
"leadingComment": leadingComment,
|
|
|
|
"trailingComment": trailingComment,
|
|
|
|
"leadingDetachedComments": leadingDetachedComments,
|
2019-04-26 15:34:25 -03:00
|
|
|
"stringFileOptionsExtension": stringFileOptionsExtension,
|
2019-02-08 19:03:10 +01:00
|
|
|
"stringMessageExtension": stringMessageExtension,
|
2018-02-23 16:55:45 +01:00
|
|
|
"stringFieldExtension": stringFieldExtension,
|
2019-01-18 19:39:16 +01:00
|
|
|
"int64FieldExtension": int64FieldExtension,
|
2019-02-08 19:03:10 +01:00
|
|
|
"int64MessageExtension": int64MessageExtension,
|
2018-02-23 16:55:45 +01:00
|
|
|
"stringMethodOptionsExtension": stringMethodOptionsExtension,
|
2018-08-20 17:41:36 +02:00
|
|
|
"boolMethodOptionsExtension": boolMethodOptionsExtension,
|
2019-02-08 19:03:10 +01:00
|
|
|
"boolMessageExtension": boolMessageExtension,
|
2018-02-23 16:55:45 +01:00
|
|
|
"boolFieldExtension": boolFieldExtension,
|
|
|
|
"isFieldMap": isFieldMap,
|
|
|
|
"fieldMapKeyType": fieldMapKeyType,
|
|
|
|
"fieldMapValueType": fieldMapValueType,
|
|
|
|
"replaceDict": replaceDict,
|
2018-04-06 16:42:23 +02:00
|
|
|
"setStore": setStore,
|
|
|
|
"getStore": getStore,
|
2018-01-17 14:14:39 +01:00
|
|
|
"goPkg": goPkg,
|
|
|
|
"goPkgLastElement": goPkgLastElement,
|
2020-08-27 10:46:14 -05:00
|
|
|
"cppType": cppType,
|
|
|
|
"cppTypeWithPackage": cppTypeWithPackage,
|
2020-11-03 13:39:24 -06:00
|
|
|
"rustType": rustType,
|
|
|
|
"rustTypeWithPackage": rustTypeWithPackage,
|
2018-01-12 10:38:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location
|
|
|
|
|
2018-08-23 18:48:43 +02:00
|
|
|
var store = newStore()
|
|
|
|
|
|
|
|
// Utility to store some vars across multiple scope
|
|
|
|
type globalStore struct {
|
|
|
|
store map[string]interface{}
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStore() *globalStore {
|
|
|
|
return &globalStore{
|
|
|
|
store: make(map[string]interface{}),
|
|
|
|
}
|
2018-04-06 16:42:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:48:43 +02:00
|
|
|
func (s *globalStore) getData(key string) interface{} {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
if v, ok := s.store[key]; ok {
|
2018-04-06 16:42:23 +02:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:41:58 +02:00
|
|
|
return false
|
2018-04-06 16:42:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:48:43 +02:00
|
|
|
func (s *globalStore) setData(key string, o interface{}) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.store[key] = o
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setStore(key string, o interface{}) string {
|
|
|
|
store.setData(key, o)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStore(key string) interface{} {
|
|
|
|
return store.getData(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetRegistry(reg *ggdescriptor.Registry) {
|
|
|
|
registry = reg
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:38:10 -07:00
|
|
|
func InitPathMap(file *descriptor.FileDescriptorProto) {
|
|
|
|
pathMap = make(map[interface{}]*descriptor.SourceCodeInfo_Location)
|
|
|
|
addToPathMap(file.GetSourceCodeInfo(), file, []int32{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitPathMaps(files []*descriptor.FileDescriptorProto) {
|
|
|
|
pathMap = make(map[interface{}]*descriptor.SourceCodeInfo_Location)
|
|
|
|
for _, file := range files {
|
|
|
|
addToPathMap(file.GetSourceCodeInfo(), file, []int32{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:36:29 -07:00
|
|
|
// addToPathMap traverses through the AST adding SourceCodeInfo_Location entries to the pathMap.
|
|
|
|
// Since the AST is a tree, the recursion finishes once it has gone through all the nodes.
|
2018-01-12 10:38:10 -07:00
|
|
|
func addToPathMap(info *descriptor.SourceCodeInfo, i interface{}, path []int32) {
|
|
|
|
loc := findLoc(info, path)
|
|
|
|
if loc != nil {
|
|
|
|
pathMap[i] = loc
|
|
|
|
}
|
|
|
|
switch d := i.(type) {
|
|
|
|
case *descriptor.FileDescriptorProto:
|
|
|
|
for index, descriptor := range d.MessageType {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 4, index))
|
|
|
|
}
|
|
|
|
for index, descriptor := range d.EnumType {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 5, index))
|
|
|
|
}
|
|
|
|
for index, descriptor := range d.Service {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 6, index))
|
|
|
|
}
|
|
|
|
case *descriptor.DescriptorProto:
|
|
|
|
for index, descriptor := range d.Field {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 2, index))
|
|
|
|
}
|
|
|
|
for index, descriptor := range d.NestedType {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 3, index))
|
|
|
|
}
|
|
|
|
for index, descriptor := range d.EnumType {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 4, index))
|
|
|
|
}
|
|
|
|
case *descriptor.EnumDescriptorProto:
|
|
|
|
for index, descriptor := range d.Value {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 2, index))
|
|
|
|
}
|
|
|
|
case *descriptor.ServiceDescriptorProto:
|
|
|
|
for index, descriptor := range d.Method {
|
|
|
|
addToPathMap(info, descriptor, newPath(path, 2, index))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPath(base []int32, field int32, index int) []int32 {
|
|
|
|
p := append([]int32{}, base...)
|
|
|
|
p = append(p, field, int32(index))
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func findLoc(info *descriptor.SourceCodeInfo, path []int32) *descriptor.SourceCodeInfo_Location {
|
|
|
|
for _, loc := range info.GetLocation() {
|
|
|
|
if samePath(loc.Path, path) {
|
|
|
|
return loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func samePath(a, b []int32) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, p := range a {
|
|
|
|
if p != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-19 20:01:03 +01:00
|
|
|
/*func findSourceInfoLocation(i interface{}) *descriptor.SourceCodeInfo_Location {
|
2018-01-12 10:38:10 -07:00
|
|
|
if pathMap == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pathMap[i]
|
2019-01-19 20:01:03 +01:00
|
|
|
}*/
|
2018-01-12 10:38:10 -07:00
|
|
|
|
|
|
|
func leadingComment(i interface{}) string {
|
|
|
|
loc := pathMap[i]
|
|
|
|
return loc.GetLeadingComments()
|
|
|
|
}
|
|
|
|
func trailingComment(i interface{}) string {
|
|
|
|
loc := pathMap[i]
|
|
|
|
return loc.GetTrailingComments()
|
|
|
|
}
|
|
|
|
func leadingDetachedComments(i interface{}) []string {
|
|
|
|
loc := pathMap[i]
|
|
|
|
return loc.GetLeadingDetachedComments()
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:55:45 +01:00
|
|
|
// stringMethodOptionsExtension extracts method options of a string type.
|
|
|
|
// To define your own extensions see:
|
|
|
|
// https://developers.google.com/protocol-buffers/docs/proto#customoptions
|
|
|
|
// Typically the fieldID of private extensions should be in the range:
|
|
|
|
// 50000-99999
|
|
|
|
func stringMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorProto) string {
|
|
|
|
if f == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.MethodOptions
|
|
|
|
var extensionType *string
|
|
|
|
|
2019-04-26 15:34:25 -03:00
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
|
|
|
if eds[fieldID] == nil {
|
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
|
|
|
Field: fieldID,
|
|
|
|
Tag: fmt.Sprintf("bytes,%d", fieldID),
|
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
str, ok := ext.(*string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return *str
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringFileOptionsExtension extracts file options of a string type.
|
|
|
|
// To define your own extensions see:
|
|
|
|
// https://developers.google.com/protocol-buffers/docs/proto#customoptions
|
|
|
|
// Typically the fieldID of private extensions should be in the range:
|
|
|
|
// 50000-99999
|
|
|
|
func stringFileOptionsExtension(fieldID int32, f *descriptor.FileDescriptorProto) string {
|
|
|
|
if f == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.FileOptions
|
|
|
|
var extensionType *string
|
|
|
|
|
2018-02-23 16:55:45 +01:00
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
|
|
|
if eds[fieldID] == nil {
|
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
|
|
|
Field: fieldID,
|
|
|
|
Tag: fmt.Sprintf("bytes,%d", fieldID),
|
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
str, ok := ext.(*string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return *str
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:56:25 -07:00
|
|
|
func stringFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) string {
|
2018-01-12 10:38:10 -07:00
|
|
|
if f == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.FieldOptions
|
|
|
|
var extensionType *string
|
|
|
|
|
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
2018-01-12 10:56:25 -07:00
|
|
|
if eds[fieldID] == nil {
|
2018-01-12 10:38:10 -07:00
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
2018-01-12 10:56:25 -07:00
|
|
|
Field: fieldID,
|
|
|
|
Tag: fmt.Sprintf("bytes,%d", fieldID),
|
2018-01-12 10:38:10 -07:00
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:56:25 -07:00
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
2018-01-12 10:38:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
str, ok := ext.(*string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return *str
|
|
|
|
}
|
|
|
|
|
2019-01-18 19:39:16 +01:00
|
|
|
func int64FieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) int64 {
|
|
|
|
if f == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.FieldOptions
|
2019-02-10 19:50:23 +01:00
|
|
|
var extensionType *int64
|
2019-01-18 19:39:16 +01:00
|
|
|
|
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
|
|
|
if eds[fieldID] == nil {
|
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
|
|
|
Field: fieldID,
|
2019-02-10 19:50:23 +01:00
|
|
|
Tag: fmt.Sprintf("varint,%d", fieldID),
|
2019-01-18 19:39:16 +01:00
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
i, ok := ext.(*int64)
|
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return *i
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:03:10 +01:00
|
|
|
func int64MessageExtension(fieldID int32, f *descriptor.DescriptorProto) int64 {
|
|
|
|
if f == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.MessageOptions
|
2019-02-10 19:50:23 +01:00
|
|
|
var extensionType *int64
|
2019-02-08 19:03:10 +01:00
|
|
|
|
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
|
|
|
if eds[fieldID] == nil {
|
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
|
|
|
Field: fieldID,
|
2019-02-10 19:50:23 +01:00
|
|
|
Tag: fmt.Sprintf("varint,%d", fieldID),
|
2019-02-08 19:03:10 +01:00
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
i, ok := ext.(*int64)
|
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return *i
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringMessageExtension(fieldID int32, f *descriptor.DescriptorProto) string {
|
|
|
|
if f == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.MessageOptions
|
|
|
|
var extensionType *string
|
|
|
|
|
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
|
|
|
if eds[fieldID] == nil {
|
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
|
|
|
Field: fieldID,
|
|
|
|
Tag: fmt.Sprintf("bytes,%d", fieldID),
|
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
str, ok := ext.(*string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return *str
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:41:36 +02:00
|
|
|
func boolMethodOptionsExtension(fieldID int32, f *descriptor.MethodDescriptorProto) bool {
|
|
|
|
if f == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.MethodOptions
|
|
|
|
var extensionType *bool
|
|
|
|
|
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
|
|
|
if eds[fieldID] == nil {
|
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
|
|
|
Field: fieldID,
|
2019-01-18 19:39:16 +01:00
|
|
|
Tag: fmt.Sprintf("varint,%d", fieldID),
|
2018-08-20 17:41:36 +02:00
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
b, ok := ext.(*bool)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return *b
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:56:25 -07:00
|
|
|
func boolFieldExtension(fieldID int32, f *descriptor.FieldDescriptorProto) bool {
|
2018-01-12 10:38:10 -07:00
|
|
|
if f == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.FieldOptions
|
|
|
|
var extensionType *bool
|
|
|
|
|
2019-02-08 19:03:10 +01:00
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
|
|
|
if eds[fieldID] == nil {
|
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
|
|
|
Field: fieldID,
|
|
|
|
Tag: fmt.Sprintf("varint,%d", fieldID),
|
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
b, ok := ext.(*bool)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return *b
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolMessageExtension(fieldID int32, f *descriptor.DescriptorProto) bool {
|
|
|
|
if f == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.Options == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var extendedType *descriptor.MessageOptions
|
|
|
|
var extensionType *bool
|
|
|
|
|
2018-01-12 10:38:10 -07:00
|
|
|
eds := proto.RegisteredExtensions(f.Options)
|
2018-01-12 10:56:25 -07:00
|
|
|
if eds[fieldID] == nil {
|
2018-01-12 10:38:10 -07:00
|
|
|
ed := &proto.ExtensionDesc{
|
|
|
|
ExtendedType: extendedType,
|
|
|
|
ExtensionType: extensionType,
|
2018-01-12 10:56:25 -07:00
|
|
|
Field: fieldID,
|
|
|
|
Tag: fmt.Sprintf("varint,%d", fieldID),
|
2018-01-12 10:38:10 -07:00
|
|
|
}
|
|
|
|
proto.RegisterExtension(ed)
|
|
|
|
eds = proto.RegisteredExtensions(f.Options)
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:56:25 -07:00
|
|
|
ext, err := proto.GetExtension(f.Options, eds[fieldID])
|
2018-01-12 10:38:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:41:36 +02:00
|
|
|
b, ok := ext.(*bool)
|
2018-01-12 10:38:10 -07:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:41:36 +02:00
|
|
|
return *b
|
2016-12-15 13:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for k, v := range sprig.TxtFuncMap() {
|
|
|
|
ProtoHelpersFuncMap[k] = v
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 09:26:26 +00:00
|
|
|
|
2017-05-18 19:44:05 +02:00
|
|
|
func getProtoFile(name string) *ggdescriptor.File {
|
|
|
|
if registry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
file, err := registry.LookupFile(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMessageType(f *descriptor.FileDescriptorProto, name string) *ggdescriptor.Message {
|
|
|
|
if registry != nil {
|
|
|
|
msg, err := registry.LookupMsg(".", name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2016-12-27 17:56:44 +01:00
|
|
|
// name is in the form .packageName.MessageTypeName.InnerMessageTypeName...
|
|
|
|
// e.g. .article.ProductTag
|
|
|
|
splits := strings.Split(name, ".")
|
|
|
|
target := splits[len(splits)-1]
|
2016-12-21 09:26:26 +00:00
|
|
|
for _, m := range f.MessageType {
|
2016-12-27 17:56:44 +01:00
|
|
|
if target == *m.Name {
|
2017-05-18 19:44:05 +02:00
|
|
|
return &ggdescriptor.Message{
|
|
|
|
DescriptorProto: m,
|
|
|
|
}
|
2016-12-21 09:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:46:18 +02:00
|
|
|
func getEnumValue(f []*descriptor.EnumDescriptorProto, name string) []*descriptor.EnumValueDescriptorProto {
|
|
|
|
for _, item := range f {
|
|
|
|
if strings.EqualFold(*item.Name, name) {
|
|
|
|
return item.GetValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:04:39 +02:00
|
|
|
func isFieldMessageTimeStamp(f *descriptor.FieldDescriptorProto) bool {
|
|
|
|
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
2017-09-18 18:56:37 +02:00
|
|
|
if strings.Compare(*f.TypeName, ".google.protobuf.Timestamp") == 0 {
|
2017-09-18 15:04:39 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-21 09:26:26 +00:00
|
|
|
func isFieldMessage(f *descriptor.FieldDescriptorProto) bool {
|
2016-12-29 17:21:53 +01:00
|
|
|
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
2016-12-21 09:26:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
|
2016-12-26 12:13:04 +01:00
|
|
|
func isFieldRepeated(f *descriptor.FieldDescriptorProto) bool {
|
2018-01-12 10:38:10 -07:00
|
|
|
if f == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-12-26 12:13:04 +01:00
|
|
|
if f.Type != nil && f.Label != nil && *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:38:10 -07:00
|
|
|
func isFieldMap(f *descriptor.FieldDescriptorProto, m *descriptor.DescriptorProto) bool {
|
|
|
|
if f.TypeName == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
shortName := shortType(*f.TypeName)
|
|
|
|
var nt *descriptor.DescriptorProto
|
|
|
|
for _, t := range m.NestedType {
|
|
|
|
if *t.Name == shortName {
|
|
|
|
nt = t
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nt == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range nt.Field {
|
|
|
|
switch *f.Name {
|
|
|
|
case "key":
|
|
|
|
if *f.Number != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case "value":
|
|
|
|
if *f.Number != 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldMapKeyType(f *descriptor.FieldDescriptorProto, m *descriptor.DescriptorProto) *descriptor.FieldDescriptorProto {
|
|
|
|
if f.TypeName == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
shortName := shortType(*f.TypeName)
|
|
|
|
var nt *descriptor.DescriptorProto
|
|
|
|
for _, t := range m.NestedType {
|
|
|
|
if *t.Name == shortName {
|
|
|
|
nt = t
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range nt.Field {
|
|
|
|
if *f.Name == "key" {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldMapValueType(f *descriptor.FieldDescriptorProto, m *descriptor.DescriptorProto) *descriptor.FieldDescriptorProto {
|
|
|
|
if f.TypeName == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
shortName := shortType(*f.TypeName)
|
|
|
|
var nt *descriptor.DescriptorProto
|
|
|
|
for _, t := range m.NestedType {
|
|
|
|
if *t.Name == shortName {
|
|
|
|
nt = t
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range nt.Field {
|
|
|
|
if *f.Name == "value" {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:23:10 +02:00
|
|
|
// goTypeWithGoPackage types the field MESSAGE and ENUM with the go_package name.
|
|
|
|
// This method is an evolution of goTypeWithPackage. It handles message embedded.
|
|
|
|
//
|
|
|
|
// example:
|
|
|
|
// ```proto
|
|
|
|
// message GetArticleResponse {
|
|
|
|
// Article article = 1;
|
|
|
|
// message Storage {
|
|
|
|
// string code = 1;
|
|
|
|
// }
|
|
|
|
// repeated Storage storages = 2;
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// Then the type of `storages` is `GetArticleResponse_Storage` for the go language.
|
|
|
|
//
|
|
|
|
func goTypeWithGoPackage(p *descriptor.FileDescriptorProto, f *descriptor.FieldDescriptorProto) string {
|
|
|
|
pkg := ""
|
|
|
|
if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE || *f.Type == descriptor.FieldDescriptorProto_TYPE_ENUM {
|
|
|
|
if isTimestampPackage(*f.TypeName) {
|
|
|
|
pkg = "timestamp"
|
|
|
|
} else {
|
|
|
|
pkg = *p.GetOptions().GoPackage
|
|
|
|
if strings.Contains(*p.GetOptions().GoPackage, ";") {
|
|
|
|
pkg = strings.Split(*p.GetOptions().GoPackage, ";")[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return goTypeWithEmbedded(pkg, f, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warning does not handle message embedded like goTypeWithGoPackage does.
|
2017-05-01 23:55:55 +02:00
|
|
|
func goTypeWithPackage(f *descriptor.FieldDescriptorProto) string {
|
2017-05-02 12:23:34 +02:00
|
|
|
pkg := ""
|
2017-08-30 14:46:45 +02:00
|
|
|
if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE || *f.Type == descriptor.FieldDescriptorProto_TYPE_ENUM {
|
2018-08-09 10:23:10 +02:00
|
|
|
if isTimestampPackage(*f.TypeName) {
|
|
|
|
pkg = "timestamp"
|
|
|
|
} else {
|
|
|
|
pkg = getPackageTypeName(*f.TypeName)
|
|
|
|
}
|
2017-05-02 12:23:34 +02:00
|
|
|
}
|
2017-05-01 23:55:55 +02:00
|
|
|
return goType(pkg, f)
|
|
|
|
}
|
|
|
|
|
2017-06-22 13:40:09 +02:00
|
|
|
func haskellType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
|
|
|
switch *f.Type {
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Float]"
|
|
|
|
}
|
|
|
|
return "Float"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Float]"
|
|
|
|
}
|
|
|
|
return "Float"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Int64]"
|
|
|
|
}
|
|
|
|
return "Int64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Word]"
|
|
|
|
}
|
|
|
|
return "Word"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Int]"
|
|
|
|
}
|
|
|
|
return "Int"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Word]"
|
|
|
|
}
|
|
|
|
return "Word"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Bool]"
|
|
|
|
}
|
|
|
|
return "Bool"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Text]"
|
|
|
|
}
|
|
|
|
return "Text"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
|
|
|
if pkg != "" {
|
|
|
|
pkg = pkg + "."
|
|
|
|
}
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return fmt.Sprintf("[%s%s]", pkg, shortType(*f.TypeName))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName))
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[Word8]"
|
|
|
|
}
|
|
|
|
return "Word8"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
|
|
|
return fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName))
|
|
|
|
default:
|
|
|
|
return "Generic"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 13:39:24 -06:00
|
|
|
// Warning does not handle message embedded like goTypeWithGoPackage does.
|
|
|
|
func rustTypeWithPackage(f *descriptor.FieldDescriptorProto) string {
|
|
|
|
pkg := ""
|
|
|
|
if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE || *f.Type == descriptor.FieldDescriptorProto_TYPE_ENUM {
|
|
|
|
if isTimestampPackage(*f.TypeName) {
|
|
|
|
pkg = "timestamp"
|
|
|
|
} else {
|
|
|
|
pkg = getPackageTypeName(*f.TypeName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rustType(pkg, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rustType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
|
|
|
isRepeat := *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED
|
|
|
|
typeName := "???"
|
|
|
|
switch *f.Type {
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
|
|
|
typeName = "f64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
|
|
|
typeName = "f32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
|
|
|
typeName = "i64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
|
|
|
typeName = "u64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
|
|
|
typeName = "i32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
|
|
|
typeName = "u32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
|
|
|
typeName = "bool"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
|
|
|
typeName = "String"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
|
|
|
if pkg != "" {
|
|
|
|
pkg = pkg + "."
|
|
|
|
}
|
|
|
|
typeName = fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName))
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
|
|
|
typeName = "Vec<u8>"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
|
|
|
typeName = fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName))
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if isRepeat {
|
|
|
|
return "Vec<" + typeName + ">"
|
|
|
|
}
|
|
|
|
return typeName
|
|
|
|
}
|
|
|
|
|
2020-08-27 10:46:14 -05:00
|
|
|
// Warning does not handle message embedded like goTypeWithGoPackage does.
|
|
|
|
func cppTypeWithPackage(f *descriptor.FieldDescriptorProto) string {
|
|
|
|
pkg := ""
|
|
|
|
if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE || *f.Type == descriptor.FieldDescriptorProto_TYPE_ENUM {
|
|
|
|
if isTimestampPackage(*f.TypeName) {
|
|
|
|
pkg = "timestamp"
|
|
|
|
} else {
|
|
|
|
pkg = getPackageTypeName(*f.TypeName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cppType(pkg, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cppType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
|
|
|
isRepeat := *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED
|
|
|
|
typeName := "???"
|
|
|
|
switch *f.Type {
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
|
|
|
typeName = "double"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
|
|
|
typeName = "float"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
|
|
|
typeName = "int64_t"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
|
|
|
typeName = "uint64_t"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
|
|
|
typeName = "int32_t"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
|
|
|
typeName = "uint32_t"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
|
|
|
typeName = "bool"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
|
|
|
typeName = "std::string"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
|
|
|
if pkg != "" {
|
|
|
|
pkg = pkg + "."
|
|
|
|
}
|
|
|
|
typeName = fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName))
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
|
|
|
typeName = "std::vector<uint8_t>"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
|
|
|
typeName = fmt.Sprintf("%s%s", pkg, shortType(*f.TypeName))
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if isRepeat {
|
|
|
|
return "std::vector<" + typeName + ">"
|
|
|
|
}
|
|
|
|
return typeName
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:23:10 +02:00
|
|
|
func goTypeWithEmbedded(pkg string, f *descriptor.FieldDescriptorProto, p *descriptor.FileDescriptorProto) string {
|
|
|
|
if pkg != "" {
|
|
|
|
pkg = pkg + "."
|
|
|
|
}
|
|
|
|
switch *f.Type {
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]float64"
|
|
|
|
}
|
|
|
|
return "float64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]float32"
|
|
|
|
}
|
|
|
|
return "float32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]int64"
|
|
|
|
}
|
|
|
|
return "int64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]uint64"
|
|
|
|
}
|
|
|
|
return "uint64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]int32"
|
|
|
|
}
|
|
|
|
return "int32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]uint32"
|
|
|
|
}
|
|
|
|
return "uint32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]bool"
|
|
|
|
}
|
|
|
|
return "bool"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]string"
|
|
|
|
}
|
|
|
|
return "string"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
|
|
|
name := *f.TypeName
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
fieldPackage := strings.Split(*f.TypeName, ".")
|
|
|
|
filePackage := strings.Split(*p.Package, ".")
|
|
|
|
// check if we are working with a message embedded.
|
|
|
|
if len(fieldPackage) > 1 && len(fieldPackage)+1 > len(filePackage)+1 {
|
|
|
|
name = strings.Join(fieldPackage[len(filePackage)+1:], "_")
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("[]*%s%s", pkg, shortType(name))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("*%s%s", pkg, shortType(name))
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]byte"
|
|
|
|
}
|
|
|
|
return "byte"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
|
|
|
name := *f.TypeName
|
|
|
|
fieldPackage := strings.Split(*f.TypeName, ".")
|
|
|
|
filePackage := strings.Split(*p.Package, ".")
|
|
|
|
// check if we are working with a message embedded.
|
|
|
|
if len(fieldPackage) > 1 && len(fieldPackage)+1 > len(filePackage)+1 {
|
|
|
|
name = strings.Join(fieldPackage[len(filePackage)+1:], "_")
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("*%s%s", pkg, shortType(name))
|
|
|
|
default:
|
|
|
|
return "interface{}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Deprecated. Instead use goTypeWithEmbedded
|
2016-12-22 22:41:17 +00:00
|
|
|
func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
2017-06-22 13:40:29 +02:00
|
|
|
if pkg != "" {
|
|
|
|
pkg = pkg + "."
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
switch *f.Type {
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]float64"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "float64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]float32"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "float32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]int64"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "int64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]uint64"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "uint64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
2017-06-09 11:53:33 +02:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]int32"
|
|
|
|
}
|
|
|
|
return "int32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]uint32"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "uint32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]bool"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "bool"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]string"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "string"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
2017-04-06 14:40:41 +02:00
|
|
|
return fmt.Sprintf("[]*%s%s", pkg, shortType(*f.TypeName))
|
2016-12-22 22:41:17 +00:00
|
|
|
}
|
2017-04-06 14:40:41 +02:00
|
|
|
return fmt.Sprintf("*%s%s", pkg, shortType(*f.TypeName))
|
2016-12-22 22:41:17 +00:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
2017-02-07 15:09:35 +01:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return "[]byte"
|
|
|
|
}
|
2016-12-22 22:41:17 +00:00
|
|
|
return "byte"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
2017-06-22 13:40:29 +02:00
|
|
|
return fmt.Sprintf("*%s%s", pkg, shortType(*f.TypeName))
|
2016-12-22 22:41:17 +00:00
|
|
|
default:
|
|
|
|
return "interface{}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:38:10 -07:00
|
|
|
func goZeroValue(f *descriptor.FieldDescriptorProto) string {
|
2018-01-12 11:12:15 -07:00
|
|
|
const nilString = "nil"
|
2018-01-12 10:38:10 -07:00
|
|
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
2018-01-12 11:12:15 -07:00
|
|
|
return nilString
|
2018-01-12 10:38:10 -07:00
|
|
|
}
|
|
|
|
switch *f.Type {
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
|
|
|
return "0.0"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
|
|
|
return "0.0"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
|
|
|
return "0"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
|
|
|
return "0"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
|
|
|
return "0"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
|
|
|
return "0"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
|
|
|
return "false"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
|
|
|
return "\"\""
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
2018-01-12 11:12:15 -07:00
|
|
|
return nilString
|
2018-01-12 10:38:10 -07:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
|
|
|
return "0"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
2018-01-12 11:12:15 -07:00
|
|
|
return nilString
|
2018-01-12 10:38:10 -07:00
|
|
|
default:
|
2018-01-12 11:12:15 -07:00
|
|
|
return nilString
|
2018-01-12 10:38:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 00:35:21 +01:00
|
|
|
func jsType(f *descriptor.FieldDescriptorProto) string {
|
2017-01-05 18:04:20 +01:00
|
|
|
template := "%s"
|
2017-12-25 09:17:09 +01:00
|
|
|
if isFieldRepeated(f) {
|
2017-01-05 18:04:20 +01:00
|
|
|
template = "Array<%s>"
|
|
|
|
}
|
|
|
|
|
2017-01-04 00:35:21 +01:00
|
|
|
switch *f.Type {
|
2017-01-10 11:40:48 +01:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_ENUM:
|
2017-01-10 12:24:06 +01:00
|
|
|
return fmt.Sprintf(template, namespacedFlowType(*f.TypeName))
|
2017-01-05 18:04:20 +01:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_FLOAT,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_INT64,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_UINT64,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_INT32,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_FIXED64,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_FIXED32,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_UINT32,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_SFIXED32,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_SINT32,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_SINT64:
|
|
|
|
return fmt.Sprintf(template, "number")
|
2017-01-04 00:35:21 +01:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
2017-01-05 18:04:20 +01:00
|
|
|
return fmt.Sprintf(template, "boolean")
|
2017-01-05 18:32:54 +01:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
2017-01-11 14:52:00 +01:00
|
|
|
return fmt.Sprintf(template, "Uint8Array")
|
2017-01-04 00:35:21 +01:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
2017-01-05 18:04:20 +01:00
|
|
|
return fmt.Sprintf(template, "string")
|
2017-01-04 00:35:21 +01:00
|
|
|
default:
|
2017-01-05 18:04:20 +01:00
|
|
|
return fmt.Sprintf(template, "any")
|
2017-01-04 00:35:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 13:15:40 +02:00
|
|
|
func jsSuffixReservedKeyword(s string) string {
|
|
|
|
return jsReservedRe.ReplaceAllString(s, "${1}${2}_${3}")
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:23:10 +02:00
|
|
|
func isTimestampPackage(s string) bool {
|
|
|
|
var isTimestampPackage bool
|
2017-06-08 16:40:52 +02:00
|
|
|
if strings.Compare(s, ".google.protobuf.Timestamp") == 0 {
|
2018-08-09 10:23:10 +02:00
|
|
|
isTimestampPackage = true
|
2017-06-08 16:40:52 +02:00
|
|
|
}
|
2018-08-09 10:23:10 +02:00
|
|
|
return isTimestampPackage
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPackageTypeName(s string) string {
|
2017-05-01 23:55:55 +02:00
|
|
|
if strings.Contains(s, ".") {
|
|
|
|
return strings.Split(s, ".")[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-12-22 22:41:17 +00:00
|
|
|
func shortType(s string) string {
|
|
|
|
t := strings.Split(s, ".")
|
|
|
|
return t[len(t)-1]
|
|
|
|
}
|
|
|
|
|
2017-01-10 12:24:06 +01:00
|
|
|
func namespacedFlowType(s string) string {
|
|
|
|
trimmed := strings.TrimLeft(s, ".")
|
|
|
|
splitted := strings.Split(trimmed, ".")
|
2017-02-11 17:18:26 +01:00
|
|
|
return strings.Join(splitted, "$")
|
2017-01-10 12:24:06 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 22:41:17 +00:00
|
|
|
func httpPath(m *descriptor.MethodDescriptorProto) string {
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
opts, ok := ext.(*options.HttpRule)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Sprintf("extension is %T; want an HttpRule", ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := opts.Pattern.(type) {
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
case *options.HttpRule_Get:
|
|
|
|
return t.Get
|
|
|
|
case *options.HttpRule_Post:
|
|
|
|
return t.Post
|
|
|
|
case *options.HttpRule_Put:
|
|
|
|
return t.Put
|
|
|
|
case *options.HttpRule_Delete:
|
|
|
|
return t.Delete
|
|
|
|
case *options.HttpRule_Patch:
|
|
|
|
return t.Patch
|
|
|
|
case *options.HttpRule_Custom:
|
|
|
|
return t.Custom.Path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 15:57:07 +01:00
|
|
|
func httpPathsAdditionalBindings(m *descriptor.MethodDescriptorProto) []string {
|
|
|
|
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
opts, ok := ext.(*options.HttpRule)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("extension is %T; want an HttpRule", ext))
|
|
|
|
}
|
|
|
|
|
|
|
|
var httpPaths []string
|
|
|
|
var optsAdditionalBindings = opts.GetAdditionalBindings()
|
|
|
|
for _, optAdditionalBindings := range optsAdditionalBindings {
|
|
|
|
switch t := optAdditionalBindings.Pattern.(type) {
|
|
|
|
case *options.HttpRule_Get:
|
|
|
|
httpPaths = append(httpPaths, t.Get)
|
|
|
|
case *options.HttpRule_Post:
|
|
|
|
httpPaths = append(httpPaths, t.Post)
|
|
|
|
case *options.HttpRule_Put:
|
|
|
|
httpPaths = append(httpPaths, t.Put)
|
|
|
|
case *options.HttpRule_Delete:
|
|
|
|
httpPaths = append(httpPaths, t.Delete)
|
|
|
|
case *options.HttpRule_Patch:
|
|
|
|
httpPaths = append(httpPaths, t.Patch)
|
|
|
|
case *options.HttpRule_Custom:
|
|
|
|
httpPaths = append(httpPaths, t.Custom.Path)
|
|
|
|
default:
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return httpPaths
|
|
|
|
}
|
|
|
|
|
2021-01-08 22:22:43 +03:00
|
|
|
func openapiOption(m *descriptor.MethodDescriptorProto) *openapi_options.Operation {
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(m.Options, openapi_options.E_Openapiv2Operation)
|
|
|
|
if err != nil {
|
2021-01-10 03:33:10 +03:00
|
|
|
return nil
|
2021-01-08 22:22:43 +03:00
|
|
|
}
|
|
|
|
opts, ok := ext.(*openapi_options.Operation)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("extension is %T; want an Openapiv2Operation", ext))
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func httpOption(m *descriptor.MethodDescriptorProto) *HttpOption {
|
|
|
|
|
2021-01-12 13:39:43 +03:00
|
|
|
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-30 19:34:30 +03:00
|
|
|
hr, ok := ext.(*options.HttpRule)
|
2021-01-12 13:39:43 +03:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("extension is %T; want an HttpRule", ext))
|
|
|
|
}
|
|
|
|
|
2021-01-30 19:34:30 +03:00
|
|
|
var method string
|
|
|
|
var path string
|
|
|
|
switch t := hr.Pattern.(type) {
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
case *options.HttpRule_Get:
|
|
|
|
method = "GET"
|
|
|
|
path = t.Get
|
|
|
|
case *options.HttpRule_Post:
|
|
|
|
method = "POST"
|
|
|
|
path = t.Post
|
|
|
|
case *options.HttpRule_Put:
|
|
|
|
method = "PUT"
|
|
|
|
path = t.Put
|
|
|
|
case *options.HttpRule_Delete:
|
|
|
|
method = "DELETE"
|
|
|
|
path = t.Delete
|
|
|
|
case *options.HttpRule_Patch:
|
|
|
|
method = "PATCH"
|
|
|
|
path = t.Patch
|
|
|
|
case *options.HttpRule_Custom:
|
|
|
|
method = t.Custom.Kind
|
|
|
|
path = t.Custom.Path
|
2021-01-08 22:22:43 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 19:34:30 +03:00
|
|
|
opt := &HttpOption{Method: method, Body: hr.Body, Path: path}
|
|
|
|
|
|
|
|
for _, ahr := range hr.AdditionalBindings {
|
|
|
|
switch t := ahr.Pattern.(type) {
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
case *options.HttpRule_Get:
|
|
|
|
method = "GET"
|
|
|
|
path = t.Get
|
|
|
|
case *options.HttpRule_Post:
|
|
|
|
method = "POST"
|
|
|
|
path = t.Post
|
|
|
|
case *options.HttpRule_Put:
|
|
|
|
method = "PUT"
|
|
|
|
path = t.Put
|
|
|
|
case *options.HttpRule_Delete:
|
|
|
|
method = "DELETE"
|
|
|
|
path = t.Delete
|
|
|
|
case *options.HttpRule_Patch:
|
|
|
|
method = "PATCH"
|
|
|
|
path = t.Patch
|
|
|
|
case *options.HttpRule_Custom:
|
|
|
|
method = t.Custom.Kind
|
|
|
|
path = t.Custom.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
aopt := &HttpOption{Method: method, Body: ahr.Body, Path: path}
|
|
|
|
opt.Additional = append(opt.Additional, aopt)
|
2021-01-08 22:22:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
2016-12-22 22:41:17 +00:00
|
|
|
func httpVerb(m *descriptor.MethodDescriptorProto) string {
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
opts, ok := ext.(*options.HttpRule)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Sprintf("extension is %T; want an HttpRule", ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := opts.Pattern.(type) {
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
case *options.HttpRule_Get:
|
|
|
|
return "GET"
|
|
|
|
case *options.HttpRule_Post:
|
|
|
|
return "POST"
|
|
|
|
case *options.HttpRule_Put:
|
|
|
|
return "PUT"
|
|
|
|
case *options.HttpRule_Delete:
|
|
|
|
return "DELETE"
|
|
|
|
case *options.HttpRule_Patch:
|
|
|
|
return "PATCH"
|
|
|
|
case *options.HttpRule_Custom:
|
|
|
|
return t.Custom.Kind
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 14:38:14 +01:00
|
|
|
|
2017-09-19 14:53:57 -06:00
|
|
|
func httpBody(m *descriptor.MethodDescriptorProto) string {
|
|
|
|
|
|
|
|
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
opts, ok := ext.(*options.HttpRule)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Sprintf("extension is %T; want an HttpRule", ext)
|
|
|
|
}
|
|
|
|
return opts.Body
|
|
|
|
}
|
|
|
|
|
2021-01-08 22:22:43 +03:00
|
|
|
func urlVarsFields(path string, d *ggdescriptor.Message) []*descriptor.FieldDescriptorProto {
|
|
|
|
var vars []*descriptor.FieldDescriptorProto
|
|
|
|
for _, field := range d.Field {
|
|
|
|
if !isFieldMessage(field) {
|
|
|
|
if strings.Contains(path, fmt.Sprintf("{%s}", *field.Name)) {
|
|
|
|
vars = append(vars, field)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// JSON name field is checked as fallback. The value set by the protocol compiler.
|
|
|
|
// If the user has set a "json_name" option on a field, that option's value
|
|
|
|
// will be used in this check. By default value in this property will be field name in
|
|
|
|
// camelCase format.
|
|
|
|
if strings.Contains(path, fmt.Sprintf("{%s}", *field.JsonName)) {
|
|
|
|
vars = append(vars, field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vars
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:10:20 +02:00
|
|
|
func urlHasVarsFromMessage(path string, d *ggdescriptor.Message) bool {
|
2017-02-01 14:38:14 +01:00
|
|
|
for _, field := range d.Field {
|
|
|
|
if !isFieldMessage(field) {
|
|
|
|
if strings.Contains(path, fmt.Sprintf("{%s}", *field.Name)) {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-08 22:22:43 +03:00
|
|
|
// JSON name field is checked as fallback. The value set by the protocol compiler.
|
|
|
|
// If the user has set a "json_name" option on a field, that option's value
|
|
|
|
// will be used in this check. By default value in this property will be field name in
|
|
|
|
// camelCase format.
|
|
|
|
if strings.Contains(path, fmt.Sprintf("{%s}", *field.JsonName)) {
|
|
|
|
return true
|
|
|
|
}
|
2017-02-01 14:38:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-11-29 15:46:48 +01:00
|
|
|
|
|
|
|
// lowerGoNormalize takes a string and applies formatting
|
|
|
|
// rules to conform to Golang convention. It applies a camel
|
|
|
|
// case filter, lowers the first character and formats fields
|
|
|
|
// with `id` to `ID`.
|
|
|
|
func lowerGoNormalize(s string) string {
|
|
|
|
fmtd := xstrings.ToCamelCase(s)
|
|
|
|
fmtd = xstrings.FirstRuneToLower(fmtd)
|
|
|
|
return formatID(s, fmtd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// goNormalize takes a string and applies formatting rules
|
|
|
|
// to conform to Golang convention. It applies a camel case
|
|
|
|
// filter and formats fields with `id` to `ID`.
|
|
|
|
func goNormalize(s string) string {
|
|
|
|
fmtd := xstrings.ToCamelCase(s)
|
|
|
|
return formatID(s, fmtd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatID takes a base string alonsgide a formatted string.
|
|
|
|
// It acts as a transformation filter for fields containing
|
|
|
|
// `id` in order to conform to Golang convention.
|
|
|
|
func formatID(base string, formatted string) string {
|
|
|
|
if formatted == "" {
|
|
|
|
return formatted
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case base == "id":
|
|
|
|
// id -> ID
|
|
|
|
return "ID"
|
|
|
|
case strings.HasPrefix(base, "id_"):
|
|
|
|
// id_some -> IDSome
|
|
|
|
return "ID" + formatted[2:]
|
|
|
|
case strings.HasSuffix(base, "_id"):
|
|
|
|
// some_id -> SomeID
|
|
|
|
return formatted[:len(formatted)-2] + "ID"
|
|
|
|
case strings.HasSuffix(base, "_ids"):
|
|
|
|
// some_ids -> SomeIDs
|
|
|
|
return formatted[:len(formatted)-3] + "IDs"
|
|
|
|
}
|
|
|
|
return formatted
|
|
|
|
}
|
2018-01-12 10:38:10 -07:00
|
|
|
|
|
|
|
func replaceDict(src string, dict map[string]interface{}) string {
|
|
|
|
for old, v := range dict {
|
2021-01-08 22:22:43 +03:00
|
|
|
n, ok := v.(string)
|
2018-01-12 10:38:10 -07:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-08 22:22:43 +03:00
|
|
|
src = strings.Replace(src, old, n, -1)
|
2018-01-12 10:38:10 -07:00
|
|
|
}
|
|
|
|
return src
|
|
|
|
}
|
2018-01-17 14:14:39 +01:00
|
|
|
|
|
|
|
func goPkg(f *descriptor.FileDescriptorProto) string {
|
2021-01-12 13:39:43 +03:00
|
|
|
if name := f.Options.GetGoPackage(); name != "" {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return f.GetPackage()
|
2018-01-17 14:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func goPkgLastElement(f *descriptor.FileDescriptorProto) string {
|
|
|
|
pkg := goPkg(f)
|
|
|
|
pkgSplitted := strings.Split(pkg, "/")
|
|
|
|
return pkgSplitted[len(pkgSplitted)-1]
|
|
|
|
}
|