summaryrefslogtreecommitdiff
path: root/protoReformat.go
blob: 87eaf1a63b712311d3273f1064ec06606b162bf5 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"fmt"
	"iter"
	"os"
	"strings"
	sync "sync"

	"go.wit.com/log"
)

// like 'goimport', but for .proto files

var allTheLines *LinesScanner

func protoReformat(filename string) error {
	// read in the .proto file
	data, err := os.ReadFile(filename)
	if err != nil {
		log.Info("file read failed", filename, err)
		return err
	}

	var newfile string
	var fmtmsg *FormatMsg
	fmtmsg = new(FormatMsg)

	var bigName int64
	var bigType int64

	var inMessage bool
	var allLinesIter iter.Seq[string]
	allLinesIter = makeLineIter(data)
	// gets the max vartype and varname
	for line := range allLinesIter {
		if strings.HasPrefix(line, "message ") {
			inMessage = true
			continue
		}

		// find the end of the message
		if strings.HasPrefix(line, "}") {
			inMessage = false
			setMaxSizes(fmtmsg)
			if bigName < fmtmsg.MaxVarname {
				bigName = fmtmsg.MaxVarname
			}
			if bigType < fmtmsg.MaxVartype {
				bigType = fmtmsg.MaxVartype
			}
			fmtmsg = new(FormatMsg)
			continue
		}

		// don't format or change anything when not in a "message {" section
		if !inMessage {
			continue
		}
		fmtmsg.Lines = append(fmtmsg.Lines, line)
	}

	fmtmsg = new(FormatMsg)
	fmtmsg.MaxVarname = bigName
	fmtmsg.MaxVartype = bigType

	// getMessage(fmtmsg)

	// write out the messages
	allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
	for allTheLines.Scan() {
		line := allTheLines.Next()
		if strings.HasPrefix(line, "oneof ") {
			/*
				if inMessage {
					// message inception. search for the architect. don't forget your totem
					newmsg := new(FormatMsg)
					newmsg.MaxVarname = bigName
					newmsg.MaxVartype = bigType
					newmsg.Lines = append(newmsg.Lines, line)
					getInceptionEnum(newmsg)
					fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg)
					continue
				}
			*/
			newmsg := new(FormatMsg)
			newmsg.MaxVarname = bigName
			newmsg.MaxVartype = bigType
			newmsg.Lines = append(newmsg.Lines, line)
			getInceptionEnum(newmsg)
			for _, newline := range formatMessage(newmsg) {
				newfile += fmt.Sprintln(newline)
			}
			continue
		}

		if strings.HasPrefix(line, "enum ") {
			/*
				if inMessage {
					// message inception. search for the architect. don't forget your totem
					newmsg := new(FormatMsg)
					newmsg.MaxVarname = bigName
					newmsg.MaxVartype = bigType
					newmsg.Lines = append(newmsg.Lines, line)
					getInceptionEnum(newmsg)
					fmtmsg.Enums = append(fmtmsg.Enums, newmsg)
					continue
				}
			*/
			newmsg := new(FormatMsg)
			newmsg.MaxVarname = bigName
			newmsg.MaxVartype = bigType
			newmsg.Header = line
			getInceptionEnum(newmsg)
			for _, newline := range formatEnum(newmsg) {
				newfile += fmt.Sprintln(newline)
			}
			continue
		}

		if strings.HasPrefix(line, "message ") {
			newmsg := new(FormatMsg)
			newmsg.MaxVarname = bigName
			newmsg.MaxVartype = bigType
			newmsg.Header = line

			getInceptionMsg(newmsg)
			for _, newline := range formatMessage(newmsg) {
				newfile += fmt.Sprintln(newline)
			}
			/*
				if inMessage {
					// message inception. search for the architect. don't forget your totem
					newmsg := new(FormatMsg)
					newmsg.MaxVarname = bigName
					newmsg.MaxVartype = bigType
					newmsg.Lines = append(newmsg.Lines, line)
					getInceptionMsg(newmsg)
					fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
					continue

				}
				inMessage = true
				parts := strings.Fields(line)
				if len(parts) > 3 {
					// hack to actually indent comments on the message line itself. you're welcome
					start := parts[0] + " " + parts[1] + " " + parts[2]
					end := strings.Join(parts[3:], " ")
					offset := int(bigName) + int(bigType) + 16 - len(start)
					pad := fmt.Sprintf("%d", offset)
					hmm := "%s     %" + pad + "s %s"
					line = fmt.Sprintf(hmm, start, " ", end)
				}
				newfile += fmt.Sprintln(line)
			*/
			continue
		}

		newfile += fmt.Sprintln(line)
	}

	pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Info("file open error. permissions?", filename, err)
		return err
	}
	newfile = strings.TrimSpace(newfile)
	fmt.Fprintln(pf, newfile)
	pf.Close()

	// for i, s := range slices.Backward(pf.ToSort) {
	return nil
}

func getInceptionMsg(fmtmsg *FormatMsg) {
	for allTheLines.Scan() {
		line := allTheLines.Next()
		if strings.HasPrefix(line, "oneof ") {
			// message inception. search for the architect. don't forget your totem
			newmsg := new(FormatMsg)
			newmsg.MaxVarname = fmtmsg.MaxVarname
			newmsg.MaxVartype = fmtmsg.MaxVartype
			newmsg.Header = line
			getInceptionEnum(newmsg)
			fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg)
			continue
		}
		if strings.HasPrefix(line, "enum ") {
			// message inception. search for the architect. don't forget your totem
			newmsg := new(FormatMsg)
			newmsg.MaxVarname = fmtmsg.MaxVarname
			newmsg.MaxVartype = fmtmsg.MaxVartype
			newmsg.Header = line
			fmtmsg.Enums = append(fmtmsg.Enums, newmsg)
			continue
		}
		if strings.HasPrefix(line, "message ") {
			// message inception. search for the architect. don't forget your totem
			newmsg := new(FormatMsg)
			newmsg.MaxVarname = fmtmsg.MaxVarname
			newmsg.MaxVartype = fmtmsg.MaxVartype
			newmsg.Header = line
			fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
			continue
		}
		if strings.HasPrefix(line, "}") {
			fmtmsg.Footer = line
			return
		}
		fmtmsg.Lines = append(fmtmsg.Lines, line)
	}
}

