Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pluralsh/polly
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.1.5
Choose a base ref
...
head repository: pluralsh/polly
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.1.6
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Mar 12, 2024

  1. Make map merge variadic (#23)

    Can help with cases where you actually need to merge 3+ maps which does happen with helm-y stuff.
    michaeljguarino authored Mar 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    afa6af0 View commit details
  2. add generic paginator (#24)

    * add generic paginator
    
    * improve NextPage
    
    * improve NextPage
    zreigz authored Mar 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    fd7c1ec View commit details
Showing with 61 additions and 2 deletions.
  1. +11 −2 algorithms/map.go
  2. +50 −0 algorithms/pager.go
13 changes: 11 additions & 2 deletions algorithms/map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package algorithms

func Merge(m1, m2 map[string]interface{}) map[string]interface{} {
func Merge(maps ...map[string]interface{}) map[string]interface{} {
res := maps[0]
for _, m := range maps[1:] {
res = deepMerge(res, m)
}

return res
}

func deepMerge(m1, m2 map[string]interface{}) map[string]interface{} {
// lifted from helm's merge code
out := make(map[string]interface{}, len(m1))
for k, v := range m1 {
@@ -11,7 +20,7 @@ func Merge(m1, m2 map[string]interface{}) map[string]interface{} {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = Merge(bv, v)
out[k] = deepMerge(bv, v)
continue
}
}
50 changes: 50 additions & 0 deletions algorithms/pager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package algorithms

type FetchPageFunc[T any] func(page *string, size int64) ([]T, *PageInfo, error)

type PageInfo struct {
HasNext bool
After *string
PageSize int64
}

type Pager[T any] struct {
pageInfo *PageInfo
err error

fetchPage FetchPageFunc[T]
}

type PageIter[T any] interface {
NextPage() ([]T, error)
}

func NewPager[T any](pageSize int64, fetchPage FetchPageFunc[T]) *Pager[T] {
p := &Pager[T]{
fetchPage: fetchPage,
}
p.pageInfo = &PageInfo{
HasNext: true,
After: nil,
PageSize: pageSize,
}
return p
}

func (p *Pager[T]) NextPage() ([]T, error) {
var list []T

if !p.pageInfo.HasNext {
return nil, nil
}

list, p.pageInfo, p.err = p.fetchPage(p.pageInfo.After, p.pageInfo.PageSize)
if p.err != nil {
return nil, p.err
}
return list, nil
}

func (p *Pager[T]) HasNext() bool {
return p.pageInfo.HasNext
}