-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (158 loc) · 3.87 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
var (
w *bufio.Writer
)
func main() {
convertFile(strings.TrimSpace(os.Args[1]))
}
func convertFile(fileName string) {
stat, err := os.Stat(fileName)
check(err)
if stat.IsDir() {
filepath.Walk(fileName, func(path string, info os.FileInfo, err error) error {
if path != fileName {
convertFile(path)
}
return nil
})
}
if !strings.HasSuffix(fileName, ".fbs") {
return
}
println("converting " + fileName)
flatFile, err := os.Open(fileName)
check(err)
newFileName := strings.TrimSuffix(fileName, ".fbs") + ".proto"
protoFile, err := os.Create(newFileName)
check(err)
w = bufio.NewWriter(protoFile)
fmt.Fprintf(w, "syntax = \"proto3\";\n")
reader := bufio.NewReader(flatFile)
for {
line, prefix, afterPrefix, err := readNextLine(reader)
if err != nil {
w.Flush()
return
}
if line == "" {
fmt.Fprintln(w)
} else if strings.HasPrefix(line, "//") {
fmt.Fprintln(w, line)
} else if prefix == "namespace" {
fmt.Fprintf(w, "package %s\n", afterPrefix)
} else if prefix == "include" {
fmt.Fprintf(w, "import %s\n", strings.Replace(afterPrefix, ".fbs", ".proto", -1))
} else if prefix == "table" || prefix == "struct" {
fmt.Fprintf(w, "message %s\n", afterPrefix)
handleTableContent(reader, 1)
} else if prefix == "enum" {
fmt.Fprintf(w, "enum %s{\n", strings.Split(afterPrefix, ":")[0])
handleEnumContent(reader, 1)
} else {
fmt.Fprintf(w, "!!! unkown line: %s\n", line)
}
}
}
func handleTableContent(reader *bufio.Reader, depth int) {
fieldID := 1
for {
line, prefix, afterPrefix, err := readNextLine(reader)
check(err)
if line == "}" {
createTabs(depth - 1)
fmt.Fprintln(w, line)
return
} else if strings.HasPrefix(line, "//") || line == "" {
createTabs(depth - 1)
fmt.Fprintln(w, line)
} else if prefix == "table" || prefix == "struct" {
createTabs(depth)
fmt.Fprintf(w, "message %s\n", afterPrefix)
handleTableContent(reader, depth+1)
} else {
createTabs(depth)
split := strings.Split(strings.TrimSuffix(line, ";"), ":")
if len(split) != 2 {
fmt.Fprintf(w, "!!! unkown line: %s\n", line)
continue
}
if strings.HasPrefix(split[1], "[") {
// Arrays
split[1] = "repeated " + getProtoType(split[1][1:len(split[1])-1])
}
optionIndex := strings.Index(split[1], "(")
if optionIndex > 0 {
optionEnd := strings.Index(split[1], ")")
option := strings.TrimSpace(split[1][optionIndex+1 : optionEnd])
if option == "required" {
// required does not exist for proto3
split[1] = strings.TrimSpace(split[1][:optionIndex])
} else {
// throw away the option
split[1] = strings.TrimSpace(split[1][:optionIndex])
}
}
fmt.Fprintf(w, "%s %s = %d;\n", getProtoType(split[1]), split[0], fieldID)
fieldID++
}
}
}
func getProtoType(flatType string) string {
switch flatType {
case "int":
return "int32"
case "short":
return "int32"
case "byte":
return "int32"
case "ubyte":
return "uint32"
}
return flatType
}
func handleEnumContent(reader *bufio.Reader, depth int) {
for {
line, _, _, err := readNextLine(reader)
check(err)
if line == "}" {
createTabs(depth - 1)
fmt.Fprintln(w, line)
return
}
createTabs(depth)
fmt.Fprintln(w, strings.TrimSuffix(line, ",")+";")
}
}
func readNextLine(reader *bufio.Reader) (line string, prefix string, afterPrefix string, err error) {
bytes, _, readErr := reader.ReadLine()
if readErr == io.EOF {
err = readErr
return
}
check(readErr)
line = strings.TrimSpace(string(bytes))
spaceIndex := strings.Index(line, " ")
if spaceIndex > 0 {
afterPrefix = strings.TrimSpace(line[spaceIndex:])
prefix = strings.TrimSpace(line[:spaceIndex])
}
return
}
func createTabs(depth int) {
for i := 0; i < depth; i++ {
fmt.Fprint(w, "\t")
}
}
func check(e error) {
if e != nil {
panic(e)
}
}