diff --git a/generate.go b/generate.go index 458e6ac..16f2c43 100644 --- a/generate.go +++ b/generate.go @@ -231,23 +231,32 @@ func (pb *Files) makeNewSortfile(pf *File) error { return fmt.Errorf("failed to find struct %s", s.VarType) } - var ucount int - // append() functions + // find() for _, v := range msg.Vars { if v.IsRepeated { continue } - if !v.HasUnique { - continue + if v.HasUnique { + // funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a " + v.VarType + ") *" + s.VarType + "(using" + v.VarName + ")" + // log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) + var FUNCNAME string + if PARENT == VARNAME { + // special case because of the enforced .proto format + FUNCNAME = "Find" + v.VarName + } else { + FUNCNAME = "Find" + VARNAME + "By" + v.VarName + } + + funcdef := msg.generateFindBy(wSort, FUNCNAME, PARENT, VARNAME, s.VarType, v.VarName) + // func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string { + log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) + } else { + if v.VarType == "string" { + // probably dumb. move to a database if you need this + // funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a string) []*" + s.VarType + " ???" + // log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) + } } - ucount += 1 - // funcname := "func (x *" + PARENT + ") Append" + VARNAME + "By" + v.VarName + "(" + v.VarType + ")" - // log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) - // func (msg *MsgName) addAppendBy(w io.Writer, STRUCT, FUNCNAME, VARNAME, CHILD, COLOR string) string { - // func (msg *MsgName) addAppendBy(w io.Writer, STRUCT, FUNCNAME, STRUCTVAR, VARNAME, VARTYPE string) string { - FUNCNAME := "Append" + VARNAME + "By" + v.VarName - funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, v.VarName, s.VarType) - log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) } // delete() functions @@ -264,7 +273,13 @@ func (pb *Files) makeNewSortfile(pf *File) error { APPLES := VARNAME APPLE := v.VarName COLOR := v.VarType - FUNCNAME := "Delete" + VARNAME + "By" + v.VarName + var FUNCNAME string + if PARENT == VARNAME { + // special case because of the enforced .proto format + FUNCNAME = "Delete" + v.VarName + } else { + FUNCNAME = "Delete" + VARNAME + "By" + v.VarName + } var funcdef string if argv.Delete { funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName) @@ -274,36 +289,38 @@ func (pb *Files) makeNewSortfile(pf *File) error { log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) } + var ucount int + // append() functions. these need to be rethought for _, v := range msg.Vars { if v.IsRepeated { continue } - if v.HasUnique { - funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a " + v.VarType + ") *" + s.VarType + "(using" + v.VarName + ")" - log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) - - FUNCNAME := "Find" + VARNAME + "By" + v.VarName - funcdef := msg.generateFindBy(wSort, FUNCNAME, PARENT, VARNAME, s.VarType, v.VarName) - // func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string { - log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) - } else { - if v.VarType == "string" { - funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a string) []*" + s.VarType + " ???" - log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) - } + if !v.HasUnique { + continue } + ucount += 1 + var FUNCNAME string + if PARENT == VARNAME { + // special case because of the enforced .proto format + FUNCNAME = "Append" + v.VarName + } else { + FUNCNAME = "Append" + VARNAME + "By" + v.VarName + } + funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, v.VarName, s.VarType) + log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) } + if ucount == 1 { for _, v := range msg.Vars { if !v.HasUnique { continue } - funcname = "func (x *" + PARENT + ") Insert(a *" + v.VarType + ") (*" + CHILD + ", isNew bool)" + funcname = "func (x *" + PARENT + ") Insert(a *" + v.VarType + ") (*" + CHILD + ", isNew bool) // todo" log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) } } if ucount > 1 { - funcname = "func (x *" + PARENT + ") Insert(a *" + CHILD + ") (*" + CHILD + ", isNew bool)" + funcname = "func (x *" + PARENT + ") Insert(a *" + CHILD + ") (*" + CHILD + ", isNew bool) // todo" log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) } @@ -346,88 +363,6 @@ func (pf *File) specialBases(wFind io.Writer) { } /* - -// to simplify being able to read the code, FRUIT, APPLES and APPLE are used -// FRUIT == the string name of the message in the protobuf file -// APPLE == the type of the repeated variable -// APPLES == the variable name of the repeated struct -func (pf *File) processMessage(parent *MsgName, wSort, wFind io.Writer) error { - var FRUIT string = cases.Title(language.English, cases.NoLower).String(parent.Name) - var LOCK string = parent.Lockname - - log.Printf("Generating functions for %s\n", FRUIT) - - for _, v := range parent.Vars { - if !v.IsRepeated { - // log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) - continue - } - log.Printf("\tFOUND REPEATED %s %s\n", v.VarName, v.VarType) - // use easier to read variable names APPLE and APPLES in the code - var APPLE string = v.VarType - var APPLES string = cases.Title(language.English, cases.NoLower).String(v.VarName) - - // try and find the message struct for APPLE - var found *MsgName - if pf.Base.Name == APPLE { - found = pf.Base - } - for _, m := range pf.MsgNames { - if m.Name == APPLE { - found = m - break - } - } - if found == nil { - return fmt.Errorf("failed to find struct %s", APPLE) - } - - log.Printf("FOUND: %s %s for %s\n", APPLES, APPLE, FRUIT) - - found.addFindByMsg(wFind, FRUIT, APPLES, APPLE, LOCK) - found.addAppendByMsg(wFind, FRUIT, APPLES, APPLE) - found.addDeleteByMsg(wFind, FRUIT, APPLES, APPLE) - found.addInsertByMsg(wFind, FRUIT, APPLES, APPLE) // new idea - - found.addSortByMsg(wSort, FRUIT, APPLES, APPLE) - } - return nil -} -*/ - -/* -func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK string) { - for _, v := range parent.Vars { - if v.HasUnique { - var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName) - log.Printf("\tFIND: (x %s) FindBy%s(string) *%s\n", FRUIT, COLOR, APPLE) - generateFindBy(w, FRUIT, APPLES, APPLE, COLOR, LOCK) - } - } -} -*/ - -/* -func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { - // log.Printf("\tDELETE: %s %s for %s\n", APPLES, APPLE, FRUIT) - var COLORS []string - for _, v := range parent.Vars { - if !v.HasUnique { - continue - } - var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName) - COLORS = append(COLORS, COLOR) - - log.Printf("\tDELETE: (x %s) DeleteBy%s(string) *%s\n", FRUIT, COLOR, APPLE) - if argv.Delete { - parent.deleteByWithCopy(w, FRUIT, APPLES, APPLE, COLOR) - } else { - parent.deleteBy(w, FRUIT, APPLES, APPLE, COLOR) - } - } -} -*/ - func (parent *MsgName) addInsertByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { // log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT) for _, v := range parent.Vars { @@ -468,3 +403,4 @@ func (parent *MsgName) addSortByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { } } } +*/