Skip to content

Commit

Permalink
fix: correct infinite loop for continuation token (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Jun 30, 2023
2 parents 40d7005 + 4ba1a15 commit 78877c1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
10 changes: 5 additions & 5 deletions cmd/models/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func listModels(fgaClient client.SdkClient, maxPages int) (string, error) {
// This is needed to ensure empty array is marshaled as [] instead of nil
models := make([]openfga.AuthorizationModel, 0)

var continuationToken *string
var continuationToken string

pageIndex := 0

for {
options := client.ClientReadAuthorizationModelsOptions{
ContinuationToken: continuationToken,
ContinuationToken: &continuationToken,
}

response, err := fgaClient.ReadAuthorizationModels(context.Background()).Options(options).Execute()
Expand All @@ -51,11 +51,11 @@ func listModels(fgaClient client.SdkClient, maxPages int) (string, error) {

pageIndex++

continuationToken = response.ContinuationToken

if continuationToken == nil || pageIndex > maxPages {
if response.ContinuationToken == nil || *response.ContinuationToken == continuationToken || pageIndex > maxPages {
break
}

continuationToken = *response.ContinuationToken
}

modelsJSON, err := json.Marshal(openfga.ReadAuthorizationModelsResponse{AuthorizationModels: &models})
Expand Down
16 changes: 8 additions & 8 deletions cmd/models/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func TestListModelsEmpty(t *testing.T) {

response := openfga.ReadAuthorizationModelsResponse{
AuthorizationModels: &models,
ContinuationToken: nil,
ContinuationToken: openfga.PtrString(""),
}
mockExecute.EXPECT().Execute().Return(&response, nil)

mockRequest := mockclient.NewMockSdkClientReadAuthorizationModelsRequestInterface(mockCtrl)
options := client.ClientReadAuthorizationModelsOptions{
ContinuationToken: nil,
ContinuationToken: openfga.PtrString(""),
}
mockRequest.EXPECT().Options(options).Return(mockExecute)
mockFgaClient.EXPECT().ReadAuthorizationModels(context.Background()).Return(mockRequest)
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestListModelsFail(t *testing.T) {

mockRequest := mockclient.NewMockSdkClientReadAuthorizationModelsRequestInterface(mockCtrl)
options := client.ClientReadAuthorizationModelsOptions{
ContinuationToken: nil,
ContinuationToken: openfga.PtrString(""),
}
mockRequest.EXPECT().Options(options).Return(mockExecute)
mockFgaClient.EXPECT().ReadAuthorizationModels(context.Background()).Return(mockRequest)
Expand Down Expand Up @@ -96,13 +96,13 @@ func TestListModelsSinglePage(t *testing.T) {

response := openfga.ReadAuthorizationModelsResponse{
AuthorizationModels: &models,
ContinuationToken: nil,
ContinuationToken: openfga.PtrString(""),
}
mockExecute.EXPECT().Execute().Return(&response, nil)

mockRequest := mockclient.NewMockSdkClientReadAuthorizationModelsRequestInterface(mockCtrl)
options := client.ClientReadAuthorizationModelsOptions{
ContinuationToken: nil,
ContinuationToken: openfga.PtrString(""),
}
mockRequest.EXPECT().Options(options).Return(mockExecute)
mockFgaClient.EXPECT().ReadAuthorizationModels(context.Background()).Return(mockRequest)
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestListModelsMultiPage(t *testing.T) {

mockRequest1 := mockclient.NewMockSdkClientReadAuthorizationModelsRequestInterface(mockCtrl)
options1 := client.ClientReadAuthorizationModelsOptions{
ContinuationToken: nil,
ContinuationToken: openfga.PtrString(""),
}

mockExecute2 := mockclient.NewMockSdkClientReadAuthorizationModelsRequestInterface(mockCtrl)
Expand All @@ -161,7 +161,7 @@ func TestListModelsMultiPage(t *testing.T) {
}
response2 := openfga.ReadAuthorizationModelsResponse{
AuthorizationModels: &models2,
ContinuationToken: nil,
ContinuationToken: continuationToken1,
}

gomock.InOrder(
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestListModelsMultiPageMaxPage(t *testing.T) {

mockRequest1 := mockclient.NewMockSdkClientReadAuthorizationModelsRequestInterface(mockCtrl)
options1 := client.ClientReadAuthorizationModelsOptions{
ContinuationToken: nil,
ContinuationToken: openfga.PtrString(""),
}

mockExecute1.EXPECT().Execute().Return(&response1, nil)
Expand Down
8 changes: 4 additions & 4 deletions cmd/stores/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ var listCmd = &cobra.Command{
os.Exit(1)
}
stores := []openfga.Store{}
var continuationToken *string
continuationToken := ""
pageIndex := 0
for {
options := client.ClientListStoresOptions{
ContinuationToken: continuationToken,
ContinuationToken: &continuationToken,
}
response, err := fgaClient.ListStores(context.Background()).Options(options).Execute()
if err != nil {
Expand All @@ -62,11 +62,11 @@ var listCmd = &cobra.Command{

stores = append(stores, *response.Stores...)
pageIndex++
if continuationToken == nil || pageIndex >= maxPages {
if response.ContinuationToken == nil || *response.ContinuationToken == continuationToken || pageIndex >= maxPages {
break
}

continuationToken = response.ContinuationToken
continuationToken = *response.ContinuationToken
}

storesJSON, err := json.Marshal(openfga.ListStoresResponse{Stores: &stores})
Expand Down
10 changes: 6 additions & 4 deletions cmd/tuples/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ var changesCmd = &cobra.Command{
os.Exit(1)
}
changes := []openfga.TupleChange{}
var continuationToken *string
continuationToken := ""
pageIndex := 0
for {
body := &client.ClientReadChangesRequest{
Type: selectedType,
}
options := &client.ClientReadChangesOptions{}
options := &client.ClientReadChangesOptions{
ContinuationToken: &continuationToken,
}
response, err := fgaClient.ReadChanges(context.Background()).Body(*body).Options(*options).Execute()
if err != nil {
fmt.Printf("Failed to get tuple changes due to %v", err)
Expand All @@ -67,11 +69,11 @@ var changesCmd = &cobra.Command{

changes = append(changes, *response.Changes...)
pageIndex++
if continuationToken == nil || pageIndex >= maxPages {
if response.ContinuationToken == nil || *response.ContinuationToken == continuationToken || pageIndex >= maxPages {
break
}

continuationToken = response.ContinuationToken
continuationToken = *response.ContinuationToken
}

changesJSON, err := json.Marshal(openfga.ReadChangesResponse{Changes: &changes})
Expand Down
8 changes: 4 additions & 4 deletions cmd/tuples/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ var readCmd = &cobra.Command{
}

tuples := []openfga.Tuple{}
var continuationToken *string
continuationToken := ""
pageIndex := 0
options := client.ClientReadOptions{}
for {
options.ContinuationToken = continuationToken
options.ContinuationToken = &continuationToken
response, err := fgaClient.Read(context.Background()).Body(*body).Options(options).Execute()
if err != nil {
fmt.Printf("Failed to read tuples due to %v", err)
Expand All @@ -80,11 +80,11 @@ var readCmd = &cobra.Command{

tuples = append(tuples, *response.Tuples...)
pageIndex++
if continuationToken == nil || pageIndex >= maxPages {
if response.ContinuationToken == nil || *response.ContinuationToken == continuationToken || pageIndex >= maxPages {
break
}

continuationToken = response.ContinuationToken
continuationToken = *response.ContinuationToken
}

tuplesJSON, err := json.Marshal(openfga.ReadResponse{Tuples: &tuples})
Expand Down

0 comments on commit 78877c1

Please sign in to comment.