forked from danielvladco/go-proto-gql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
81 lines (68 loc) · 1.89 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package plugin
import (
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"strings"
"unicode"
"github.com/danielvladco/go-proto-gql/pb"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
)
func getEnum(file *descriptor.FileDescriptorProto, typeName string) *descriptor.EnumDescriptorProto {
for _, enum := range file.GetEnumType() {
if enum.GetName() == typeName {
return enum
}
}
for _, msg := range file.GetMessageType() {
if nes := getNestedEnum(file, msg, strings.TrimPrefix(typeName, msg.GetName()+".")); nes != nil {
return nes
}
}
return nil
}
func getNestedEnum(file *descriptor.FileDescriptorProto, msg *descriptor.DescriptorProto, typeName string) *descriptor.EnumDescriptorProto {
for _, enum := range msg.GetEnumType() {
if enum.GetName() == typeName {
return enum
}
}
for _, nes := range msg.GetNestedType() {
if res := getNestedEnum(file, nes, strings.TrimPrefix(typeName, nes.GetName()+".")); res != nil {
return res
}
}
return nil
}
func resolveRequired(field *descriptor.FieldDescriptorProto) bool {
if v := GetGqlFieldOptions(field); v != nil {
return v.GetRequired()
}
return false
}
func ToLowerFirst(s string) string {
if len(s) > 0 {
return string(unicode.ToLower(rune(s[0]))) + s[1:]
}
return ""
}
func GetGqlFieldOptions(field *descriptor.FieldDescriptorProto) *gql.Field {
if field.Options != nil {
v, err := proto.GetExtension(field.Options, gql.E_Field)
if err == nil && v.(*gql.Field) != nil {
return v.(*gql.Field)
}
}
return nil
}
// Match parsing algorithm from Generator.CommandLineParameters
func Params(gen *generator.Generator) map[string]string {
params := make(map[string]string)
for _, parameter := range strings.Split(gen.Request.GetParameter(), ",") {
kvp := strings.SplitN(parameter, "=", 2)
if len(kvp) != 2 {
continue
}
params[kvp[0]] = kvp[1]
}
return params
}