Skip to content

Commit bcc8a33

Browse files
feat: Add support for custom names and methods that return structs with multiple []*T fields in gen-iterators.go (#4128)
1 parent f293a76 commit bcc8a33

File tree

3 files changed

+354
-5
lines changed

3 files changed

+354
-5
lines changed

github/gen-iterators.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ var useCursorPagination = map[string]bool{
162162
"RepositoriesService.ListHookDeliveries": true,
163163
}
164164

165+
// customNames provides custom names for iterator methods where the default methodName + "Iter" would be confusing.
166+
var customNames = map[string]string{
167+
"RepositoriesService.GetCommit": "ListCommitFiles",
168+
"RepositoriesService.CompareCommits": "ListCommitComparisonFiles",
169+
"RepositoriesService.GetCombinedStatus": "ListCombinedStatus",
170+
}
171+
172+
// sliceToBeUsedForIteration identifies methods where the wrapper struct contains multiple []*T fields,
173+
// and specifies which field should be used for iteration.
174+
var sliceToBeUsedForIteration = map[string]string{
175+
"RepositoriesService.GetCommit": "Files",
176+
"RepositoriesService.CompareCommits": "Files",
177+
}
178+
165179
// customTestJSON maps method names to the JSON response they expect in tests.
166180
// This is needed for methods that internally unmarshal a wrapper struct
167181
// even though they return a slice.
@@ -301,7 +315,8 @@ func (t *templateData) processMethods(f *ast.File) error {
301315
continue
302316
}
303317

304-
if !fd.Name.IsExported() || !strings.HasPrefix(fd.Name.Name, "List") {
318+
methodKey := strings.TrimPrefix(typeToString(fd.Recv.List[0].Type), "*") + "." + fd.Name.Name
319+
if !fd.Name.IsExported() || (!strings.HasPrefix(fd.Name.Name, "List") && customNames[methodKey] == "") {
305320
continue
306321
}
307322

@@ -448,6 +463,13 @@ func (t *templateData) collectMethodInfo(fd *ast.FuncDecl) (*methodInfo, bool) {
448463
}, true
449464
}
450465

466+
func getIterName(methodInfo *methodInfo, methodName string) string {
467+
if customName, ok := customNames[methodInfo.RecvType+"."+methodName]; ok {
468+
return customName + "Iter"
469+
}
470+
return methodName + "Iter"
471+
}
472+
451473
func (t *templateData) processReturnArrayType(fd *ast.FuncDecl, sliceRet *ast.ArrayType, methodInfo *methodInfo) {
452474
testJSON, emptyReturnValue := "[]", "{}"
453475
if val, ok := customTestJSON[fd.Name.Name]; ok {
@@ -467,7 +489,7 @@ func (t *templateData) processReturnArrayType(fd *ast.FuncDecl, sliceRet *ast.Ar
467489
RecvVar: methodInfo.RecvVar,
468490
ClientField: methodInfo.ClientField,
469491
MethodName: fd.Name.Name,
470-
IterMethod: fd.Name.Name + "Iter",
492+
IterMethod: getIterName(methodInfo, fd.Name.Name),
471493
Args: methodInfo.Args,
472494
CallArgs: methodInfo.CallArgs,
473495
TestCallArgs: methodInfo.TestCallArgs,
@@ -496,8 +518,14 @@ func (t *templateData) processReturnStarExpr(fd *ast.FuncDecl, starRet *ast.Star
496518
return
497519
}
498520

499-
itemsField, itemsType, ok := findSinglePointerSliceField(wrapperDef)
500-
if !ok {
521+
var itemsField, itemsType string
522+
if field, ok := sliceToBeUsedForIteration[methodInfo.RecvType+"."+fd.Name.Name]; ok {
523+
itemsField = field
524+
if itemsType, ok = wrapperDef.Fields[itemsField]; !ok || !strings.HasPrefix(itemsType, "[]*") {
525+
logf("Skipping %v.%v: specified items field %v not found or not of type []*T in wrapper %v", methodInfo.RecvTypeRaw, fd.Name.Name, itemsField, wrapperType)
526+
return
527+
}
528+
} else if itemsField, itemsType, ok = findSinglePointerSliceField(wrapperDef); !ok {
501529
logf("Skipping %v.%v: wrapper %v does not contain exactly one []*T field", methodInfo.RecvTypeRaw, fd.Name.Name, wrapperType)
502530
return
503531
}
@@ -525,7 +553,7 @@ func (t *templateData) processReturnStarExpr(fd *ast.FuncDecl, starRet *ast.Star
525553
RecvVar: methodInfo.RecvVar,
526554
ClientField: methodInfo.ClientField,
527555
MethodName: fd.Name.Name,
528-
IterMethod: fd.Name.Name + "Iter",
556+
IterMethod: getIterName(methodInfo, fd.Name.Name),
529557
Args: methodInfo.Args,
530558
CallArgs: methodInfo.CallArgs,
531559
TestCallArgs: methodInfo.TestCallArgs,

github/github-iterators.go

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)