Skip to content

Commit 32981fe

Browse files
authored
feat: initial support for models with conditions (#169)
2 parents 36c14c7 + 76d930f commit 32981fe

38 files changed

+487
-330
lines changed

.golangci.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ linters-settings:
4646
- github.com/spf13/pflag
4747
- github.com/spf13/viper
4848
- google.golang.org/protobuf/encoding/protojson
49+
- google.golang.org/protobuf/types/known/structpb
4950
- gopkg.in/yaml.v3
5051
test:
5152
files:
@@ -69,4 +70,4 @@ linters-settings:
6970
statements: 80
7071

7172
skip-dirs:
72-
- mocks
73+
- mocks

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## v0.2.0
4+
5+
### [0.2.0](https://github.com/openfga/cli/compare/v0.1.2...v0.2.0) (2023-11-03)
6+
7+
Changed:
8+
- add support for models with conditions
9+
310
## v0.1.2
411

512
### [0.1.2](https://github.com/openfga/cli/compare/v0.1.1...v0.1.2) (2023-10-12)

cmd/model/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func listModels(fgaClient client.SdkClient, maxPages int) (*openfga.ReadAuthoriz
5050
return nil, fmt.Errorf("failed to list models due to %w", err)
5151
}
5252

53-
models = append(models, *response.AuthorizationModels...)
53+
models = append(models, response.AuthorizationModels...)
5454

5555
pageIndex++
5656

@@ -61,7 +61,7 @@ func listModels(fgaClient client.SdkClient, maxPages int) (*openfga.ReadAuthoriz
6161
continuationToken = *response.ContinuationToken
6262
}
6363

64-
return &openfga.ReadAuthorizationModelsResponse{AuthorizationModels: &models}, nil
64+
return &openfga.ReadAuthorizationModelsResponse{AuthorizationModels: models}, nil
6565
}
6666

6767
// listCmd represents the list command.
@@ -93,7 +93,7 @@ var listCmd = &cobra.Command{
9393
}
9494

