Created
August 15, 2024 22:51
-
-
Save judell/6b9750934cdcb3f52a0597f94c19f52b to your computer and use it in GitHub Desktop.
paginate.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ListFunc func(context.Context, interface{}, int, int) (interface{}, *wordpress.Response, error) | |
func paginate(ctx context.Context, d *plugin.QueryData, listFunc ListFunc, options interface{}) error { | |
perPage := 100 | |
offset := 0 | |
for { | |
plugin.Logger(ctx).Debug("WordPress paginate", "offset", offset) | |
// `listFunc`, the passed-in anonymous function, could call the go SDK's `Posts.List` (which wraps the API's | |
// `GET /wp-json/wp/v2/posts`, or `Comments.List` and `GET /wp-json/wp/v2/comments`, and so on. | |
items, _, err := listFunc(ctx, options, perPage, offset) | |
if err != nil { | |
plugin.Logger(ctx).Debug("wordpress.paginate", "query_error", err) | |
return err | |
} | |
// Use reflection to handle the 'items' regardless of its concrete type. | |
// `items` is an interface{}. It could wrap a slice of wordpress.Post, wordpress.Tag, or other, | |
// depending on what listFunc returns. We assume, however, that `items` a slice or array-like structure. | |
itemsSlice := reflect.ValueOf(items) | |
for i := 0; i < itemsSlice.Len(); i++ { | |
// Get the i-th item from the slice | |
// Index(i) returns a reflect.Value representing the i-th element | |
item := itemsSlice.Index(i).Interface() | |
// Stream the item to Steampipe | |
// Interface() converts the reflect.Value back to an interface{}, | |
// which is what StreamListItem expects | |
d.StreamListItem(ctx, item) | |
} | |
// If fewer items than perPage were returned, it's the last page | |
if itemsSlice.Len() < perPage { | |
break | |
} | |
offset += perPage | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment