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"
|
2017-04-13 13:15:40 +02:00
|
|
|
"regexp"
|
2016-12-16 16:08:37 +01:00
|
|
|
"strings"
|
2016-12-15 13:28:57 +01:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/Masterminds/sprig"
|
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"
|
|
|
|
"github.com/huandu/xstrings"
|
2017-03-31 18:01:58 +02:00
|
|
|
options "google.golang.org/genproto/googleapis/api/annotations"
|
2016-12-15 13:28:57 +01:00
|
|
|
)
|
|
|
|
|
2017-04-13 13:15:40 +02:00
|
|
|
var jsReservedRe *regexp.Regexp = 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-05-19 10:02:40 +02:00
|
|
|
var (
|
|
|
|
registry *ggdescriptor.Registry // some helpers need access to registry
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetRegistry(reg *ggdescriptor.Registry) {
|
|
|
|
registry = reg
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return string(a)
|
|
|
|
},
|
|
|
|
"prettyjson": func(v interface{}) string {
|
|
|
|
a, _ := json.MarshalIndent(v, "", " ")
|
|
|
|
return string(a)
|
|
|
|
},
|
2016-12-16 16:08:37 +01:00
|
|
|
"splitArray": func(sep string, s string) []string {
|
2017-06-22 13:35:19 +02:00
|
|
|
var r []string
|
|
|
|
t := strings.Split(s, sep)
|
|
|
|
for i := range t {
|
|
|
|
if t[i] != "" {
|
|
|
|
r = append(r, t[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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]
|
|
|
|
},
|
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:]
|
|
|
|
},
|
2016-12-20 11:31:46 +01:00
|
|
|
"kebabCase": func(s string) string {
|
|
|
|
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1)
|
|
|
|
},
|
2017-02-01 14:38:14 +01:00
|
|
|
"snakeCase": xstrings.ToSnakeCase,
|
2017-05-18 19:44:05 +02:00
|
|
|
"getProtoFile": getProtoFile,
|
2017-02-01 14:38:14 +01:00
|
|
|
"getMessageType": getMessageType,
|
2017-05-12 20:46:18 +02:00
|
|
|
"getEnumValue": getEnumValue,
|
2017-02-01 14:38:14 +01:00
|
|
|
"isFieldMessage": isFieldMessage,
|
|
|
|
"isFieldRepeated": isFieldRepeated,
|
2017-06-22 13:40:09 +02:00
|
|
|
"haskellType": haskellType,
|
2017-02-01 14:38:14 +01:00
|
|
|
"goType": goType,
|
2017-05-01 23:55:55 +02:00
|
|
|
"goTypeWithPackage": goTypeWithPackage,
|
2017-02-01 14:38:14 +01:00
|
|
|
"jsType": jsType,
|
2017-04-13 13:15:40 +02:00
|
|
|
"jsSuffixReserved": jsSuffixReservedKeyword,
|
2017-02-01 14:38:14 +01:00
|
|
|
"namespacedFlowType": namespacedFlowType,
|
|
|
|
"httpVerb": httpVerb,
|
|
|
|
"httpPath": httpPath,
|
|
|
|
"shortType": shortType,
|
|
|
|
"urlHasVarsFromMessage": urlHasVarsFromMessage,
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if f.Type != nil && f.Label != nil && *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:55:55 +02:00
|
|
|
func goTypeWithPackage(f *descriptor.FieldDescriptorProto) string {
|
2017-05-02 12:23:34 +02:00
|
|
|
pkg := ""
|
|
|
|
if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
|
|
|
pkg = getPackageTypeName(*f.TypeName)
|
|
|
|
}
|
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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 00:35:21 +01:00
|
|
|
func jsType(f *descriptor.FieldDescriptorProto) string {
|
2017-01-05 18:04:20 +01:00
|
|
|
template := "%s"
|
|
|
|
if isFieldRepeated(f) == true {
|
|
|
|
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}")
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:55:55 +02:00
|
|
|
func getPackageTypeName(s string) string {
|
2017-06-08 16:40:52 +02:00
|
|
|
if strings.Compare(s, ".google.protobuf.Timestamp") == 0 {
|
|
|
|
return "timestamp"
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|