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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
// auto run protoc with the correct args
import (
"os"
"strings"
"go.wit.com/lib/fhelp"
"go.wit.com/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// this parses the .proto file and handles anything with `autogenpb: `
func (pf *File) protoParse() error {
// does the file conform to the standard? (also reads in UUID & Version)
// if err := pb.hasPluralMessage(pf); err != nil {
// return err
// }
// os.Exit(0)
uuid, version, err := fhelp.ValidProtobuf(pf.Filename)
if err != nil {
// the user can bypass this, but it's probably not worth allowing this
if os.Getenv("PROTOBUF_REGRET") != "true" {
return err
}
}
pf.Uuid = uuid
pf.Version = version
// read in the .proto file
data, err := os.ReadFile(pf.Filename)
if err != nil {
return err
}
var curmsg *MsgName
// parse the proto file for message struct names
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "message ") {
if curmsg != nil {
// message defined inside another message
}
curmsg = pf.parseForMessage(line)
}
// this logic isn't right. find end of message with more bravado
if strings.HasPrefix(line, "}") {
curmsg = nil
}
if curmsg == nil {
// log.Info("curmsg == nil", line)
// can't contiue on nil below here
// TODO: look for and format enum structs here
continue
}
// log.Info("curmsg != nil", line)
parts := strings.Fields(line)
msgvar := parseMsgVar(line)
if msgvar == nil {
// log.Info("Junk in .proto file? line did not contain a message var:", line)
continue
}
if msgvar.IsRepeated {
// log.Info("ADDING ITER MAP", curmsg.Name, msgvar.VarType)
pf.IterMap[curmsg.Name] = msgvar.VarType
}
if strings.Contains(line, "autogenpb:sort") {
newS := cases.Title(language.English, cases.NoLower).String(parts[1])
// log.Info("Addded Sort:", newS, "in struct", curmsg.Name)
curmsg.Sort = append(curmsg.Sort, newS)
msgvar.HasSort = true
}
if strings.Contains(line, "autogenpb:unique") {
newU := parts[1]
newU = cases.Title(language.English, cases.NoLower).String(newU)
// log.Info("Added Unique:", newU, "in struct", curmsg.Name)
curmsg.Unique = append(curmsg.Unique, newU)
msgvar.HasUnique = true
}
if strings.Contains(line, "`autogenpb:save") {
pf.DoSave = true
}
curmsg.Vars = append(curmsg.Vars, msgvar)
}
pf.makeSortTable()
return nil
}
func (pf *File) makeSortTable() {
pf.sortWhat(pf.Bases)
// log.Info("TO SORT AFTER BASES:", pf.ToSort)
pf.sortWhat(pf.Base)
// everything else
for _, msg := range pf.MsgNames {
pf.sortWhat(msg)
}
}
func (pf *File) sortWhat(msg *MsgName) {
for _, v := range msg.Vars {
if !v.IsRepeated {
continue
}
if check := pf.findMsg(v.VarType); check == nil {
// the VarType must be a struct
continue
}
s := new(Sort)
s.MsgName = msg.Name
s.VarType = v.VarType
s.VarName = v.VarName
s.Lockname = msg.Lockname
pf.ToSort = append(pf.ToSort, s)
}
}
func parseMsgVar(line string) *MsgVar {
if strings.Contains(line, "//") {
parts := strings.Split(line, "//")
if len(parts) == 0 {
// log.Info("parseMsgVar() nothing line", line)
return nil
}
line = parts[0]
}
parts := strings.Fields(line)
if len(parts) < 3 {
// log.Info("parseMsgVar() len < 3", parts)
return nil
}
if parts[0] == "message" {
// this is the struct
return nil
}
v := new(MsgVar)
if parts[0] == "repeated" {
v.IsRepeated = true
v.VarType = parts[1]
v.VarName = cases.Title(language.English, cases.NoLower).String(parts[2])
return v
}
v.VarType = parts[0]
v.VarName = cases.Title(language.English, cases.NoLower).String(parts[1])
return v
}
// looks for mutex and marshal entries
func (pf *File) parseForMessage(line string) *MsgName {
fields := strings.Fields(line)
if len(fields) == 0 || fields[0] != "message" {
// this line doesn't start with message
return nil
}
var msg *MsgName
msg = new(MsgName)
base := cases.Title(language.English, cases.NoLower).String(pf.Filebase)
prefix := "message " + base + "s {" // only look for this for now
prefixEnglishIsWeird := "message " + base + "es {" // only look for this for now
if strings.HasPrefix(line, prefix) {
pf.Bases = msg
} else if strings.HasPrefix(line, prefixEnglishIsWeird) {
pf.Bases = msg
// panic("got here! :" + prefixEnglishIsWeird)
} else {
prefix := "message " + base + " {" // only look for this for now
if strings.HasPrefix(line, prefix) {
pf.Base = msg
} else {
pf.MsgNames = append(pf.MsgNames, msg)
}
}
msgName := cases.Title(language.English, cases.NoLower).String(fields[1])
// log.Info("found messge:", msgName)
msg.Name = msgName
msg.Lockname = pf.Filebase + "Mu" // this should be lowercase. do not export the Mutex
msg.NeedIter = true
// making this the default. it's confirmed pb.Marshal() panics with the existance of a sync var
msg.NoMutex = true
if strings.Contains(line, "`autogenpb:mutex`") {
msg.DoMutex = true
msg.NoMutex = false
// log.Info("Added Mutex=true:", msg.Name)
}
if strings.Contains(line, "`autogenpb:nomutex`") {
// msg.NoMutex = true
// log.Info("Added Mutex=true:", msg.Name)
}
if strings.Contains(line, "`autogenpb:marshal`") {
msg.DoMarshal = true
}
if strings.Contains(line, "`autogenpb:http") {
log.Info("got autogenpb:http")
pf.DoHTTP = true
msg.DoHTTP = true
msg.DoMarshal = true // http requires Marshal
}
if strings.Contains(line, "`autogenpb:gui") {
log.Info("got autogenpb:gui")
pf.DoGui = true
msg.DoGui = true
parts := strings.Split(line, "autogenpb:gu")
if len(parts) != 2 {
log.Info("len(parts) != 2", line)
os.Exit(-1)
}
log.Info("line =", line)
log.Info("end =", parts[1])
end := strings.Split(parts[1], "`")
if end[0] != "i" {
varname := strings.TrimPrefix(end[0], "i:")
msg.GuiVarName = varname
log.Info("varname =", varname)
} else {
if strings.HasSuffix(msgName, "s") {
varname := strings.TrimSuffix(msgName, "s")
msg.GuiVarName = varname
}
}
}
return msg
}
|