code: func (impl *repoImpl) paginatedQuery(c context.Context, opts paginatedQueryOpts) (out []*api.Instance, nextTok string, err error) {
  	// Validate the request, decode the cursor.
  	if err := common.ValidatePackageName(opts.Package); err != nil {
  		return nil, "", status.Errorf(codes.InvalidArgument, "bad 'package' - %s", err)
  	}
  
  	switch {
  	case opts.PageSize < 0:
  		return nil, "", status.Errorf(codes.InvalidArgument, "bad 'page_size' %d - it should be non-negative", opts.PageSize)
  	case opts.PageSize == 0:
  		opts.PageSize = 100
  	}
  
  	var cursor datastore.Cursor
  	if opts.PageToken != "" {
  		if cursor, err = datastore.DecodeCursor(c, opts.PageToken); err != nil {
  			return nil, "", status.Errorf(codes.InvalidArgument, "bad 'page_token' - %s", err)
  		}
  	}
  
  	if opts.Validator != nil {
  		if err := opts.Validator(); err != nil {
  			return nil, "", err
  		}
  	}
  
  	// Check ACLs.
  	if _, err := impl.checkRole(c, opts.Package, api.Role_READER); err != nil {
  		return nil, "", err
  	}
  
  	// Check that the package is registered.
  	if err := model.CheckPackageExists(c, opts.Package); err != nil {
  		return nil, "", err
  	}
  
  	// Do the actual listing.
  	inst, cursor, err := opts.Handler(cursor, opts.PageSize)
  	if err != nil {
  		return nil, "", errors.Annotate(err, "failed to query instances").Err()
  	}
  
  	// Convert results to proto.
  	out = make([]*api.Instance, len(inst))
  	for i, ent := range inst {
  		out[i] = ent.Proto()
  	}
  	if cursor != nil {
  		nextTok = cursor.String()
  	}
  	return
  }
docs: // paginatedQuery is a common part of ListInstances and SearchInstances.