2016-12-15 15:28:57 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-12-23 01:41:17 +03:00
|
|
|
"fmt"
|
2016-12-16 18:08:37 +03:00
|
|
|
"strings"
|
2016-12-15 15:28:57 +03:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/Masterminds/sprig"
|
2016-12-20 13:31:46 +03:00
|
|
|
"github.com/huandu/xstrings"
|
2016-12-21 12:26:26 +03:00
|
|
|
|
2016-12-23 01:41:17 +03:00
|
|
|
"github.com/golang/protobuf/proto"
|
2016-12-21 12:26:26 +03:00
|
|
|
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
2016-12-23 01:41:17 +03:00
|
|
|
options "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"
|
2016-12-15 15:28:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var ProtoHelpersFuncMap = template.FuncMap{
|
|
|
|
"string": func(i interface {
|
|
|
|
String() string
|
|
|
|
}) string {
|
|
|
|
return i.String()
|
|
|
|
},
|
|
|
|
"json": func(v interface{}) string {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
"prettyjson": func(v interface{}) string {
|
|
|
|
a, _ := json.MarshalIndent(v, "", " ")
|
|
|
|
return string(a)
|
|
|
|
},
|
2016-12-16 18:08:37 +03:00
|
|
|
"splitArray": func(sep string, s string) []string {
|
|
|
|
return strings.Split(s, sep)
|
|
|
|
},
|
|
|
|
"first": func(a []string) string {
|
2016-12-15 15:28:57 +03:00
|
|
|
return a[0]
|
|
|
|
},
|
2016-12-16 18:08:37 +03:00
|
|
|
"last": func(a []string) string {
|
2016-12-15 15:28:57 +03:00
|
|
|
return a[len(a)-1]
|
|
|
|
},
|
2016-12-19 17:43:38 +03:00
|
|
|
"upperFirst": func(s string) string {
|
|
|
|
return strings.ToUpper(s[:1]) + s[1:]
|
|
|
|
},
|
2016-12-16 18:08:37 +03:00
|
|
|
"lowerFirst": func(s string) string {
|
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
|
|
},
|
2016-12-20 13:31:46 +03:00
|
|
|
"camelCase": func(s string) string {
|
2017-01-04 01:41:58 +03:00
|
|
|
if len(s) > 1 {
|
|
|
|
return xstrings.ToCamelCase(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToUpper(s[:1])
|
2016-12-20 13:31:46 +03:00
|
|
|
},
|
2017-01-11 18:33:03 +03:00
|
|
|
"lowerCamelCase": func(s string) string {
|
|
|
|
if len(s) > 1 {
|
|
|
|
s = xstrings.ToCamelCase(s)
|
|
|
|
}
|
2017-01-04 01:41:58 +03:00
|
|
|
|
2017-01-11 18:33:03 +03:00
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
|
|
},
|
2016-12-20 13:31:46 +03:00
|
|
|
"kebabCase": func(s string) string {
|
|
|
|
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1)
|
|
|
|
},
|
2017-01-11 17:39:51 +03:00
|
|
|
"snakeCase": xstrings.ToSnakeCase,
|
2016-12-26 14:13:04 +03:00
|
|
|
"getMessageType": getMessageType,
|
|
|
|
"isFieldMessage": isFieldMessage,
|
|
|
|
"isFieldRepeated": isFieldRepeated,
|
|
|
|
"goType": goType,
|
2017-01-04 02:35:21 +03:00
|
|
|
"jsType": jsType,
|
2016-12-26 14:13:04 +03:00
|
|
|
"httpVerb": httpVerb,
|
|
|
|
"httpPath": httpPath,
|
2017-01-05 20:04:20 +03:00
|
|
|
"shortType": shortType,
|
2016-12-15 15:28:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for k, v := range sprig.TxtFuncMap() {
|
|
|
|
ProtoHelpersFuncMap[k] = v
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 12:26:26 +03:00
|
|
|
|
|
|
|
func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.DescriptorProto {
|
2016-12-27 19:56:44 +03: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 12:26:26 +03:00
|
|
|
for _, m := range f.MessageType {
|
2016-12-27 19:56:44 +03:00
|
|
|
if target == *m.Name {
|
2016-12-21 12:26:26 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFieldMessage(f *descriptor.FieldDescriptorProto) bool {
|
2016-12-29 19:21:53 +03:00
|
|
|
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
2016-12-21 12:26:26 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2016-12-23 01:41:17 +03:00
|
|
|
|
2016-12-26 14:13:04 +03:00
|
|
|
func isFieldRepeated(f *descriptor.FieldDescriptorProto) bool {
|
|
|
|
if f.Type != nil && f.Label != nil && *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:41:17 +03:00
|
|
|
func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
|
|
|
switch *f.Type {
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
|
|
|
return "float64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
|
|
|
return "float32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
|
|
|
return "int64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
|
|
|
return "uint64"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
|
|
|
return "uint32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
|
|
|
return "bool"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
|
|
|
return "string"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
|
|
|
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:
|
|
|
|
return "byte"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
|
|
|
return "uint32"
|
|
|
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
|
|
|
return fmt.Sprintf("*%s.%s", pkg, shortType(*f.TypeName))
|
|
|
|
default:
|
|
|
|
return "interface{}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:35:21 +03:00
|
|
|
func jsType(f *descriptor.FieldDescriptorProto) string {
|
2017-01-05 20:04:20 +03:00
|
|
|
template := "%s"
|
|
|
|
if isFieldRepeated(f) == true {
|
|
|
|
template = "Array<%s>"
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:35:21 +03:00
|
|
|
switch *f.Type {
|
2017-01-10 13:40:48 +03:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE,
|
|
|
|
descriptor.FieldDescriptorProto_TYPE_ENUM:
|
2017-01-10 14:24:06 +03:00
|
|
|
return fmt.Sprintf(template, namespacedFlowType(*f.TypeName))
|
2017-01-05 20:04:20 +03: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 02:35:21 +03:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
2017-01-05 20:04:20 +03:00
|
|
|
return fmt.Sprintf(template, "boolean")
|
2017-01-05 20:32:54 +03:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
2017-01-11 16:52:00 +03:00
|
|
|
return fmt.Sprintf(template, "Uint8Array")
|
2017-01-04 02:35:21 +03:00
|
|
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
2017-01-05 20:04:20 +03:00
|
|
|
return fmt.Sprintf(template, "string")
|
2017-01-04 02:35:21 +03:00
|
|
|
default:
|
2017-01-05 20:04:20 +03:00
|
|
|
return fmt.Sprintf(template, "any")
|
2017-01-04 02:35:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:41:17 +03:00
|
|
|
func shortType(s string) string {
|
|
|
|
t := strings.Split(s, ".")
|
|
|
|
return t[len(t)-1]
|
|
|
|
}
|
|
|
|
|
2017-01-10 14:24:06 +03:00
|
|
|
func namespacedFlowType(s string) string {
|
|
|
|
trimmed := strings.TrimLeft(s, ".")
|
|
|
|
splitted := strings.Split(trimmed, ".")
|
|
|
|
return strings.Join(splitted[1:], "$")
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:41:17 +03: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|