Skip to content

Commit a3beed4

Browse files
authored
test(sidekick): unit tests for pagination info (#2415)
1 parent b2a186c commit a3beed4

File tree

2 files changed

+589
-52
lines changed

2 files changed

+589
-52
lines changed

internal/sidekick/internal/parser/pagination.go

Lines changed: 83 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,68 +29,99 @@ const (
2929
// [AIP-4233](https://google.aip.dev/client-libraries/4233) as pageable.
3030
func updateMethodPagination(a *api.API) {
3131
for _, m := range a.State.MethodByID {
32-
if m.InputTypeID == "" || m.OutputTypeID == "" {
32+
reqMsg := a.State.MessageByID[m.InputTypeID]
33+
pageTokenField := paginationRequestInfo(reqMsg)
34+
if pageTokenField == nil {
3335
continue
3436
}
3537

36-
reqMsg := a.State.MessageByID[m.InputTypeID]
37-
if reqMsg == nil {
38+
respMsg := a.State.MessageByID[m.OutputTypeID]
39+
paginationInfo := paginationResponseInfo(respMsg)
40+
if paginationInfo == nil {
3841
continue
3942
}
40-
var hasPageSize bool
41-
var hasPageToken *api.Field
42-
for _, f := range reqMsg.Fields {
43-
// Some legacy services (e.g. sqladmin.googleapis.com)
44-
// predate AIP-4233 and use `maxResults` instead of
45-
// `pageSize` for the field name.
46-
switch f.JSONName {
47-
case pageSize:
48-
if f.Typez == api.INT32_TYPE {
49-
hasPageSize = true
50-
}
51-
case maxResults:
52-
// Legacy maxResults types can be int32/uint32, and protobuf wrappers Int32Value/UInt32Value.
53-
if f.Typez == api.INT32_TYPE || f.Typez == api.UINT32_TYPE ||
54-
(f.Typez == api.MESSAGE_TYPE &&
55-
(f.TypezID == ".google.protobuf.Int32Value" || f.TypezID == ".google.protobuf.UInt32Value")) {
56-
hasPageSize = true
57-
}
58-
}
59-
if f.JSONName == pageToken && f.Typez == api.STRING_TYPE {
60-
hasPageToken = f
61-
}
62-
if hasPageSize && hasPageToken != nil {
63-
break
64-
}
43+
m.Pagination = pageTokenField
44+
respMsg.Pagination = paginationInfo
45+
}
46+
}
47+
48+
func paginationRequestInfo(request *api.Message) *api.Field {
49+
if request == nil {
50+
return nil
51+
}
52+
if pageSizeField := paginationRequestPageSize(request); pageSizeField == nil {
53+
return nil
54+
}
55+
return paginationRequestToken(request)
56+
}
57+
58+
func paginationRequestPageSize(request *api.Message) *api.Field {
59+
for _, field := range request.Fields {
60+
// Some legacy services (e.g. sqladmin.googleapis.com)
61+
// predate AIP-4233 and use `maxResults` instead of
62+
// `pageSize` for the field name.
63+
if field.JSONName == pageSize && isPaginationPageSizeType(field) {
64+
return field
6565
}
66-
if !hasPageSize || hasPageToken == nil {
67-
continue
66+
if field.JSONName == maxResults && isPaginationMaxResultsType(field) {
67+
return field
6868
}
69+
}
70+
return nil
71+
}
6972

70-
respMsg := a.State.MessageByID[m.OutputTypeID]
71-
if respMsg == nil {
72-
continue
73+
func isPaginationPageSizeType(field *api.Field) bool {
74+
return field.Typez == api.INT32_TYPE || field.Typez == api.UINT32_TYPE
75+
}
76+
77+
func isPaginationMaxResultsType(field *api.Field) bool {
78+
// Legacy maxResults types can be int32/uint32, and protobuf wrappers Int32Value/UInt32Value.
79+
if isPaginationPageSizeType(field) {
80+
return true
81+
}
82+
return field.Typez == api.MESSAGE_TYPE &&
83+
(field.TypezID == ".google.protobuf.Int32Value" ||
84+
field.TypezID == ".google.protobuf.UInt32Value")
85+
}
86+
87+
func paginationRequestToken(request *api.Message) *api.Field {
88+
for _, field := range request.Fields {
89+
if field.JSONName == pageToken && field.Typez == api.STRING_TYPE {
90+
return field
7391
}
74-
var hasNextPageToken bool
75-
var hasRepeatedItem bool
76-
info := api.PaginationInfo{}
77-
for _, f := range respMsg.Fields {
78-
if f.JSONName == nextPageToken && f.Typez == api.STRING_TYPE {
79-
hasNextPageToken = true
80-
info.NextPageToken = f
81-
}
82-
if f.Repeated && f.Typez == api.MESSAGE_TYPE {
83-
hasRepeatedItem = true
84-
info.PageableItem = f
85-
}
86-
if hasNextPageToken && hasRepeatedItem {
87-
break
88-
}
92+
}
93+
return nil
94+
}
95+
96+
func paginationResponseInfo(response *api.Message) *api.PaginationInfo {
97+
if response == nil {
98+
return nil
99+
}
100+
pageableItem := paginationResponseItem(response)
101+
nextPageToken := paginationResponseNextPageToken(response)
102+
if pageableItem == nil || nextPageToken == nil {
103+
return nil
104+
}
105+
return &api.PaginationInfo{
106+
PageableItem: pageableItem,
107+
NextPageToken: nextPageToken,
108+
}
109+
}
110+
111+
func paginationResponseItem(response *api.Message) *api.Field {
112+
for _, field := range response.Fields {
113+
if field.Repeated && field.Typez == api.MESSAGE_TYPE {
114+
return field
89115
}
90-
if !hasNextPageToken || !hasRepeatedItem {
91-
continue
116+
}
117+
return nil
118+
}
119+
120+
func paginationResponseNextPageToken(response *api.Message) *api.Field {
121+
for _, field := range response.Fields {
122+
if field.JSONName == nextPageToken && field.Typez == api.STRING_TYPE {
123+
return field
92124
}
93-
m.Pagination = hasPageToken
94-
respMsg.Pagination = &info
95125
}
126+
return nil
96127
}

0 commit comments

Comments
 (0)