-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
144 lines (112 loc) · 3.18 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"flag"
"fmt"
"path/filepath"
"strings"
"github.com/golang/glog"
hiveOpts "github.com/simo7/protoc-gen-hive/hive_options"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
var protoToHive = map[string]string{
"bool": "boolean",
"int32": "int",
"int64": "bigint",
"uint32": "int",
"uint64": "bigint",
"float": "float",
"float64": "double",
"double": "double",
"string": "string",
"[]byte": "binary",
"enum": "string",
}
func main() {
var flags flag.FlagSet
protogen.Options{
ParamFunc: flags.Set,
}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if f.Generate {
generateFile(gen, f)
}
}
return nil
})
}
func getIndent(indentLevel int) string {
return strings.Repeat(" ", indentLevel*2)
}
func generateFile(gen *protogen.Plugin, file *protogen.File) {
path, _ := filepath.Split(file.GeneratedFilenamePrefix)
for _, message := range file.Messages {
opts := message.Desc.Options()
if !opts.ProtoReflect().IsValid() {
continue
}
optValue := proto.GetExtension(opts, hiveOpts.E_MessageOpts)
tableName := optValue.(*hiveOpts.MessageOptions).GetTableName()
if tableName == "" {
continue
}
filename := path + tableName + ".json"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("[")
for i, field := range message.Fields {
g.P(" {")
generateField(g, field.Desc, 2)
if i == len(message.Fields)-1 {
g.P(" }")
continue
}
g.P(" },")
}
g.P("]")
}
}
func generateField(g *protogen.GeneratedFile, field protoreflect.FieldDescriptor, indentLevel int) {
fieldName := string(field.Name())
protoKind := field.Kind().String()
fieldType := generateFieldType(field, fieldName, protoKind)
g.P(fmt.Sprintf(`%s"name": "%s",`, getIndent(indentLevel), fieldName))
g.P(fmt.Sprintf(`%s"type": "%s"`, getIndent(indentLevel), fieldType))
}
func generateFieldType(field protoreflect.FieldDescriptor, fieldName string, protoKind string) string {
fieldType := protoToHive[protoKind]
if protoKind != "message" && fieldType == "" {
glog.Fatalf("type %v for field %v was not recognized", protoKind, fieldName)
}
opts := field.Options()
if opts.ProtoReflect().IsValid() {
optValue := proto.GetExtension(opts, hiveOpts.E_FieldOpts)
fieldType = optValue.(*hiveOpts.FieldOptions).GetTypeOverride()
}
if protoKind == "message" {
fds := field.Message().Fields()
if fds.Len() > 0 {
if isProtoTimestamp(fds.Get(0)) {
return "timestamp"
}
}
var messageFields []string
for i := 0; i < fds.Len(); i++ {
field := fds.Get(i)
fieldName := string(field.Name())
protoKind := field.Kind().String()
structField := fmt.Sprintf("%s:%s",
fieldName, generateFieldType(field, fieldName, protoKind),
)
messageFields = append(messageFields, structField)
}
fieldType = fmt.Sprintf("struct<%s>", strings.Join(messageFields, ","))
}
if field.IsList() {
return fmt.Sprintf("array<%s>", fieldType)
}
return fieldType
}
func isProtoTimestamp(fd protoreflect.FieldDescriptor) bool {
return strings.HasPrefix(string(fd.FullName()), "google.protobuf.Timestamp.")
}