Repository navigation
Expand file tree
/
Copy pathgen.go
More file actions
98 lines (76 loc) · 1.64 KB
/
Copy pathgen.go
File metadata and controls
98 lines (76 loc) · 1.64 KB
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
package main
import (
"fmt"
"os"
"strings"
"log"
"io/ioutil"
"path/filepath"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/golang/protobuf/proto"
"github.com/springstar/robot/pb"
"github.com/springstar/robot/msg"
)
var typeMap map[string]string = map[string]string{
"TYPE_STRING": "string",
"TYPE_INT32": "int32",
"TYPE_INT": "int",
"TYPE_INT64": "int64",
"TYPE_MESSAGE": "msg",
"TYPE_ENUM": "enum",
}
type Field struct {
typ string
name string
}
type DescriptorGen struct {
mds map[int32]*desc.MessageDescriptor
id2names map[int32]string
}
func newDescriptorGen() *DescriptorGen {
return &DescriptorGen{
mds: make(map[int32]*desc.MessageDescriptor),
id2names: make(map[int32]string),
}
}
func(g *DescriptorGen) parse(path string) {
if _, err := os.Stat("msg/serializer.go"); err != nil {
log.Fatal(err)
}
files, err := ioutil.ReadDir(path)
if (err != nil) {
log.Fatal(err)
}
var pfiles []string
for _, f := range files {
if f.IsDir() {
continue
}
ext := filepath.Ext(f.Name())
if strings.Compare(ext, ".proto") != 0 {
continue
}
p := filepath.Join(path, f.Name())
pfiles = append(pfiles, p)
}
var parser protoparse.Parser
for _, f := range pfiles {
fds, err := parser.ParseFiles(f)
if err != nil {
fmt.Println(err)
return
}
for _, fd := range fds {
mds := fd.GetMessageTypes()
for _, md := range mds {
options := md.GetOptions()
mid, _ := proto.GetExtension(options, pb.E_Msgid)
if (mid != nil) {
msg.AddDescriptor(*mid.(*int32), md)
g.id2names[*mid.(*int32)] = md.GetName()
}
}
}
}
}