Skip to content

Commit

Permalink
refactor(write): normalize deletes field
Browse files Browse the repository at this point in the history
  • Loading branch information
jpadilla committed Nov 22, 2023
1 parent c3ee17e commit 8a7851a
Show file tree
Hide file tree
Showing 18 changed files with 518 additions and 399 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ By default, write runs in a transaction mode where any invalid operation (deleti

```golang
body := ClientWriteRequest{
Writes: &[]ClientTupleKey{ {
Writes: &[]ClientWriteRequestTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand All @@ -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 All @@ -483,7 +483,7 @@ The SDK will split the writes into separate chunks and send them in separate req

```golang
body := ClientWriteRequest{
Writes: &[]ClientTupleKey{ {
Writes: &[]ClientWriteRequestTupleKey{ {
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
Object: "document:roadmap",
Expand All @@ -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
6 changes: 3 additions & 3 deletions api_open_fga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func TestOpenFgaApi(t *testing.T) {
RequestPath: "write",
}
requestBody := WriteRequest{
Writes: &WriteRequestTupleKeys{
Writes: &WriteRequestWrites{
TupleKeys: []WriteRequestTupleKey{{
User: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
Relation: "viewer",
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
47 changes: 34 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type ClientRequestOptionsWithAuthZModelId struct {
}

type ClientTupleKey = fgaSdk.TupleKey
type ClientTupleKeyWithoutCondition = fgaSdk.TupleKeyWithoutCondition
type ClientWriteRequestTupleKey = fgaSdk.WriteRequestTupleKey

Check failure on line 118 in client/client.go

View workflow job for this annotation

GitHub Actions / test

undefined: fgaSdk.WriteRequestTupleKey

Check failure on line 118 in client/client.go

View workflow job for this annotation

GitHub Actions / test

undefined: fgaSdk.WriteRequestTupleKey
type ClientCheckRequestTupleKey = fgaSdk.CheckRequestTupleKey
type ClientReadRequestTupleKey = fgaSdk.ReadRequestTupleKey
Expand Down Expand Up @@ -1157,7 +1158,7 @@ type SdkClientWriteRequestInterface interface {

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

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

type ClientWriteSingleResponse struct {
type ClientWriteRequestWriteResponse struct {
TupleKey ClientWriteRequestTupleKey `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 +1224,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 +1290,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 +1306,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 +1329,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 +1341,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 Down Expand Up @@ -1377,7 +1398,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 @@ -1509,7 +1530,7 @@ type SdkClientDeleteTuplesRequestInterface interface {
GetOptions() *ClientWriteOptions
}

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

func (client *OpenFgaClient) DeleteTuples(ctx _context.Context) SdkClientDeleteTuplesRequestInterface {
return &SdkClientDeleteTuplesRequest{
Expand Down
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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)


16 changes: 8 additions & 8 deletions docs/WriteRequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Writes** | Pointer to [**WriteRequestTupleKeys**](WriteRequestTupleKeys.md) | | [optional]
**Deletes** | Pointer to [**WriteRequestTupleKeys**](WriteRequestTupleKeys.md) | | [optional]
**Writes** | Pointer to [**WriteRequestWrites**](WriteRequestWrites.md) | | [optional]
**Deletes** | Pointer to [**WriteRequestDeletes**](WriteRequestDeletes.md) | | [optional]
**AuthorizationModelId** | Pointer to **string** | | [optional]

## Methods
Expand All @@ -29,20 +29,20 @@ but it doesn't guarantee that properties required by API are set

### GetWrites

`func (o *WriteRequest) GetWrites() WriteRequestTupleKeys`
`func (o *WriteRequest) GetWrites() WriteRequestWrites`

GetWrites returns the Writes field if non-nil, zero value otherwise.

### GetWritesOk

`func (o *WriteRequest) GetWritesOk() (*WriteRequestTupleKeys, bool)`
`func (o *WriteRequest) GetWritesOk() (*WriteRequestWrites, bool)`

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

### SetWrites

`func (o *WriteRequest) SetWrites(v WriteRequestTupleKeys)`
`func (o *WriteRequest) SetWrites(v WriteRequestWrites)`

SetWrites sets Writes field to given value.

Expand All @@ -54,20 +54,20 @@ HasWrites returns a boolean if a field has been set.

### GetDeletes

`func (o *WriteRequest) GetDeletes() WriteRequestTupleKeys`
`func (o *WriteRequest) GetDeletes() WriteRequestDeletes`

GetDeletes returns the Deletes field if non-nil, zero value otherwise.

### GetDeletesOk

`func (o *WriteRequest) GetDeletesOk() (*WriteRequestTupleKeys, bool)`
`func (o *WriteRequest) GetDeletesOk() (*WriteRequestDeletes, bool)`

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

### SetDeletes

`func (o *WriteRequest) SetDeletes(v WriteRequestTupleKeys)`
`func (o *WriteRequest) SetDeletes(v WriteRequestDeletes)`

SetDeletes sets Deletes field to given value.

Expand Down
Loading

0 comments on commit 8a7851a

Please sign in to comment.