Skip to content

Commit

Permalink
refactor(write): normalize deletes field (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpadilla authored Nov 22, 2023
1 parent c3ee17e commit b20683f
Show file tree
Hide file tree
Showing 18 changed files with 528 additions and 410 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ body := ClientWriteRequest{
Relation: "viewer",
Object: "document:budget",
} },
Deletes: &[]ClientTupleKey{ {
Deletes: &[]ClientTupleKeyWithoutCondition{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "writer",
Object: "document:roadmap",
Expand Down Expand Up @@ -492,7 +492,7 @@ body := ClientWriteRequest{
Relation: "viewer",
Object: "document:budget",
} },
Deletes: &[]ClientTupleKey{ {
Deletes: &[]ClientTupleKeyWithoutCondition{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "writer",
Object: "document:roadmap",
Expand Down Expand Up @@ -843,6 +843,7 @@ Class | Method | HTTP request | Description
- [Tuple](docs/Tuple.md)
- [TupleChange](docs/TupleChange.md)
- [TupleKey](docs/TupleKey.md)
- [TupleKeyWithoutCondition](docs/TupleKeyWithoutCondition.md)
- [TupleOperation](docs/TupleOperation.md)
- [TupleToUserset](docs/TupleToUserset.md)
- [TypeDefinition](docs/TypeDefinition.md)
Expand All @@ -858,8 +859,8 @@ Class | Method | HTTP request | Description
- [WriteAuthorizationModelRequest](docs/WriteAuthorizationModelRequest.md)
- [WriteAuthorizationModelResponse](docs/WriteAuthorizationModelResponse.md)
- [WriteRequest](docs/WriteRequest.md)
- [WriteRequestTupleKey](docs/WriteRequestTupleKey.md)
- [WriteRequestTupleKeys](docs/WriteRequestTupleKeys.md)
- [WriteRequestDeletes](docs/WriteRequestDeletes.md)
- [WriteRequestWrites](docs/WriteRequestWrites.md)


## Contributing
Expand Down
2 changes: 2 additions & 0 deletions api_open_fga.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ type OpenFgaApi interface {
You may also specify `contextual_tuples` that will be treated as regular tuples.
The response will contain the related objects in an array in the "objects" field of the response and they will be strings in the object format `<type>:<id>` (e.g. "document:roadmap").
The number of objects in the response array will be limited by the execution timeout specified in the flag OPENFGA_LIST_OBJECTS_DEADLINE and by the upper bound specified in the flag OPENFGA_LIST_OBJECTS_MAX_RESULTS, whichever is hit first.
The objects given will not be sorted, and therefore two identical calls can give a given different set of objects.
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @return ApiListObjectsRequest
*/
Expand Down Expand Up @@ -1998,6 +1999,7 @@ An `authorization_model_id` may be specified in the body. If it is not specified
You may also specify `contextual_tuples` that will be treated as regular tuples.
The response will contain the related objects in an array in the "objects" field of the response and they will be strings in the object format `<type>:<id>` (e.g. "document:roadmap").
The number of objects in the response array will be limited by the execution timeout specified in the flag OPENFGA_LIST_OBJECTS_DEADLINE and by the upper bound specified in the flag OPENFGA_LIST_OBJECTS_MAX_RESULTS, whichever is hit first.
The objects given will not be sorted, and therefore two identical calls can give a given different set of objects.
- @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
- @return ApiListObjectsRequest
*/
Expand Down
8 changes: 4 additions & 4 deletions api_open_fga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,8 @@ func TestOpenFgaApi(t *testing.T) {
RequestPath: "write",
}
requestBody := WriteRequest{
Writes: &WriteRequestTupleKeys{
TupleKeys: []WriteRequestTupleKey{{
Writes: &WriteRequestWrites{
TupleKeys: []TupleKey{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand Down Expand Up @@ -667,8 +667,8 @@ func TestOpenFgaApi(t *testing.T) {
}

requestBody := WriteRequest{
Deletes: &WriteRequestTupleKeys{
TupleKeys: []WriteRequestTupleKey{{
Deletes: &WriteRequestDeletes{
TupleKeys: []TupleKeyWithoutCondition{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand Down
62 changes: 41 additions & 21 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type ClientRequestOptionsWithAuthZModelId struct {
}

type ClientTupleKey = fgaSdk.TupleKey
type ClientWriteRequestTupleKey = fgaSdk.WriteRequestTupleKey
type ClientTupleKeyWithoutCondition = fgaSdk.TupleKeyWithoutCondition
type ClientCheckRequestTupleKey = fgaSdk.CheckRequestTupleKey
type ClientReadRequestTupleKey = fgaSdk.ReadRequestTupleKey
type ClientExpandRequestTupleKey = fgaSdk.ExpandRequestTupleKey
Expand Down Expand Up @@ -1156,8 +1156,8 @@ type SdkClientWriteRequestInterface interface {
}

type ClientWriteRequest struct {
Writes []ClientWriteRequestTupleKey
Deletes []ClientWriteRequestTupleKey
Writes []ClientTupleKey
Deletes []ClientTupleKeyWithoutCondition
}

type TransactionOptions struct {
Expand All @@ -1182,14 +1182,34 @@ const (
FAILURE ClientWriteStatus = "CLIENT_WRITE_STATUS_FAILURE"
)

type ClientWriteSingleResponse struct {
TupleKey ClientWriteRequestTupleKey `json:"tuple_key,omitempty"`
Status ClientWriteStatus `json:"status,omitempty"`
HttpResponse *_nethttp.Response `json:"http_response,omitempty"`
Error error `json:"error,omitempty"`
type ClientWriteRequestWriteResponse struct {
TupleKey ClientTupleKey `json:"tuple_key,omitempty"`
Status ClientWriteStatus `json:"status,omitempty"`
HttpResponse *_nethttp.Response `json:"http_response,omitempty"`
Error error `json:"error,omitempty"`
}

func (o ClientWriteSingleResponse) MarshalJSON() ([]byte, error) {
func (o ClientWriteRequestWriteResponse) MarshalJSON() ([]byte, error) {
toSerialize := map[string]interface{}{}
toSerialize["tuple_key"] = o.TupleKey
toSerialize["status"] = o.Status
if o.HttpResponse != nil {
toSerialize["http_response"] = o.HttpResponse
}
if o.Error != nil {
toSerialize["error"] = o.Error
}
return json.Marshal(toSerialize)
}

type ClientWriteRequestDeleteResponse struct {
TupleKey ClientTupleKeyWithoutCondition `json:"tuple_key,omitempty"`
Status ClientWriteStatus `json:"status,omitempty"`
HttpResponse *_nethttp.Response `json:"http_response,omitempty"`
Error error `json:"error,omitempty"`
}

func (o ClientWriteRequestDeleteResponse) MarshalJSON() ([]byte, error) {
toSerialize := map[string]interface{}{}
toSerialize["tuple_key"] = o.TupleKey
toSerialize["status"] = o.Status
Expand All @@ -1203,8 +1223,8 @@ func (o ClientWriteSingleResponse) MarshalJSON() ([]byte, error) {
}

type ClientWriteResponse struct {
Writes []ClientWriteSingleResponse `json:"writes,omitempty"`
Deletes []ClientWriteSingleResponse `json:"deletes,omitempty"`
Writes []ClientWriteRequestWriteResponse `json:"writes,omitempty"`
Deletes []ClientWriteRequestDeleteResponse `json:"deletes,omitempty"`
}

func (o ClientWriteResponse) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -1269,8 +1289,8 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface
}

response := ClientWriteResponse{
Writes: []ClientWriteSingleResponse{},
Deletes: []ClientWriteSingleResponse{},
Writes: []ClientWriteRequestWriteResponse{},
Deletes: []ClientWriteRequestDeleteResponse{},
}

authorizationModelId, err := client.getAuthorizationModelId(request.GetAuthorizationModelIdOverride())
Expand All @@ -1285,14 +1305,14 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface
AuthorizationModelId: authorizationModelId,
}
if request.GetBody().Writes != nil && len(request.GetBody().Writes) > 0 {
writes := fgaSdk.WriteRequestTupleKeys{}
writes := fgaSdk.WriteRequestWrites{}
for index := 0; index < len(request.GetBody().Writes); index++ {
writes.TupleKeys = append(writes.TupleKeys, (request.GetBody().Writes)[index])
}
writeRequest.Writes = &writes
}
if request.GetBody().Deletes != nil && len(request.GetBody().Deletes) > 0 {
deletes := fgaSdk.WriteRequestTupleKeys{}
deletes := fgaSdk.WriteRequestDeletes{}
for index := 0; index < len(request.GetBody().Deletes); index++ {
deletes.TupleKeys = append(deletes.TupleKeys, (request.GetBody().Deletes)[index])
}
Expand All @@ -1308,7 +1328,7 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface
if request.GetBody() != nil && request.GetBody().Writes != nil {
writeRequestTupleKeys := request.GetBody().Writes
for index := 0; index < len(writeRequestTupleKeys); index++ {
response.Writes = append(response.Writes, ClientWriteSingleResponse{
response.Writes = append(response.Writes, ClientWriteRequestWriteResponse{
TupleKey: writeRequestTupleKeys[index],
HttpResponse: httpResponse,
Status: clientWriteStatus,
Expand All @@ -1320,7 +1340,7 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface
if request.GetBody() != nil && request.GetBody().Deletes != nil {
deleteRequestTupleKeys := request.GetBody().Deletes
for index := 0; index < len(deleteRequestTupleKeys); index++ {
response.Deletes = append(response.Deletes, ClientWriteSingleResponse{
response.Deletes = append(response.Deletes, ClientWriteRequestDeleteResponse{
TupleKey: deleteRequestTupleKeys[index],
HttpResponse: httpResponse,
Status: clientWriteStatus,
Expand All @@ -1337,7 +1357,7 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface
// - each request is a transaction
// - the max items in each request are based on maxPerChunk (default=1)
var writeChunkSize = int(maxPerChunk)
var writeChunks [][]ClientWriteRequestTupleKey
var writeChunks [][]ClientTupleKey
if request.GetBody() != nil {
for i := 0; i < len(request.GetBody().Writes); i += writeChunkSize {
end := int(math.Min(float64(i+writeChunkSize), float64(len(request.GetBody().Writes))))
Expand Down Expand Up @@ -1377,7 +1397,7 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface
_ = writeGroup.Wait()

var deleteChunkSize = int(maxPerChunk)
var deleteChunks [][]ClientWriteRequestTupleKey
var deleteChunks [][]ClientTupleKeyWithoutCondition
if request.GetBody() != nil {
for i := 0; i < len(request.GetBody().Deletes); i += deleteChunkSize {
end := int(math.Min(float64(i+writeChunkSize), float64(len(request.GetBody().Deletes))))
Expand Down Expand Up @@ -1445,7 +1465,7 @@ type SdkClientWriteTuplesRequestInterface interface {
GetOptions() *ClientWriteOptions
}

type ClientWriteTuplesBody = []ClientWriteRequestTupleKey
type ClientWriteTuplesBody = []ClientTupleKey

func (client *OpenFgaClient) WriteTuples(ctx _context.Context) SdkClientWriteTuplesRequestInterface {
return &SdkClientWriteTuplesRequest{
Expand Down Expand Up @@ -1509,7 +1529,7 @@ type SdkClientDeleteTuplesRequestInterface interface {
GetOptions() *ClientWriteOptions
}

type ClientDeleteTuplesBody = []ClientWriteRequestTupleKey
type ClientDeleteTuplesBody = []ClientTupleKeyWithoutCondition

func (client *OpenFgaClient) DeleteTuples(ctx _context.Context) SdkClientDeleteTuplesRequestInterface {
return &SdkClientDeleteTuplesRequest{
Expand Down
12 changes: 6 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func TestOpenFgaClient(t *testing.T) {
RequestPath: "write",
}
requestBody := ClientWriteRequest{
Writes: []ClientWriteRequestTupleKey{{
Writes: []ClientTupleKey{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand Down Expand Up @@ -761,7 +761,7 @@ func TestOpenFgaClient(t *testing.T) {
RequestPath: "write",
}
requestBody := ClientWriteRequest{
Writes: []ClientWriteRequestTupleKey{{
Writes: []ClientTupleKey{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand Down Expand Up @@ -791,7 +791,7 @@ func TestOpenFgaClient(t *testing.T) {
RequestPath: "write",
}
requestBody := ClientWriteRequest{
Writes: []ClientWriteRequestTupleKey{{
Writes: []ClientTupleKey{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand All @@ -800,7 +800,7 @@ func TestOpenFgaClient(t *testing.T) {
Relation: "viewer",
Object: "document:budget",
}},
Deletes: []ClientWriteRequestTupleKey{{
Deletes: []ClientTupleKeyWithoutCondition{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:planning",
Expand Down Expand Up @@ -889,7 +889,7 @@ func TestOpenFgaClient(t *testing.T) {
Method: http.MethodPost,
RequestPath: "write",
}
requestBody := []ClientWriteRequestTupleKey{{
requestBody := []ClientTupleKey{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand Down Expand Up @@ -972,7 +972,7 @@ func TestOpenFgaClient(t *testing.T) {
RequestPath: "write",
}

requestBody := []ClientWriteRequestTupleKey{{
requestBody := []ClientTupleKeyWithoutCondition{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand Down
93 changes: 93 additions & 0 deletions docs/TupleKeyWithoutCondition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# TupleKeyWithoutCondition

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**User** | **string** | |
**Relation** | **string** | |
**Object** | **string** | |

## Methods

### NewTupleKeyWithoutCondition

`func NewTupleKeyWithoutCondition(user string, relation string, object string, ) *TupleKeyWithoutCondition`

NewTupleKeyWithoutCondition instantiates a new TupleKeyWithoutCondition object
This constructor will assign default values to properties that have it defined,
and makes sure properties required by API are set, but the set of arguments
will change when the set of required properties is changed

### NewTupleKeyWithoutConditionWithDefaults

`func NewTupleKeyWithoutConditionWithDefaults() *TupleKeyWithoutCondition`

NewTupleKeyWithoutConditionWithDefaults instantiates a new TupleKeyWithoutCondition object
This constructor will only assign default values to properties that have it defined,
but it doesn't guarantee that properties required by API are set

### GetUser

`func (o *TupleKeyWithoutCondition) GetUser() string`

GetUser returns the User field if non-nil, zero value otherwise.

### GetUserOk

`func (o *TupleKeyWithoutCondition) GetUserOk() (*string, bool)`

GetUserOk returns a tuple with the User field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.

### SetUser

`func (o *TupleKeyWithoutCondition) SetUser(v string)`

SetUser sets User field to given value.


### GetRelation

`func (o *TupleKeyWithoutCondition) GetRelation() string`

GetRelation returns the Relation field if non-nil, zero value otherwise.

### GetRelationOk

`func (o *TupleKeyWithoutCondition) GetRelationOk() (*string, bool)`

GetRelationOk returns a tuple with the Relation field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.

### SetRelation

`func (o *TupleKeyWithoutCondition) SetRelation(v string)`

SetRelation sets Relation field to given value.


### GetObject

`func (o *TupleKeyWithoutCondition) GetObject() string`

GetObject returns the Object field if non-nil, zero value otherwise.

### GetObjectOk

`func (o *TupleKeyWithoutCondition) GetObjectOk() (*string, bool)`

GetObjectOk returns a tuple with the Object field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.

### SetObject

`func (o *TupleKeyWithoutCondition) SetObject(v string)`

SetObject sets Object field to given value.



[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Loading

0 comments on commit b20683f

Please sign in to comment.