-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto2map.go
151 lines (134 loc) · 3.73 KB
/
proto2map.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
145
146
147
148
149
150
151
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"strings"
"path/filepath"
)
//https://mholt.github.io/json-to-go/
//https://studygolang.com/articles/6112
type Message struct {
Name string `json:"name"`
LongName string `json:"longName"`
FullName string `json:"fullName"`
Desc string `json:"description"`
HasExtensions bool `json:"hasExtensions"`
HasFields bool `json:"hasFields"`
Extensions []interface{} `json:"extensions"`
Code int
IsEnum bool
Fields []struct {
Name string `json:"name"`
Desc string `json:"description"`
Label string `json:"label"`
Type string `json:"type"`
LongType string `json:"longType"`
FullType string `json:"fullType"`
DefaultValue string `json:"defaultValue"`
} `json:"fields"`
}
type Enum struct {
Name string `json:"name"`
LongName string `json:"longName"`
FullName string `json:"fullName"`
Desc string `json:"description"`
IsEnum bool
Values []struct {
Name string `json:"name"`
Number string `json:"number"`
Desc string `json:"description"`
} `json:"values"`
}
type File struct {
Name string `json:"name"`
Desc string `json:"description"`
Package string `json:"package"`
HasEnums bool `json:"hasEnums"`
HasExtensions bool `json:"hasExtensions"`
HasMessages bool `json:"hasMessages"`
HasServices bool `json:"hasServices"`
Enums []*Enum `json:"enums"`
Extensions []interface{} `json:"extensions"`
Messages []*Message `json:"messages"`
Services []interface{} `json:"services"`
}
type AutoGenerated struct {
Files []File `json:"files"`
}
var idDic map[string]int
var idFile *string
var templatePath *string
var outputFile *string
func main() {
templatePath = flag.String("f", "./tpl/MessageIDs.java", " -f your_template_file_name")
outputFile = flag.String("t", "./java/map/MessageIDs.java", " -t your_output_file")
idFile = flag.String("j", "./mapids.js", "-j js_file")
jsonFile := flag.String("data", "./msg.json", " -data your_data_file")
flag.Parse()
var e error
idFileStr, e := ioutil.ReadFile(*idFile)
if e == nil {
err := json.Unmarshal([]byte(idFileStr), &idDic)
ifErr("字典格式不正确:", err)
}else {
ioutil.WriteFile(*idFile,[]byte(`{"max_map_proto_id":0}`),0777) //清空时可用{"max_map_proto_id":0}
}
jsonFileBytes, e := ioutil.ReadFile(*jsonFile)
ifErr("读取数据失败:", e)
var protoMap map[string]interface{}
e = json.Unmarshal(jsonFileBytes, &protoMap)
ifErr("proto集合格式不正确:", e)
var body AutoGenerated
e = json.Unmarshal(jsonFileBytes, &body)
ifErr("解析失败", e)
for _,file:=range body.Files {
doFile(&file)
}
}
func ifErr(str string , err error) {
if err != nil {
fmt.Println(str, err)
os.Exit(1)
}
}
func doFile(file *File) {
//获取之前记录的已分配的最大id号
max, ok := idDic["max_map_proto_id"]
if !ok {
idDic = make(map[string]int)
max = 1
}
for _, v := range file.Messages {
//检查所有协议,凡是发现未定义的,都赋给它新的id号
if _, ok := idDic[v.Name]; !ok {
idDic[v.Name] = max
max = max + 1
}
var id int
id = idDic[v.Name]
v.Code = id
}
//再次记录id
idDic["max_map_proto_id"] = max
dicBytes, err := json.MarshalIndent(idDic, "", " ")
ifErr("出错了",err)
ioutil.WriteFile(*idFile, dicBytes, 0777)
f, err := os.OpenFile(*outputFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0777)
ifErr("创建文件出错",err)
t1, err := template.New(filepath.Base(*templatePath)).Funcs(template.FuncMap{
"hasSuffix": HasSuffix,
"hasPrefix": HasPrefix,
}).ParseFiles(*templatePath)
ifErr("模板执行出错",err)
t1.Execute(f, *file)
}
func HasSuffix(s string,suffix string) bool {
return strings.HasSuffix(s,suffix)
}
func HasPrefix(s string,prefix string) bool {
return strings.HasPrefix(s,prefix)
}