@@ -21,6 +21,7 @@ import (
2121 "io"
2222 "log"
2323 "os"
24+ "path"
2425 "strconv"
2526 "strings"
2627)
@@ -126,15 +127,22 @@ func ParseAndGenerate(filename string, failfast bool) error {
126127 Enums : enums ,
127128 }
128129 // create new file
129- f , err := os .Create (typeLower + "_enums.go" )
130+ // get the p from the filename
131+
132+ p := path .Dir (filename )
133+ // path separator
134+ linuxPathSeparator := "/"
135+ fullPath := p + linuxPathSeparator + typeLower + "_enums.go"
136+ fmt .Println ("Creating file: " , fullPath )
137+ f , err := os .Create (fullPath )
130138 if err != nil {
131139 log .Fatalf ("Error creating file: %v" , err )
132140 }
133141 w := io .StringWriter (f )
134142 defer f .Close ()
135143 writeAll (w , enumRep )
136144 // format the file
137- err = formatFile (typeLower + "_enums.go" )
145+ err = formatFile (fullPath )
138146 if err != nil {
139147 return fmt .Errorf ("failed to format file: %w" , err )
140148 }
@@ -197,7 +205,7 @@ func parseEnums(node *ast.File, typeComments map[string]string) ([]Enum, string,
197205 iotaTypeComment = getTypeComment (valueSpec , typeComments )
198206 comment := getComment (valueSpec )
199207 valid := ! strings .Contains (comment , "invalid" )
200- comment , alternate := getAlternateName (comment , name )
208+ comment , alternate := getAlternateName (comment , name , nameTPairs )
201209 nameTPairsCopy := copyNameTPairs (nameTPairs , getValues (comment ))
202210 enums = append (enums , Enum {
203211 Info : info {
@@ -294,29 +302,34 @@ func copyNameTPairs(nameTPairs []nameTypePair, values []string) []nameTypePair {
294302 return nameTPairsCopy
295303}
296304
297- func getAlternateName (comment string , name * ast.Ident ) (string , string ) {
305+ func getAlternateName (comment string , name * ast.Ident , nameTPairs []nameTypePair ) (string , string ) {
306+ // get value between the first space and the first comma
307+ comment = strings .TrimLeft (comment , " " )
298308 count := strings .Count (comment , " " )
299- alternate := name .Name
300- if count > 0 {
301- nameComm := strings .Split (comment , " " )
302- alternate = nameComm [0 ]
303- comment = nameComm [1 ]
304- }
305- if comment != "" {
306- alternate = comment
309+ switch count {
310+ case 0 :
311+ if comment == "" {
312+ return "" , name .Name
313+ }
314+ if strings .Contains (comment , "," ) {
315+ return comment , name .Name
316+ }
317+ if len (nameTPairs ) == 1 {
318+ return comment , name .Name
319+ }
320+ return comment , comment
321+ case 1 :
322+ split := strings .Split (comment , " " )
323+ return split [1 ], split [0 ]
307324 }
308- comment = strings .TrimSpace (comment )
309- return comment , alternate
325+ return comment , name .Name
310326}
311327
312328func getComment (valueSpec * ast.ValueSpec ) string {
313329 var comment string
314330 if valueSpec .Comment != nil && len (valueSpec .Comment .List ) > 0 {
315- comment = strings .TrimSpace (valueSpec .Comment .List [0 ].Text )
316-
331+ comment = valueSpec .Comment .List [0 ].Text
317332 comment = comment [2 :]
318-
319- comment = strings .TrimSpace (comment )
320333 }
321334 return comment
322335}
@@ -484,12 +497,12 @@ func generateIndexAndNameRun(rep EnumRepresentation) (string, string) {
484497 nameConst := fmt .Sprintf ("_%s_name = %q\n " , rep .TypeInfo .Lower , b .String ())
485498 b .Reset ()
486499 fmt .Fprintf (b , " _%s_index = [...]uint16{0" , rep .TypeInfo .Lower )
487- idx := rep .TypeInfo .Index
500+ // idx := rep.TypeInfo.Index
488501 for _ , i := range indexes {
489502 if i > 0 {
490503 fmt .Fprintf (b , ", " )
491504 }
492- fmt .Fprintf (b , "%d" , idx )
505+ fmt .Fprintf (b , "%d" , i )
493506 }
494507 fmt .Fprintf (b , "}\n " )
495508 return b .String (), nameConst
@@ -554,7 +567,6 @@ func writePackage(w io.StringWriter, rep EnumRepresentation) {
554567func writeImports (w io.StringWriter , rep EnumRepresentation ) {
555568 w .WriteString ("import (\n " )
556569 w .WriteString ("\t \" fmt\" \n " )
557- w .WriteString ("\t \" strings\" \n " )
558570 w .WriteString ("\t \" strconv\" \n " )
559571 w .WriteString ("\t \" bytes\" \n " )
560572 w .WriteString ("\t \" database/sql/driver\" \n " )
@@ -649,10 +661,10 @@ func setupIntToTypeMethod(w io.StringWriter, rep EnumRepresentation) {
649661
650662func setupStringToTypeMethod (w io.StringWriter , rep EnumRepresentation ) {
651663 w .WriteString ("func stringTo" + rep .TypeInfo .Camel + "(s string) " + rep .TypeInfo .Camel + " {\n " )
652- w .WriteString ("\t lwr := strings.ToLower(s)\n " )
653- w .WriteString ("\t switch lwr {\n " )
664+ // w.WriteString("\tlwr := strings.ToLower(s)\n")
665+ w .WriteString ("\t switch s {\n " )
654666 for _ , info := range rep .Enums {
655- w .WriteString ("\t case \" " + info .Info .Lower + "\" :\n " )
667+ w .WriteString ("\t case \" " + info .Info .AlternateName + "\" :\n " )
656668 w .WriteString ("\t \t return " + rep .TypeInfo .PluralCamel + "." + info .Info .Upper + "\n " )
657669 }
658670 w .WriteString ("\t }\n " )
0 commit comments