summaryrefslogtreecommitdiff
path: root/protoParse.go
blob: 8c36e570c1e2393cf8f79379a2a8a008e1d4791e (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
package main

// auto run protoc with the correct args

import (
	"os"
	"strings"

	"go.wit.com/log"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

// this parses the .proto file and handles anything with `autogenpb: `

// finds autogenpb:marshal and autogenpb:unique in the .proto file
//
//	adds fields to []marshal and []unique
func (pb *Files) protoParseNew(f *File) error {
	// log.Info("starting findAutogenpb() on", names["protofile"])
	// read in the .proto file
	data, err := os.ReadFile(f.Filename)
	if err != nil {
		// log.Info("open config file :", err)
		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 ") {
			curmsg = f.parseForMessage(line)
		}
		if strings.HasPrefix(line, "}") {
			curmsg = nil
		}

		// log.Info("line:", line)
		parts := strings.Fields(line)

		if strings.Contains(line, "autogenpb:sort") {
			if parts[0] == "repeated" {
				newS := parts[1]
				if curmsg == nil {
					log.Info("Error: Found Sort for:", newS, "however, this struct can't be used")
				} else {
					log.Info("Addded Sort:", newS, "in struct", curmsg.Name)
					curmsg.Sort = append(curmsg.Sort, newS)
				}
			} else {
				log.Info("Error:", line)
				log.Info("Error: can not sort on non repeated fields")
			}
		}

		if strings.Contains(line, "autogenpb:unique") {
			if parts[0] == "repeated" {
				newU := parts[1]
				newU = cases.Title(language.English, cases.NoLower).String(newU)
				if curmsg == nil {
					log.Info("Error: Found Unique for:", newU, "however, this struct can't be used")
				} else {
					log.Info("Added Unique:", newU, "in struct", curmsg.Name)
					curmsg.Unique = append(curmsg.Unique, newU)
				}
			} else {
				log.Info("Error:", line)
				log.Info("Error: can not append on non repeated fields")
			}
		}
	}
	return nil
}

// looks for mutex and marshal entries
func (f *File) parseForMessage(line string) *MsgName {
	fields := strings.Fields(line)
	if fields[0] != "message" {
		return nil
	}
	msgName := fields[1]
	msg := new(MsgName)
	f.MsgNames = append(f.MsgNames, msg)
	msg.Name = msgName
	msg.Lockname = msgName + "Mu"

	if strings.Contains(line, "`autogenpb:mutex`") {
		msg.DoMutex = true
		log.Info("Added Mutex=true:", msg.Name)
	}
	if strings.Contains(line, "`autogenpb:marshal`") {
		msg.DoMarshal = true
		log.Info("Added Marshal=true:", msg.Name)
	}
	return msg
}

// this doesn't do anything anymore (?)
func (pb *Files) protoParse(f *File) error {
	// log.Info("starting findAutogenpb() on", filename)
	// read in the .proto file
	data, err := os.ReadFile(f.Filename)
	if err != nil {
		// log.Info("open config file :", err)
		return err
	}

	lines := strings.Split(string(data), "\n")
	for _, line := range lines {
		if strings.Contains(line, "autogenpb:ignoreproto") {
			// ignore this protofile completely (don't make foo.pb.go)
			os.Exit(0)
		}
		if strings.Contains(line, "autogenpb:no-marshal") {
			// don't marshal anything (don't make foo.marshal.pb.go)
			argv.NoMarshal = true
		}
		if strings.Contains(line, "autogenpb:no-sort") {
			// don't sort anything (don't make foo.sort.pb.go)
			argv.NoSort = true
		}
		if strings.Contains(line, "autogenpb:mutex") {
			// try the mutex hack
			argv.Mutex = true
		}
		if strings.Contains(line, "autogenpb:gover:") {
			// todo: parse the output here
			parts := strings.Split(line, "autogenpb:gover:")
			log.Info("found gover:", parts[1])
			argv.Mutex = true
		}
	}
	return nil
}