mutex lock. duh. actually use it.

This commit is contained in:
Jeff Carr 2025-03-26 04:22:21 -05:00
parent 7a68c6247a
commit 4ab3a465af
3 changed files with 19 additions and 14 deletions

View File

@ -26,7 +26,7 @@ type args struct {
} }
func (a args) Description() string { func (a args) Description() string {
return "autogenpb " + VERSION + " Built on " + BUILDTIME + ` return "go.wit.com/apps/autogenpb " + VERSION + " Built on " + BUILDTIME + `
Auto Generate protocol buffer Sort() and Marshal() functions Auto Generate protocol buffer Sort() and Marshal() functions
@ -38,5 +38,5 @@ See the git sources for an example .proto file.
} }
func (args) Version() string { func (args) Version() string {
return "autogenpb " + VERSION + " Built on " + BUILDTIME return "go.wit.com/apps/autogenpb " + VERSION + " Built on " + BUILDTIME
} }

View File

@ -51,7 +51,7 @@ func newIter(w io.Writer, msg *MsgName) string {
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "type "+APPLE+"Iterator struct {") fmt.Fprintln(w, "type "+APPLE+"Iterator struct {")
fmt.Fprintln(w, " sync.RWMutex // this isn't getting used properly yet?") fmt.Fprintln(w, " sync.Mutex")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " things []*"+APPLE+"") fmt.Fprintln(w, " things []*"+APPLE+"")
fmt.Fprintln(w, " index int") fmt.Fprintln(w, " index int")
@ -61,7 +61,9 @@ func newIter(w io.Writer, msg *MsgName) string {
fmt.Fprintln(w, " if it.index >= len(it.things) {") fmt.Fprintln(w, " if it.index >= len(it.things) {")
fmt.Fprintln(w, " return false") fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }") fmt.Fprintln(w, " }")
fmt.Fprintln(w, " it.Lock()")
fmt.Fprintln(w, " it.index++") fmt.Fprintln(w, " it.index++")
fmt.Fprintln(w, " it.Unlock()")
fmt.Fprintln(w, " return true") fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")

View File

@ -14,8 +14,8 @@ import (
// like 'goimport' but for .proto files // like 'goimport' but for .proto files
var maxVarname int // var maxVarname int
var maxVartype int // var maxVartype int
var linesIter iter.Seq[string] var linesIter iter.Seq[string]
func protoReformat(filename string) error { func protoReformat(filename string) error {
@ -46,7 +46,6 @@ func protoReformat(filename string) error {
// find the end of the message // find the end of the message
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
inMessage = false inMessage = false
// formatMessage(curmsg)
formatMessage2(fmtmsg) formatMessage2(fmtmsg)
if bigName < fmtmsg.MaxVarname { if bigName < fmtmsg.MaxVarname {
bigName = fmtmsg.MaxVarname bigName = fmtmsg.MaxVarname
@ -65,12 +64,11 @@ func protoReformat(filename string) error {
fmtmsg.Lines = append(fmtmsg.Lines, line) fmtmsg.Lines = append(fmtmsg.Lines, line)
} }
maxVarname = int(bigName) fmtmsg = new(FormatMsg)
maxVartype = int(bigType) fmtmsg.MaxVarname = bigName
fmtmsg.MaxVartype = bigType
var curmsg []string // write out the messages
// gets the max vartype and varname
for line := range linesIter { for line := range linesIter {
if strings.HasPrefix(line, "message ") { if strings.HasPrefix(line, "message ") {
if inMessage { if inMessage {
@ -95,11 +93,13 @@ func protoReformat(filename string) error {
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
inMessage = false inMessage = false
// format and write the last message to the file // format and write the last message to the file
for _, newline := range formatMessage(curmsg) { for _, newline := range formatMessage2(fmtmsg) {
newfile += fmt.Sprintln(newline) newfile += fmt.Sprintln(newline)
} }
newfile += fmt.Sprintln(line) newfile += fmt.Sprintln(line)
curmsg = nil fmtmsg = new(FormatMsg)
fmtmsg.MaxVarname = bigName
fmtmsg.MaxVartype = bigType
continue continue
} }
@ -108,7 +108,8 @@ func protoReformat(filename string) error {
newfile += fmt.Sprintln(line) newfile += fmt.Sprintln(line)
continue continue
} }
curmsg = append(curmsg, line)
fmtmsg.Lines = append(fmtmsg.Lines, line)
} }
pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
@ -124,6 +125,7 @@ func protoReformat(filename string) error {
return nil return nil
} }
/*
func formatMessage(curmsg []string) []string { func formatMessage(curmsg []string) []string {
var newmsg []string var newmsg []string
@ -172,6 +174,7 @@ func formatMessage(curmsg []string) []string {
} }
return newmsg return newmsg
} }
*/
// returns vartype, varname, id, end // returns vartype, varname, id, end
func tokenMsgVar(line string) (string, string, string, string) { func tokenMsgVar(line string) (string, string, string, string) {