9595
models := authorizationmodel.AuthzModelList{}
96-
authzModels := *response.AuthorizationModels
96+
authzModels := response.AuthorizationModels
9797
for index := 0; index < len(authzModels); index++ {
9898
authModel := authorizationmodel.AuthzModel{}
9999
authModel.Set(authzModels[index])

cmd/model/list_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestListModelsEmpty(t *testing.T) {
2828
var models []openfga.AuthorizationModel
2929

3030
response := openfga.ReadAuthorizationModelsResponse{
31-
AuthorizationModels: &models,
31+
AuthorizationModels: models,
3232
ContinuationToken: openfga.PtrString(""),
3333
}
3434
mockExecute.EXPECT().Execute().Return(&response, nil)
@@ -101,7 +101,7 @@ func TestListModelsSinglePage(t *testing.T) {
101101
}
102102

103103
response := openfga.ReadAuthorizationModelsResponse{
104-
AuthorizationModels: &models,
104+
AuthorizationModels: models,
105105
ContinuationToken: openfga.PtrString(""),
106106
}
107107
mockExecute.EXPECT().Execute().Return(&response, nil)
@@ -149,7 +149,7 @@ func TestListModelsMultiPage(t *testing.T) {
149149
}
150150
continuationToken1 := openfga.PtrString("abcdef")
151151
response1 := openfga.ReadAuthorizationModelsResponse{
152-
AuthorizationModels: &models1,
152+
AuthorizationModels: models1,
153153
ContinuationToken: continuationToken1,
154154
}
155155

@@ -172,7 +172,7 @@ func TestListModelsMultiPage(t *testing.T) {
172172
}
173173
emptyToken := ""
174174
response2 := openfga.ReadAuthorizationModelsResponse{
175-
AuthorizationModels: &models2,
175+
AuthorizationModels: models2,
176176
ContinuationToken: &emptyToken,
177177
}
178178

@@ -231,7 +231,7 @@ func TestListModelsMultiPageMaxPage(t *testing.T) {
231231
}
232232
continuationToken1 := openfga.PtrString("abcdef")
233233
response1 := openfga.ReadAuthorizationModelsResponse{
234-
AuthorizationModels: &models1,
234+
AuthorizationModels: models1,
235235
ContinuationToken: continuationToken1,
236236
}
237237

cmd/model/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var testCmd = &cobra.Command{
3333
Use: "test",
3434
Short: "Test an Authorization Model",
3535
Long: "Run a set of tests against a particular Authorization Model.",
36-
Example: `fga model test --tests tests.fga.yaml`,
36+
Example: `fga model test --tests model.fga.yaml`,
3737
RunE: func(cmd *cobra.Command, args []string) error {
3838
clientConfig := cmdutils.GetClientConfig(cmd)
3939

cmd/model/write_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/golang/mock/gomock"
1010
"github.com/openfga/cli/internal/authorizationmodel"
1111
mockclient "github.com/openfga/cli/internal/mocks"
12-
openfga "github.com/openfga/go-sdk"
1312
"github.com/openfga/go-sdk/client"
1413
)
1514

@@ -71,7 +70,7 @@ func TestWriteModel(t *testing.T) {
7170

7271
modelID := "01GXSB8YR785C4FYS3C0RTG7C2"
7372
response := client.ClientWriteAuthorizationModelResponse{
74-
AuthorizationModelId: openfga.PtrString(modelID),
73+
AuthorizationModelId: modelID,
7574
}
7675
mockExecute.EXPECT().Execute().Return(&response, nil)
7776

cmd/query/check.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func check(
3131
user string,
3232
relation string,
3333
object string,
34-
contextualTuples []client.ClientTupleKey,
34+
contextualTuples []client.ClientContextualTupleKey,
3535
) (*client.ClientCheckResponse, error) {
3636
body := &client.ClientCheckRequest{
3737
User: user,
3838
Relation: relation,
3939
Object: object,
40-
ContextualTuples: &contextualTuples,
40+
ContextualTuples: contextualTuples,
4141
}
4242
options := &client.ClientCheckOptions{}
4343

cmd/query/check_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ func TestCheckWithError(t *testing.T) {
3131
mockRequest.EXPECT().Options(options).Return(mockExecute)
3232

3333
mockBody := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl)
34-
contextualTuples := []client.ClientTupleKey{
34+
contextualTuples := []client.ClientContextualTupleKey{
3535
{User: "user:foo", Relation: "admin", Object: "doc:doc1"},
3636
}
3737

3838
body := client.ClientCheckRequest{
3939
User: "user:foo",
4040
Relation: "writer",
4141
Object: "doc:doc1",
42-
ContextualTuples: &contextualTuples,
42+
ContextualTuples: contextualTuples,
4343
}
4444
mockBody.EXPECT().Body(body).Return(mockRequest)
4545

@@ -75,14 +75,14 @@ func TestCheckWithNoError(t *testing.T) {
7575

7676
mockBody := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl)
7777

78-
contextualTuples := []client.ClientTupleKey{
78+
contextualTuples := []client.ClientContextualTupleKey{
7979
{User: "user:foo", Relation: "admin", Object: "doc:doc1"},
8080
}
8181
body := client.ClientCheckRequest{
8282
User: "user:foo",
8383
Relation: "writer",
8484
Object: "doc:doc1",
85-
ContextualTuples: &contextualTuples,
85+
ContextualTuples: contextualTuples,
8686
}
8787
mockBody.EXPECT().Body(body).Return(mockRequest)
8888

cmd/query/list-objects.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func listObjects(
3232
user string,
3333
relation string,
3434
objectType string,
35-
contextualTuples []client.ClientTupleKey,
35+
contextualTuples []client.ClientContextualTupleKey,
3636
) (*client.ClientListObjectsResponse, error) {
3737
body := &client.ClientListObjectsRequest{
3838
User: user,
3939
Relation: relation,
4040
Type: objectType,
41-
ContextualTuples: &contextualTuples,
41+
ContextualTuples: contextualTuples,
4242
}
4343
options := &client.ClientListObjectsOptions{}
4444

cmd/query/list-objects_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func TestListObjectsWithError(t *testing.T) {
3131

3232
mockBody := mock_client.NewMockSdkClientListObjectsRequestInterface(mockCtrl)
3333

34-
contextualTuples := []client.ClientTupleKey{
34+
contextualTuples := []client.ClientContextualTupleKey{
3535
{User: "user:foo", Relation: "admin", Object: "doc:doc1"},
3636
}
3737
body := client.ClientListObjectsRequest{
3838
User: "user:foo",
3939
Relation: "writer",
4040
Type: "doc",
41-
ContextualTuples: &contextualTuples,
41+
ContextualTuples: contextualTuples,
4242
}
4343
mockBody.EXPECT().Body(body).Return(mockRequest)
4444

@@ -60,7 +60,7 @@ func TestListObjectsWithNoError(t *testing.T) {
6060
mockExecute := mock_client.NewMockSdkClientListObjectsRequestInterface(mockCtrl)
6161

6262
expectedResponse := client.ClientListObjectsResponse{
63-
Objects: &[]string{"doc:doc1", "doc:doc2"},
63+
Objects: []string{"doc:doc1", "doc:doc2"},
6464
}
6565

6666
mockExecute.EXPECT().Execute().Return(&expectedResponse, nil)
@@ -71,14 +71,14 @@ func TestListObjectsWithNoError(t *testing.T) {
7171

7272
mockBody := mock_client.NewMockSdkClientListObjectsRequestInterface(mockCtrl)
7373

74-
contextualTuples := []client.ClientTupleKey{
74+
contextualTuples := []client.ClientContextualTupleKey{
7575
{User: "user:foo", Relation: "admin", Object: "doc:doc1"},
7676
}
7777
body := client.ClientListObjectsRequest{
7878
User: "user:foo",
7979
Relation: "writer",
8080
Type: "doc",
81-
ContextualTuples: &contextualTuples,
81+
ContextualTuples: contextualTuples,
8282
}
8383
mockBody.EXPECT().Body(body).Return(mockRequest)
8484

@@ -89,7 +89,7 @@ func TestListObjectsWithNoError(t *testing.T) {
8989
t.Error(err)
9090
}
9191

92-
if *output != expectedResponse {
92+
if output != &expectedResponse {
9393
t.Errorf("Expect %v but actual %v", expectedResponse, *output)
9494
}
9595
}

0 commit comments

Comments
 (0)