Skip to content

Commit 9a5d4dd

Browse files
authored
fix(gencli): guard iterator.Done check for LROs with paging fields (#1758)
1 parent 72d3d31 commit 9a5d4dd

3 files changed

Lines changed: 191 additions & 1 deletion

File tree

internal/gencli/cmd_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ var {{$methodCmdVar}} = &cobra.Command{
364364
printMessage(result){{ end }}
365365
{{ end }}
366366
{{ end }}
367-
{{ if .Paged }}
367+
{{ if and .Paged (not .IsLRO) }}
368368
if err == iterator.Done {
369369
return nil
370370
}

internal/gencli/cmd_file_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,41 @@ func TestCommandFile(t *testing.T) {
137137
IsLRO: true,
138138
}
139139

140+
// LRO and Paged
141+
lroPagedCmd := &Command{
142+
Service: "Todo",
143+
Method: "LROPagedTodo",
144+
MethodCmd: "lro-paged-todo",
145+
ShortDesc: "lro paged todo",
146+
LongDesc: "lro paged todo description",
147+
InputMessage: "todopb.LROPagedTodoRequest",
148+
InputMessageVar: "LROPagedTodoInput",
149+
OutputMessageType: ".google.longrunning.Operation",
150+
Imports: map[string]*pbinfo.ImportSpec{
151+
"todopb": &pbinfo.ImportSpec{Name: "todopb", Path: "github.com/googleapis/todo/generated"},
152+
},
153+
Flags: []*Flag{
154+
&Flag{
155+
Name: "page_size",
156+
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32,
157+
FieldName: "PageSize",
158+
VarName: "LROPagedTodoInput",
159+
Usage: "size of page to list",
160+
Optional: true,
161+
},
162+
&Flag{
163+
Name: "page_token",
164+
FieldName: "PageToken",
165+
VarName: "LROPagedTodoInput",
166+
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING,
167+
Usage: "the token from a previous page listed",
168+
Optional: true,
169+
},
170+
},
171+
IsLRO: true,
172+
Paged: true,
173+
}
174+
140175
// client streaming
141176
copyTodosCmd := &Command{
142177
Service: "Todo",
@@ -224,6 +259,14 @@ func TestCommandFile(t *testing.T) {
224259
name: "start-todo",
225260
goldenPath: filepath.Join("testdata", "start-todo.want"),
226261
},
262+
{
263+
g: &gcli{
264+
format: true,
265+
},
266+
cmd: lroPagedCmd,
267+
name: "lro-paged-todo",
268+
goldenPath: filepath.Join("testdata", "lro-paged.want"),
269+
},
227270
{
228271
g: &gcli{
229272
format: true,
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Code generated. DO NOT EDIT.
2+
3+
package main
4+
5+
import (
6+
"github.com/spf13/cobra"
7+
8+
todopb "github.com/googleapis/todo/generated"
9+
)
10+
11+
var LROPagedTodoInput todopb.LROPagedTodoRequest
12+
13+
var LROPagedTodoFromFile string
14+
15+
var LROPagedTodoFollow bool
16+
17+
var LROPagedTodoPollOperation string
18+
19+
var lROPagedTodoInputPageSize int32
20+
21+
var lROPagedTodoInputPageToken string
22+
23+
func init() {
24+
TodoServiceCmd.AddCommand(LROPagedTodoCmd)
25+
26+
LROPagedTodoCmd.Flags().Int32Var(&lROPagedTodoInputPageSize, "page_size", 10, "Default is 10. size of page to list")
27+
28+
LROPagedTodoCmd.Flags().StringVar(&lROPagedTodoInputPageToken, "page_token", "", "the token from a previous page listed")
29+
30+
LROPagedTodoCmd.Flags().StringVar(&LROPagedTodoFromFile, "from_file", "", "Absolute path to JSON file containing request payload")
31+
32+
LROPagedTodoCmd.Flags().BoolVar(&LROPagedTodoFollow, "follow", false, "Block until the long running operation completes")
33+
34+
TodoServiceCmd.AddCommand(LROPagedTodoPollCmd)
35+
36+
LROPagedTodoPollCmd.Flags().BoolVar(&LROPagedTodoFollow, "follow", false, "Block until the long running operation completes")
37+
38+
LROPagedTodoPollCmd.Flags().StringVar(&LROPagedTodoPollOperation, "operation", "", "Required. Operation name to poll for")
39+
40+
LROPagedTodoPollCmd.MarkFlagRequired("operation")
41+
42+
}
43+
44+
var LROPagedTodoCmd = &cobra.Command{
45+
Use: "lro-paged-todo",
46+
Short: "lro paged todo",
47+
Long: "lro paged todo description",
48+
PreRun: func(cmd *cobra.Command, args []string) {
49+
50+
if LROPagedTodoFromFile == "" {
51+
52+
}
53+
54+
},
55+
RunE: func(cmd *cobra.Command, args []string) (err error) {
56+
57+
in := os.Stdin
58+
if LROPagedTodoFromFile != "" {
59+
in, err = os.Open(LROPagedTodoFromFile)
60+
if err != nil {
61+
return err
62+
}
63+
defer in.Close()
64+
65+
err = jsonpb.Unmarshal(in, &LROPagedTodoInput)
66+
if err != nil {
67+
return err
68+
}
69+
70+
}
71+
72+
if Verbose {
73+
printVerboseInput("Todo", "LROPagedTodo", &LROPagedTodoInput)
74+
}
75+
resp, err := TodoClient.LROPagedTodo(ctx, &LROPagedTodoInput)
76+
if err != nil {
77+
return err
78+
}
79+
80+
if !LROPagedTodoFollow {
81+
var s interface{}
82+
s = resp.Name()
83+
84+
if OutputJSON {
85+
d := make(map[string]string)
86+
d["operation"] = resp.Name()
87+
s = d
88+
}
89+
90+
printMessage(s)
91+
return err
92+
}
93+
94+
result, err := resp.Wait(ctx)
95+
if err != nil {
96+
return err
97+
}
98+
99+
if Verbose {
100+
fmt.Print("Output: ")
101+
}
102+
printMessage(result)
103+
104+
return err
105+
},
106+
}
107+
108+
var LROPagedTodoPollCmd = &cobra.Command{
109+
Use: "poll-lro-paged-todo",
110+
Short: "Poll the status of a LROPagedTodoOperation by name",
111+
RunE: func(cmd *cobra.Command, args []string) (err error) {
112+
op := TodoClient.LROPagedTodoOperation(LROPagedTodoPollOperation)
113+
114+
if LROPagedTodoFollow {
115+
resp, err := op.Wait(ctx)
116+
if err != nil {
117+
return err
118+
}
119+
120+
if Verbose {
121+
fmt.Print("Output: ")
122+
}
123+
printMessage(resp)
124+
return err
125+
}
126+
127+
resp, err := op.Poll(ctx)
128+
if err != nil {
129+
return err
130+
} else if resp != nil {
131+
if Verbose {
132+
fmt.Print("Output: ")
133+
}
134+
135+
printMessage(resp)
136+
return
137+
}
138+
139+
if op.Done() {
140+
fmt.Println(fmt.Sprintf("Operation %s is done", op.Name()))
141+
} else {
142+
fmt.Println(fmt.Sprintf("Operation %s not done", op.Name()))
143+
}
144+
145+
return err
146+
},
147+
}

0 commit comments

Comments
 (0)