Convert PageLinker to DeltaPageLinker for NonDelta pagers (#3411)

This ensures that the interface for both Delta and NonDelta requests are the same and we don't have to cast the type before getting delta url from the returned object.

Follow up to https://github.com/alcionai/corso/pull/3212#discussion_r1191511216

---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* #<issue>

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Abin Simon 2023-05-16 10:56:13 +05:30 committed by GitHub
parent a55758020f
commit 5a2cd41d57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 22 deletions

View File

@ -222,13 +222,13 @@ func NewContactPager(
return &contactPager{gs, builder, options}, nil return &contactPager{gs, builder, options}, nil
} }
func (p *contactPager) getPage(ctx context.Context) (api.PageLinker, error) { func (p *contactPager) getPage(ctx context.Context) (api.DeltaPageLinker, error) {
resp, err := p.builder.Get(ctx, p.options) resp, err := p.builder.Get(ctx, p.options)
if err != nil { if err != nil {
return nil, graph.Stack(ctx, err) return nil, graph.Stack(ctx, err)
} }
return resp, nil return api.EmptyDeltaLinker[models.Contactable]{PageLinkValuer: resp}, nil
} }
func (p *contactPager) setNext(nextLink string) { func (p *contactPager) setNext(nextLink string) {
@ -301,7 +301,7 @@ func NewContactDeltaPager(
return &contactDeltaPager{gs, user, directoryID, builder, options}, nil return &contactDeltaPager{gs, user, directoryID, builder, options}, nil
} }
func (p *contactDeltaPager) getPage(ctx context.Context) (api.PageLinker, error) { func (p *contactDeltaPager) getPage(ctx context.Context) (api.DeltaPageLinker, error) {
resp, err := p.builder.Get(ctx, p.options) resp, err := p.builder.Get(ctx, p.options)
if err != nil { if err != nil {
return nil, graph.Stack(ctx, err) return nil, graph.Stack(ctx, err)

View File

@ -283,13 +283,13 @@ func NewEventPager(
return &eventPager{gs, builder, options}, nil return &eventPager{gs, builder, options}, nil
} }
func (p *eventPager) getPage(ctx context.Context) (api.PageLinker, error) { func (p *eventPager) getPage(ctx context.Context) (api.DeltaPageLinker, error) {
resp, err := p.builder.Get(ctx, p.options) resp, err := p.builder.Get(ctx, p.options)
if err != nil { if err != nil {
return nil, graph.Stack(ctx, err) return nil, graph.Stack(ctx, err)
} }
return resp, nil return api.EmptyDeltaLinker[models.Eventable]{PageLinkValuer: resp}, nil
} }
func (p *eventPager) setNext(nextLink string) { func (p *eventPager) setNext(nextLink string) {
@ -359,7 +359,7 @@ func getEventDeltaBuilder(
return builder return builder
} }
func (p *eventDeltaPager) getPage(ctx context.Context) (api.PageLinker, error) { func (p *eventDeltaPager) getPage(ctx context.Context) (api.DeltaPageLinker, error) {
resp, err := p.builder.Get(ctx, p.options) resp, err := p.builder.Get(ctx, p.options)
if err != nil { if err != nil {
return nil, graph.Stack(ctx, err) return nil, graph.Stack(ctx, err)

View File

@ -385,13 +385,13 @@ func NewMailPager(
return &mailPager{gs, builder, options}, nil return &mailPager{gs, builder, options}, nil
} }
func (p *mailPager) getPage(ctx context.Context) (api.PageLinker, error) { func (p *mailPager) getPage(ctx context.Context) (api.DeltaPageLinker, error) {
page, err := p.builder.Get(ctx, p.options) page, err := p.builder.Get(ctx, p.options)
if err != nil { if err != nil {
return nil, graph.Stack(ctx, err) return nil, graph.Stack(ctx, err)
} }
return page, nil return api.EmptyDeltaLinker[models.Messageable]{PageLinkValuer: page}, nil
} }
func (p *mailPager) setNext(nextLink string) { func (p *mailPager) setNext(nextLink string) {
@ -465,7 +465,7 @@ func NewMailDeltaPager(
return &mailDeltaPager{gs, user, directoryID, builder, options}, nil return &mailDeltaPager{gs, user, directoryID, builder, options}, nil
} }
func (p *mailDeltaPager) getPage(ctx context.Context) (api.PageLinker, error) { func (p *mailDeltaPager) getPage(ctx context.Context) (api.DeltaPageLinker, error) {
page, err := p.builder.Get(ctx, p.options) page, err := p.builder.Get(ctx, p.options)
if err != nil { if err != nil {
return nil, graph.Stack(ctx, err) return nil, graph.Stack(ctx, err)

View File

@ -18,7 +18,7 @@ import (
type itemPager interface { type itemPager interface {
// getPage get a page with the specified options from graph // getPage get a page with the specified options from graph
getPage(context.Context) (api.PageLinker, error) getPage(context.Context) (api.DeltaPageLinker, error)
// setNext is used to pass in the next url got from graph // setNext is used to pass in the next url got from graph
setNext(string) setNext(string)
// reset is used to clear delta url in delta pagers. When // reset is used to clear delta url in delta pagers. When
@ -119,8 +119,6 @@ func getItemsAddedAndRemovedFromContainer(
addedIDs = []string{} addedIDs = []string{}
removedIDs = []string{} removedIDs = []string{}
deltaURL string deltaURL string
nextLink string
deltaLink string
) )
itemCount := 0 itemCount := 0
@ -160,13 +158,7 @@ func getItemsAddedAndRemovedFromContainer(
} }
} }
dresp, ok := resp.(api.DeltaPageLinker) nextLink, deltaLink := api.NextAndDeltaLink(resp)
if ok {
nextLink, deltaLink = api.NextAndDeltaLink(dresp)
} else {
nextLink = api.NextLink(resp)
deltaLink = "" // to make sure we don't use an old value
}
// the deltaLink is kind of like a cursor for overall data state. // the deltaLink is kind of like a cursor for overall data state.
// once we run through pages of nextLinks, the last query will // once we run through pages of nextLinks, the last query will

View File

@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/connector/graph"
"github.com/alcionai/corso/src/internal/connector/graph/api" "github.com/alcionai/corso/src/internal/connector/graph/api"
"github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/internal/tester"
@ -31,8 +32,13 @@ func (v testPagerValue) GetAdditionalData() map[string]any {
type testPage struct{} type testPage struct{}
func (p testPage) GetOdataNextLink() *string { func (p testPage) GetOdataNextLink() *string {
next := "" // no next, just one page // no next, just one page
return &next return ptr.To("")
}
func (p testPage) GetOdataDeltaLink() *string {
// delta is not tested here
return ptr.To("")
} }
var _ itemPager = &testPager{} var _ itemPager = &testPager{}
@ -45,7 +51,7 @@ type testPager struct {
needsReset bool needsReset bool
} }
func (p *testPager) getPage(ctx context.Context) (api.PageLinker, error) { func (p *testPager) getPage(ctx context.Context) (api.DeltaPageLinker, error) {
if p.errorCode != "" { if p.errorCode != "" {
ierr := odataerrors.NewMainError() ierr := odataerrors.NewMainError()
ierr.SetCode(&p.errorCode) ierr.SetCode(&p.errorCode)

View File

@ -27,3 +27,25 @@ func NextLink(pl PageLinker) string {
func NextAndDeltaLink(pl DeltaPageLinker) (string, string) { func NextAndDeltaLink(pl DeltaPageLinker) (string, string) {
return NextLink(pl), ptr.Val(pl.GetOdataDeltaLink()) return NextLink(pl), ptr.Val(pl.GetOdataDeltaLink())
} }
type Valuer[T any] interface {
GetValue() []T
}
type PageLinkValuer[T any] interface {
PageLinker
Valuer[T]
}
// EmptyDeltaLinker is used to convert PageLinker to DeltaPageLinker
type EmptyDeltaLinker[T any] struct {
PageLinkValuer[T]
}
func (EmptyDeltaLinker[T]) GetOdataDeltaLink() *string {
return ptr.To("")
}
func (e EmptyDeltaLinker[T]) GetValue() []T {
return e.PageLinkValuer.GetValue()
}