-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As more endpoints started using the API response paginator, new patterns started to emerge that required either more flexible configuration or a lot of re-used code. This commit splits the paginator into three logical components: - The Paginator is reponsible for splitting the values of an iterator into chunks of the requested size. - The Tokenizer returns a page token that is used to uniquely identify elements of the iterator. - The Filter is used to decide if a given element should be added to the page or skipped altogether. The Paginator implementation has returned, more or less, to its initial clean design and implementation. Additional logic and flow customization can be provided by implementing the Tokenizer and Filter interfaces. One Tokenizer implementation is provided (StructsTokenizer) which can be configured to emit tokens using common fields found in structs, such as ID, Namespace, and CreateIndex. The go-bexpr Evaluator implements the Filter interface, so it can be used directly. Another implementation provided is the NamespaceFilter, which can be used to apply filtering logic based on the namespaces the ACL token used for the request is allowed to access.
- Loading branch information
Showing
11 changed files
with
446 additions
and
149 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package paginator | ||
|
||
// Filter is the interface that must be implemented to skip values when using | ||
// the Paginator. | ||
type Filter interface { | ||
// Evaluate returns true if the element should be added to the page. | ||
Evaluate(interface{}) (bool, error) | ||
} | ||
|
||
// GenericFilter wraps a function that can be used to provide simple or in | ||
// scope filtering. | ||
type GenericFilter struct { | ||
Allow func(interface{}) (bool, error) | ||
} | ||
|
||
func (f GenericFilter) Evaluate(raw interface{}) (bool, error) { | ||
return f.Allow(raw) | ||
} | ||
|
||
// NamespaceFilter skips elements with a namespace value that is not in the | ||
// allowable set. | ||
type NamespaceFilter struct { | ||
AllowableNamespaces map[string]bool | ||
} | ||
|
||
func (f NamespaceFilter) Evaluate(raw interface{}) (bool, error) { | ||
if raw == nil { | ||
return false, nil | ||
} | ||
|
||
item, _ := raw.(NamespaceGetter) | ||
namespace := item.GetNamespace() | ||
|
||
if f.AllowableNamespaces == nil { | ||
return true, nil | ||
} | ||
if f.AllowableNamespaces[namespace] { | ||
return true, nil | ||
} | ||
return false, nil | ||
} |
Oops, something went wrong.