Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions github/gen-iterators.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type method struct {
UseListCursorOptions bool
UseListOptions bool
UsePage bool
UseAfter bool
TestJSON1 string
TestJSON2 string
TestJSON3 string
Expand Down Expand Up @@ -206,6 +207,22 @@ func (t *templateData) hasIntPage(structName string) bool {
return false
}

func (t *templateData) hasStringAfter(structName string) bool {
sd, ok := t.Structs[structName]
if !ok {
return false
}
if typeStr, ok := sd.Fields["After"]; ok {
return typeStr == "string"
}
for _, embed := range sd.Embeds {
if t.hasStringAfter(embed) {
return true
}
}
return false
}

func getZeroValue(typeStr string) string {
switch typeStr {
case "int", "int64", "int32":
Expand Down Expand Up @@ -303,9 +320,10 @@ func (t *templateData) processMethods(f *ast.File) error {
useListCursorOptions := t.hasListCursorOptions(optsType)
useListOptions := t.hasListOptions(optsType)
usePage := t.hasIntPage(optsType)
useAfter := t.hasStringAfter(optsType)

if !useListCursorOptions && !useListOptions && !usePage {
logf("Skipping %s.%s: opts %s does not have ListCursorOptions, ListOptions, or Page int", recvType, fd.Name.Name, optsType)
if !useListCursorOptions && !useListOptions && !usePage && !useAfter {
logf("Skipping %s.%s: opts %s does not have ListCursorOptions, ListOptions, Page int, or After string", recvType, fd.Name.Name, optsType)
continue
}

Expand Down Expand Up @@ -346,6 +364,7 @@ func (t *templateData) processMethods(f *ast.File) error {
UseListCursorOptions: useListCursorOptions,
UseListOptions: useListOptions,
UsePage: usePage,
UseAfter: useAfter,
TestJSON1: testJSON1,
TestJSON2: testJSON2,
TestJSON3: testJSON3,
Expand Down Expand Up @@ -463,11 +482,16 @@ func ({{.RecvVar}} *{{.RecvType}}) {{.IterMethod}}({{.Args}}) iter.Seq2[{{.Retur
break
}
{{.OptsName}}.ListOptions.Page = resp.NextPage
{{else}}
{{else if .UsePage}}
if resp.NextPage == 0 {
break
}
{{.OptsName}}.Page = resp.NextPage
{{else if .UseAfter}}
if resp.After == "" {
break
}
{{.OptsName}}.After = resp.After
{{end -}}
}
}
Expand All @@ -493,7 +517,7 @@ func Test{{.RecvType}}_{{.IterMethod}}(t *testing.T) {
callNum++
switch callNum {
case 1:
{{- if .UseListCursorOptions}}
{{- if or .UseListCursorOptions .UseAfter}}
w.Header().Set("Link", ` + "`" + `<https://api.github.com/?after=yo>; rel="next"` + "`" + `)
{{else}}
w.Header().Set("Link", ` + "`" + `<https://api.github.com/?page=1>; rel="next"` + "`" + `)
Expand Down
Loading
Loading