Skip to content

Commit 5d150c0

Browse files
add custom name and slice to be used for iteration support to gen-iterators.go
1 parent f293a76 commit 5d150c0

3 files changed

Lines changed: 356 additions & 7 deletions

File tree

github/gen-iterators.go

Lines changed: 35 additions & 7 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,9 @@ 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+
320+
if !fd.Name.IsExported() || (!strings.HasPrefix(fd.Name.Name, "List") && customNames[methodKey] == "") {
305321
continue
306322
}
307323

@@ -448,6 +464,13 @@ func (t *templateData) collectMethodInfo(fd *ast.FuncDecl) (*methodInfo, bool) {
448464
}, true
449465
}
450466

467+
func getIterName(methodInfo *methodInfo, methodName string) string {
468+
if customName, ok := customNames[methodInfo.RecvType+"."+methodName]; ok {
469+
return customName + "Iter"
470+
}
471+
return methodName + "Iter"
472+
}
473+
451474
func (t *templateData) processReturnArrayType(fd *ast.FuncDecl, sliceRet *ast.ArrayType, methodInfo *methodInfo) {
452475
testJSON, emptyReturnValue := "[]", "{}"
453476
if val, ok := customTestJSON[fd.Name.Name]; ok {
@@ -467,7 +490,7 @@ func (t *templateData) processReturnArrayType(fd *ast.FuncDecl, sliceRet *ast.Ar
467490
RecvVar: methodInfo.RecvVar,
468491
ClientField: methodInfo.ClientField,
469492
MethodName: fd.Name.Name,
470-
IterMethod: fd.Name.Name + "Iter",
493+
IterMethod: getIterName(methodInfo, fd.Name.Name),
471494
Args: methodInfo.Args,
472495
CallArgs: methodInfo.CallArgs,
473496
TestCallArgs: methodInfo.TestCallArgs,
@@ -496,7 +519,7 @@ func (t *templateData) processReturnStarExpr(fd *ast.FuncDecl, starRet *ast.Star
496519
return
497520
}
498521

499-
itemsField, itemsType, ok := findSinglePointerSliceField(wrapperDef)
522+
itemsField, itemsType, ok := findSinglePointerSliceField(wrapperDef, methodInfo.RecvType+"."+fd.Name.Name)
500523
if !ok {
501524
logf("Skipping %v.%v: wrapper %v does not contain exactly one []*T field", methodInfo.RecvTypeRaw, fd.Name.Name, wrapperType)
502525
return
@@ -525,7 +548,7 @@ func (t *templateData) processReturnStarExpr(fd *ast.FuncDecl, starRet *ast.Star
525548
RecvVar: methodInfo.RecvVar,
526549
ClientField: methodInfo.ClientField,
527550
MethodName: fd.Name.Name,
528-
IterMethod: fd.Name.Name + "Iter",
551+
IterMethod: getIterName(methodInfo, fd.Name.Name),
529552
Args: methodInfo.Args,
530553
CallArgs: methodInfo.CallArgs,
531554
TestCallArgs: methodInfo.TestCallArgs,
@@ -547,17 +570,22 @@ func (t *templateData) processReturnStarExpr(fd *ast.FuncDecl, starRet *ast.Star
547570
t.Methods = append(t.Methods, m)
548571
}
549572

550-
func findSinglePointerSliceField(sd *structDef) (fieldName, fieldType string, ok bool) {
573+
func findSinglePointerSliceField(sd *structDef, methodKey string) (fieldName, fieldType string, ok bool) {
551574
matches := []string{}
552575
for name, typeStr := range sd.Fields {
553576
if strings.HasPrefix(typeStr, "[]*") {
554577
matches = append(matches, name)
555578
}
556579
}
557-
if len(matches) != 1 {
580+
if len(matches) != 1 && sliceToBeUsedForIteration[methodKey] == "" {
558581
return "", "", false
559582
}
560-
fieldName = matches[0]
583+
584+
if custom, ok := sliceToBeUsedForIteration[methodKey]; ok {
585+
fieldName = custom
586+
} else {
587+
fieldName = matches[0]
588+
}
561589
return fieldName, sd.Fields[fieldName], true
562590
}
563591

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)