forked from davyxu/cellnet
/
main.go
73 lines (57 loc) · 1.47 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
package main
import (
"io/ioutil"
"os"
"path"
"github.com/davyxu/golog"
"github.com/davyxu/pbmeta"
pbprotos "github.com/davyxu/pbmeta/proto"
plugin "github.com/davyxu/pbmeta/proto/compiler"
"github.com/golang/protobuf/proto"
)
var log *golog.Logger = golog.New("main")
func main() {
// 读取protoc请求
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Errorln("reading input")
}
var Request plugin.CodeGeneratorRequest // The input.
var Response plugin.CodeGeneratorResponse // The output.
// 解析请求
if err := proto.Unmarshal(data, &Request); err != nil {
log.Errorln("parsing input proto")
}
if len(Request.FileToGenerate) == 0 {
log.Errorln("no files to generate")
}
// 建立解析池
pool := pbmeta.NewDescriptorPool(&pbprotos.FileDescriptorSet{
File: Request.ProtoFile,
})
Response.File = make([]*plugin.CodeGeneratorResponse_File, 0)
contenxt, ok := printFile(pool)
if !ok {
os.Exit(1)
}
Response.File = append(Response.File, &plugin.CodeGeneratorResponse_File{
Name: proto.String(Request.GetParameter()),
Content: proto.String(contenxt),
})
// 发回处理结果
data, err = proto.Marshal(&Response)
if err != nil {
log.Errorln("failed to marshal output proto")
}
_, err = os.Stdout.Write(data)
if err != nil {
log.Errorln("failed to write output proto")
}
}
func changeExt(name string) string {
ext := path.Ext(name)
if ext == ".proto" {
name = name[0 : len(name)-len(ext)]
}
return name + ".msg.go"
}