Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Oct 12, 2023
1 parent 81c4ffb commit 4330674
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions cmd/tuple/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,9 @@ type readResponse struct {
simple []openfga.TupleKey
}

func read(fgaClient client.SdkClient, user string, relation string, object string, maxPages int) (
*readResponse, error,
func baseRead(fgaClient client.SdkClient, body *client.ClientReadRequest, maxPages int) (
*openfga.ReadResponse, error,
) {
body := &client.ClientReadRequest{}
if user != "" {
body.User = &user
}

if relation != "" {
body.Relation = &relation
}

if object != "" {
body.Object = &object
}

tuples := make([]openfga.Tuple, 0)
continuationToken := ""
pageIndex := 0
Expand All @@ -67,19 +54,45 @@ func read(fgaClient client.SdkClient, user string, relation string, object strin
tuples = append(tuples, *response.Tuples...)
pageIndex++

if response.ContinuationToken == nil || *response.ContinuationToken == "" || (maxPages != 0 && pageIndex >= maxPages) {
if response.ContinuationToken == nil ||
*response.ContinuationToken == "" ||
(maxPages != 0 && pageIndex >= maxPages) {
break
}

continuationToken = *response.ContinuationToken
}

return &openfga.ReadResponse{Tuples: &tuples}, nil
}

func read(fgaClient client.SdkClient, user string, relation string, object string, maxPages int) (
*readResponse, error,
) {
body := &client.ClientReadRequest{}
if user != "" {
body.User = &user
}

if relation != "" {
body.Relation = &relation
}

if object != "" {
body.Object = &object
}

response, err := baseRead(fgaClient, body, maxPages)
if err != nil {
return nil, err
}

justKeys := make([]openfga.TupleKey, 0)
for _, tuple := range tuples {
for _, tuple := range response.GetTuples() {
justKeys = append(justKeys, *tuple.Key)
}

res := readResponse{complete: &openfga.ReadResponse{Tuples: &tuples}, simple: justKeys}
res := readResponse{complete: &openfga.ReadResponse{Tuples: response.Tuples}, simple: justKeys}

return &res, nil
}
Expand Down

0 comments on commit 4330674

Please sign in to comment.