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

// auto run protoc with the correct args

import (
	"bufio"
	"fmt"
	"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: `

// does the fruit.proto file have "message Fruits"
func (pb *Files) hasPluralMessage(f *File) error {
	file, err := os.Open(f.Filename)
	if err != nil {
		return err
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()

		base := cases.Title(language.English, cases.NoLower).String(f.Filebase)
		prefix := "message " + base + "s" // to conform, it must have an added 's'
		if !strings.HasPrefix(line, prefix) {
			// log.Info("nope", prefix, "line", line)
			// nope, not this line
			continue
		}
		// found the matching message
		// f.Bases = f.parseForMessage(line)

		line = scanner.Text()
		fields := strings.Fields(line)
		if fields[0] == "string" && fields[1] != "uuid" {
			return fmt.Errorf("proto file does not have a UUID")
		}
		// ok, uuid is here
		f.Uuid = line
		log.Info("found UUID:", line)

		line = scanner.Text()
		fields = strings.Fields(line)
		if fields[0] == "string" && fields[1] != "version" {
			return fmt.Errorf("proto file does not have a version")
		}
		// found "version", the .proto file conforms
		f.Version = line
		log.Info("found Version:", line)
		return nil
	}
	return fmt.Errorf("proto file error %s", f.Filename)
}

func (pb *Files) protoParse(f *File) error {
	// does the file conform to the standard? (also reads in UUID & Version)
	if err := pb.hasPluralMessage(f); err != nil {
		return err
	}
	log.Info(f.Filename, "is valid so far")

	// read in the .proto file
	data, err := os.ReadFile(f.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") {
		base := cases.Title(language.English, cases.NoLower).String(f.Filebase)
		if strings.HasPrefix(line, "message ") {
			curmsg = f.parseForMessage(line)
			prefix := "message " + base // only look for this for now
			if strings.HasPrefix(line, prefix) {
				// f.Base = curmsg
			} else {
				f.MsgNames = append(f.MsgNames, curmsg)
			}
		}
		if strings.HasPrefix(line, "}") {
			curmsg = nil
		}
		if curmsg == nil {
			// can't contiue on nil below here
			continue
		}

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

		if strings.Contains(line, "autogenpb:sort") {
			newS := parts[1]
			log.Info("Addded Sort:", newS, "in struct", curmsg.Name)
			curmsg.Sort = append(curmsg.Sort, newS)
		}

		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)
		}
	}
	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]
	log.Info("found messge:", msgName)
	msg := new(MsgName)
	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
}