@@ -678,62 +678,63 @@ func (svc *Service) genStartWorkflowOptions(fn *g.Group, workflow string, child
678678 idFieldName = "WorkflowID"
679679 }
680680
681- // set workflow id if unset and id field and/or prefix defined
682- idFields , idPrefix := opts .GetDefaultOptions ().GetIdFields (), opts .GetDefaultOptions ().GetIdPrefix ()
683- idDelimiter := opts .GetDefaultOptions ().GetIdDelimiter ()
681+ // set workflow id if unset
682+ idFields , idDelimiter := opts .GetDefaultOptions ().GetIdFields (), opts .GetDefaultOptions ().GetIdDelimiter ()
684683 if idDelimiter == "" {
685684 idDelimiter = "/"
686685 }
687- if idPrefix != "" || (hasInput && idFields != "" ) {
688- fn .If (g .Id ("opts" ).Dot (idFieldName ).Op ("==" ).Lit ("" )).BlockFunc (func (b * g.Group ) {
689- // build list of id parameters from options
690- var fieldFormats []string
691- var fields []g.Code
692-
693- // if set, add prefix to id params
694- if idPrefix != "" {
695- fields = append (fields , g .Id (fmt .Sprintf ("%sIDPrefix" , workflow )))
696- fieldFormats = append (fieldFormats , "%s" )
697- }
686+ fn .If (g .Id ("opts" ).Dot (idFieldName ).Op ("==" ).Lit ("" )).BlockFunc (func (b * g.Group ) {
687+ if idFields == "" {
688+ b .Id ("opts" ).Dot (idFieldName ).Op ("=" ).Qual (uuidPkg , "New" ).Call ().Dot ("String" ).Call ()
689+ return
690+ }
698691
699- // if no id_fields provided, default to uuid()
700- if idFields == "" {
701- idFields = "uuid()"
702- }
692+ // build list of id parameters from options
693+ var fieldFormats []string
694+ var fields []g.Code
703695
704- // create index of input message fields
705- fieldsByName := map [string ]* protogen.Field {}
696+ // create index of input message fields
697+ fieldsByName := map [string ]* protogen.Field {}
698+ if hasInput {
706699 for _ , field := range method .Input .Fields {
707700 fieldsByName [string (field .Desc .Name ())] = field
708701 }
702+ }
709703
710- // add id fields
711- for _ , idField := range strings .Split (idFields , "," ) {
712- switch idField {
713- case "uuid()" :
714- fields = append (fields , g .Qual (uuidPkg , "New" ).Call ().Dot ("String" ).Call ())
715- fieldFormats = append (fieldFormats , "%s" )
716- default :
717- field , ok := fieldsByName [idField ]
718- if ! ok {
719- svc .Plugin .Error (fmt .Errorf ("workflow default options references nonexistent id field: %s" , idField ))
720- return
721- }
722- // add field to list
723- fields = append (fields , g .Id ("req" ).Dot (fmt .Sprintf ("Get%s" , field .GoName )).Call ())
724- fieldFormats = append (fieldFormats , "%v" )
704+ // add id fields
705+ for _ , idField := range strings .Split (idFields , "," ) {
706+ switch idField {
707+ case "uuid()" :
708+ fields = append (fields , g .Qual (uuidPkg , "New" ).Call ().Dot ("String" ).Call ())
709+ fieldFormats = append (fieldFormats , "%s" )
710+ default :
711+ field , ok := fieldsByName [idField ]
712+ if ! ok {
713+ svc .Plugin .Error (fmt .Errorf ("workflow default options references nonexistent id field: %s" , idField ))
714+ return
725715 }
716+ // add field to list
717+ fields = append (fields , g .Id ("req" ).Dot (fmt .Sprintf ("Get%s" , field .GoName )).Call ())
718+ fieldFormats = append (fieldFormats , "%v" )
726719 }
720+ }
727721
728- if len (fields ) == 1 {
729- b .Id ("opts" ).Dot (idFieldName ).Op ("=" ).Add (fields [0 ])
730- } else {
731- b .Id ("opts" ).Dot (idFieldName ).Op ("=" ).Qual ("fmt" , "Sprintf" ).Call (
732- g .Lit (strings .Join (fieldFormats , idDelimiter )),
733- g .List (fields ... ),
734- )
735- }
736- })
722+ if len (fields ) == 1 {
723+ b .Id ("opts" ).Dot (idFieldName ).Op ("=" ).Add (fields [0 ])
724+ } else {
725+ b .Id ("opts" ).Dot (idFieldName ).Op ("=" ).Qual ("fmt" , "Sprintf" ).Call (
726+ g .Lit (strings .Join (fieldFormats , idDelimiter )),
727+ g .List (fields ... ),
728+ )
729+ }
730+ })
731+
732+ // add id prefix if specified
733+ idPrefix := opts .GetDefaultOptions ().GetIdPrefix ()
734+ if idPrefix != "" {
735+ fn .If (g .Op ("!" ).Qual ("strings" , "HasPrefix" ).Call (g .Id ("opts" ).Dot (idFieldName ), g .Id (fmt .Sprintf ("%sIDPrefix" , workflow )))).Block (
736+ g .Id ("opts" ).Dot (idFieldName ).Op ("=" ).Qual ("fmt" , "Sprintf" ).Call (g .Lit (fmt .Sprintf ("%%s%s%%s" , idDelimiter )), g .Id (fmt .Sprintf ("%sIDPrefix" , workflow )), g .Id ("opts" ).Dot (idFieldName )),
737+ )
737738 }
738739
739740 // set default id reuse policy
0 commit comments