maybe a function I can use with patchsets.proto
This commit is contained in:
parent
d36344e463
commit
e676c213b1
71
main.go
71
main.go
|
@ -48,9 +48,34 @@ func main() {
|
|||
os.Exit(-1)
|
||||
}
|
||||
|
||||
f := new(File)
|
||||
pb.Files = append(pb.Files, f)
|
||||
f.Filename = argv.Proto
|
||||
pf := new(File)
|
||||
pb.Files = append(pb.Files, pf)
|
||||
pf.Filename = argv.Proto
|
||||
|
||||
pf.Filebase = strings.TrimSuffix(argv.Proto, ".proto")
|
||||
|
||||
// parse sort & marshal options from the .proto file
|
||||
// this goes through the .proto files and looks
|
||||
// for `autogenpb: ` lines
|
||||
if err := pb.protoParse(pf); err != nil {
|
||||
log.Info("autogenpb parse error:", err)
|
||||
badExit(err)
|
||||
}
|
||||
|
||||
if pf.Bases == nil {
|
||||
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase))
|
||||
}
|
||||
if pf.Base == nil {
|
||||
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase))
|
||||
}
|
||||
|
||||
// show the protobuf of the protobuf. It's like Inception
|
||||
pf.printMsgTable()
|
||||
|
||||
// if you have gotten here, at least the .proto buf file is OK
|
||||
if argv.DryRun {
|
||||
okExit("")
|
||||
}
|
||||
|
||||
// todo, look for go.work files
|
||||
if argv.GoSrc == "" {
|
||||
|
@ -96,59 +121,37 @@ func main() {
|
|||
} else {
|
||||
packageName = argv.Package
|
||||
}
|
||||
f.Package = packageName
|
||||
|
||||
protobase := strings.TrimSuffix(argv.Proto, ".proto")
|
||||
f.Filebase = protobase
|
||||
|
||||
// parse sort & marshal options from the .proto file
|
||||
// this goes through the .proto files and looks
|
||||
// for `autogenpb: ` lines
|
||||
if err := pb.protoParse(f); err != nil {
|
||||
log.Info("autogenpb parse error:", err)
|
||||
badExit(err)
|
||||
}
|
||||
|
||||
if f.Bases == nil {
|
||||
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", f.Filebase))
|
||||
}
|
||||
if f.Base == nil {
|
||||
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", f.Filebase))
|
||||
}
|
||||
|
||||
if argv.DryRun {
|
||||
okExit("")
|
||||
}
|
||||
pf.Package = packageName
|
||||
|
||||
// try to make foo.pb.go with protoc if it's not here
|
||||
// this is helpful because the protoc-gen-go lines
|
||||
// are also annoying to code by hand
|
||||
|
||||
f.Pbfilename = f.Filebase + ".pb.go"
|
||||
pf.Pbfilename = pf.Filebase + ".pb.go"
|
||||
// try to create the foo.pb.go file using protoc if it is not there
|
||||
if !shell.Exists(f.Pbfilename) {
|
||||
if err := pb.protocBuild(f); err != nil {
|
||||
if !shell.Exists(pf.Pbfilename) {
|
||||
if err := pb.protocBuild(pf); err != nil {
|
||||
badExit(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// try to add the Mutex to the pb.go file
|
||||
if err := pb.addMutex(f); err != nil {
|
||||
if err := pb.addMutex(pf); err != nil {
|
||||
badExit(err)
|
||||
}
|
||||
|
||||
// if foo.pb.go still doesn't exist, protoc failed
|
||||
if !shell.Exists(f.Pbfilename) {
|
||||
log.Info("protoc build error.", f.Pbfilename)
|
||||
if !shell.Exists(pf.Pbfilename) {
|
||||
log.Info("protoc build error.", pf.Pbfilename)
|
||||
badExit(errors.New("failed to be created with protoc and proto-gen-go"))
|
||||
}
|
||||
|
||||
// make the marshal.pb.go file
|
||||
pb.marshal(f)
|
||||
pb.marshal(pf)
|
||||
|
||||
// make the sort.pb.go file
|
||||
pb.makeNewSortfile(f)
|
||||
pb.makeNewSortfile(pf)
|
||||
}
|
||||
|
||||
func okExit(s string) {
|
||||
|
|
23
sort.go
23
sort.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"go.wit.com/log"
|
||||
|
@ -43,29 +44,26 @@ func (pb *Files) makeNewSortfile(pf *File) error {
|
|||
}
|
||||
pf.findFunc(wFind)
|
||||
|
||||
// show the protobuf of the protobuf. Inception 2025
|
||||
pf.printMsgTable()
|
||||
|
||||
// attempt to add sort functions for pf.Base
|
||||
pf.processMessage(pf.Base)
|
||||
pf.processMessage(pf.Base, wSort, wFind)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pf *File) processMessage(msg *MsgName) error {
|
||||
func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error {
|
||||
log.Printf("%s\n", msg.Name)
|
||||
for _, v := range msg.Vars {
|
||||
if !v.IsRepeated {
|
||||
// log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType)
|
||||
continue
|
||||
}
|
||||
if err := pf.addSortByMsg(msg, v); err != nil {
|
||||
if err := pf.addSortByMsg(msg, v, wSort, wFind); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar) error {
|
||||
func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writer) error {
|
||||
// log.Printf("\tLOOK HERE: %s %s\n", find.VarName, find.VarType)
|
||||
var found *MsgName
|
||||
for _, msg := range pf.MsgNames {
|
||||
|
@ -88,6 +86,17 @@ func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar) error {
|
|||
// log.Printf("\tUniq!: %s %s for %s\n", find.VarName, find.VarType, v.VarName)
|
||||
newS := cases.Title(language.English, cases.NoLower).String(v.VarName)
|
||||
log.Printf("\t(x %s) AppendUniqueBy%s(%s)\n", parent.Name, newS, find.VarType)
|
||||
var FRUIT string = parent.Name
|
||||
var APPLES string = cases.Title(language.English, cases.NoLower).String(find.VarName)
|
||||
var APPLE string = find.VarType
|
||||
var COLOR string = newS
|
||||
var FruitLock string
|
||||
if argv.Mutex {
|
||||
FruitLock = "x.Lock" // uses mutex frum protoc.pb.go file
|
||||
} else {
|
||||
FruitLock = FRUIT + ".Lock" // ugly global lock hack. should probably deprecate
|
||||
}
|
||||
appendUniqueBy(wFind, FRUIT, APPLES, APPLE, COLOR, FruitLock)
|
||||
log.Printf("\t(x %s) FindBy%s(string) *%s\n", parent.Name, newS, find.VarType)
|
||||
if v.VarType == "string" {
|
||||
log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, find.VarType)
|
||||
|
|
19
sortFunc.go
19
sortFunc.go
|
@ -193,6 +193,25 @@ func (pf *File) appendUnique(w io.Writer) {
|
|||
fmt.Fprintln(w, "")
|
||||
}
|
||||
|
||||
func appendUniqueBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FruitLock string) {
|
||||
fmt.Fprintln(w, "// TESTING")
|
||||
fmt.Fprintln(w, "// enforces "+APPLE+"."+COLOR+" is unique in "+FRUIT+"."+APPLES)
|
||||
fmt.Fprintln(w, "func (x *"+FRUIT+") AppendUniqueBy"+COLOR+"(newP *"+APPLE+") bool {")
|
||||
fmt.Fprintln(w, " "+FruitLock+".Lock()")
|
||||
fmt.Fprintln(w, " defer "+FruitLock+".Unlock()")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, " for _, p := range x."+APPLES+" {")
|
||||
fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {")
|
||||
fmt.Fprintln(w, " return false")
|
||||
fmt.Fprintln(w, " }")
|
||||
fmt.Fprintln(w, " }")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)")
|
||||
fmt.Fprintln(w, " return true")
|
||||
fmt.Fprintln(w, "}")
|
||||
fmt.Fprintln(w, "")
|
||||
}
|
||||
|
||||
func (pf *File) replaceFunc(w io.Writer) {
|
||||
var MSG string = pf.Bases.Name
|
||||
var BASE string = pf.Base.Name
|
||||
|
|
Loading…
Reference in New Issue