-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
143 lines (117 loc) · 2.64 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/kazu/fbshelper/fbsparser"
"github.com/kazu/loncha/structer"
)
var RootName string = "_root"
func main() {
if len(os.Args) < 3 {
fmt.Fprint(os.Stderr, "usage: fbshelper fbsfile templatefile outputdir")
return
}
fbsfile := os.Args[1]
tmplate := os.Args[2]
tmplateunion := filepath.Join(filepath.Dir(os.Args[2]), "union."+
filepath.Base(os.Args[2]))
outDir := os.Args[3]
bytes, err := ioutil.ReadFile(fbsfile)
if err != nil {
fmt.Fprint(os.Stderr, "cannot read file"+fbsfile)
}
parser := &fbsparser.Parser{Buffer: string(bytes)}
parser.Init()
err = parser.Parse()
if err != nil {
fmt.Fprintf(os.Stderr, "parse error %s err=%s", fbsfile, err)
}
parser.Execute()
parser.Fbs.FinilizeForFbs()
RootName = parser.Fbs.RootType
for _, info := range parser.Fbs.Structs {
newSrc, err := FromTemplate(info, tmplate)
if err == nil {
output := filepath.Join(outDir, info.Name+".fbshelper.go")
ioutil.WriteFile(output, []byte(newSrc), 0644)
} else {
fmt.Fprint(os.Stderr, err.Error()+"\n")
}
}
for _, union := range parser.Fbs.Unions {
newSrc, err := FromTemplate(union, tmplateunion)
if err == nil {
output := filepath.Join(outDir, union.Name+".fbshelper.go")
ioutil.WriteFile(output, []byte(newSrc), 0644)
} else {
fmt.Fprint(os.Stderr, err.Error()+"\n")
}
}
}
func FromTemplate(info interface{}, path string) (out string, err error) {
tmpStr := structer.LoadFile(path)
funcMap := template.FuncMap{
"isMessage": IsMessage,
"isSlice": IsSlice,
"toCamel": ToCamelCase,
"isUnion": Search,
"search": Search,
"toBareType": ToBareType,
"isRoot": IsRoot,
}
t := template.Must(template.New("info").Funcs(funcMap).Parse(tmpStr))
s := &strings.Builder{}
err = t.Execute(s, info)
out = s.String()
return
}
func IsMessage(s string) bool {
if s[:2] == `[]` {
if strings.ToUpper(s[2:3]) == s[2:3] {
return true
}
return false
}
if strings.ToUpper(s[:1]) == s[:1] {
return true
}
return false
}
func IsSlice(s string) bool {
//fmt.Printf("IsSlice s=%s result=%v\n", s, s[:2] == `[]`)
if s[:2] == `[]` {
return true
}
return false
}
func IsRoot(s string) bool {
return s == RootName
}
func ToCamelCase(s string) string {
if s[:2] == `[]` {
return fbsparser.ToCamelCase(s[2:])
}
return fbsparser.ToCamelCase(s)
}
func Search(m map[string]bool, s string) bool {
for key, value := range m {
if key == s {
return value
}
}
return false
}
func ToBareType(st string) (s string) {
s = st
if s[:2] == `[]` {
s = s[2:]
}
if s[:1] == `*` {
s = s[1:]
}
return
}