// returns vartype, varname, id, end
func tokenMsgVar(line string) (string, string, string, string) {
	parts := strings.Split(line, ";")
	front := parts[0]
	end := strings.Join(parts[1:], ";")

	var id string
	var varname string
	var vartype string

	parts = strings.Fields(front)
	parts, id = slicesPop(parts)
	parts, _ = slicesPop(parts) // this is the "=" sign
	parts, varname = slicesPop(parts)
	vartype = strings.Join(parts, " ")

	return vartype, varname, id, end
}

func slicesPop(parts []string) ([]string, string) {
	if len(parts) == 0 {
		return nil, ""
	}
	if len(parts) == 1 {
		return nil, parts[0]
	}
	x := len(parts)
	end := parts[x-1]
	return parts[0 : x-1], end
}

// 'for x := range' syntax using the smartly done golang 1.24 'iter'
func makeLineIter(data []byte) iter.Seq[string] {
	items := strings.Split(string(data), "\n")
	// log.Println("Made All() Iter.Seq[] with length", len(items))
	return func(yield func(string) bool) {
		for _, v := range items {
			if !yield(v) {
				return
			}
		}
	}
}

func getInceptionEnum(curmsg *FormatMsg) {
	for allTheLines.Scan() {
		line := allTheLines.Next()
		if strings.HasPrefix(line, "}") {
			curmsg.Lines = append(curmsg.Lines, line)
			return
		}
		curmsg.Lines = append(curmsg.Lines, line)
	}
}

// find the max length of varname and vartype
func setMaxSizes(curmsg *FormatMsg) {
	for _, line := range curmsg.Lines {
		parts := strings.Split(line, ";")
		if len(parts) < 2 {
			// line is blank or just a comment
			continue
		}

		vartype, varname, _, _ := tokenMsgVar(line)
		if len(vartype) > int(curmsg.MaxVartype) {
			curmsg.MaxVartype = int64(len(vartype))
		}
		if len(varname) > int(curmsg.MaxVarname) {
			curmsg.MaxVarname = int64(len(varname))
		}
	}
}

func formatEnum(curmsg *FormatMsg) []string {
	var newmsg []string
	newmsg = append(newmsg, curmsg.Header) // +" //header")

	for _, line := range curmsg.Lines {
		line = strings.TrimSpace(line)
		newmsg = append(newmsg, line)
	}

	newmsg = append(newmsg, curmsg.Footer) // +" //footer")
	return newmsg
}

func formatMessage(curmsg *FormatMsg) []string {
	var newmsg []string
	newmsg = append(newmsg, curmsg.Header) // +" //header")

	// find the max length of varname and vartype
	setMaxSizes(curmsg)
	/*
		for _, line := range curmsg.Lines {
			parts := strings.Split(line, ";")
			if len(parts) < 2 {
				// line is blank or just a comment
				continue
			}

			vartype, varname, _, _ := tokenMsgVar(line)
			if len(vartype) > int(curmsg.MaxVartype) {
				curmsg.MaxVartype = int64(len(vartype))
			}
			if len(varname) > int(curmsg.MaxVarname) {
				curmsg.MaxVarname = int64(len(varname))
			}
		}
	*/

	for _, line := range curmsg.Lines {
		line = strings.TrimSpace(line)
		if line == "" {
			newmsg = append(newmsg, line)
			continue
		}
		if strings.HasPrefix(line, "//") {
			pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21)
			hmm := "%" + pad + "s %s"
			line = fmt.Sprintf(hmm, " ", line) // todo: compute 50
			newmsg = append(newmsg, line)
			continue
		}
		mt := fmt.Sprintf("%d", curmsg.MaxVartype)
		mv := fmt.Sprintf("%d", curmsg.MaxVarname)

		hmm := "        %-" + mt + "s   %-" + mv + "s     = %-3s %s"

		vartype, varname, id, end := tokenMsgVar(line)
		end = strings.TrimSpace(end)
		id = id + ";"

		newline := fmt.Sprintf(hmm, vartype, varname, id, end)
		newline = strings.TrimRight(newline, " ")
		newmsg = append(newmsg, newline)
	}
	newmsg = append(newmsg, curmsg.Footer) // +" //footer")
	return newmsg
}

// DEFINE THE Lines ITERATOR.
// itializes a new iterator.
func newLinesScanner(things []string) *LinesScanner {
	return &LinesScanner{things: things}
}

type LinesScanner struct {
	sync.Mutex

	things []string
	index  int
}

func (it *LinesScanner) Scan() bool {
	if it.index >= len(it.things) {
		return false
	}
	it.Lock()
	it.index++
	it.Unlock()
	return true
}

// Next() returns the next thing in the array
func (it *LinesScanner) Next() string {
	if it.index-1 == len(it.things) {
		fmt.Println("Next() error in LinesScanner", it.index)
	}
	return strings.TrimSpace(it.things[it.index-1])
}

// END DEFINE THE ITERATOR