Skip to content

Commit 4c7ddf3

Browse files
authored
feat!: add transform to proto and must transform (#89)
2 parents fb3314f + 31fd10d commit 4c7ddf3

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

pkg/go/transformer/dsltojson.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/hashicorp/go-multierror"
99
pb "github.com/openfga/api/proto/openfga/v1"
1010
parser "github.com/openfga/language/pkg/go/gen"
11+
"google.golang.org/protobuf/encoding/protojson"
1112
)
1213

1314
type RelationDefinitionOperator string
@@ -308,8 +309,8 @@ func ParseDSL(data string) (*OpenFgaDslListener, *OpenFgaDslErrorListener) {
308309
return listener, errorListener
309310
}
310311

311-
// TransformDSLToJSON - Converts models authored in FGA DSL syntax to the json syntax accepted by the OpenFGA API
312-
func TransformDSLToJSON(data string) (*pb.AuthorizationModel, error) {
312+
// TransformDSLToProto - Converts models authored in FGA DSL syntax to the OpenFGA Authorization Model Protobuf format
313+
func TransformDSLToProto(data string) (*pb.AuthorizationModel, error) {
313314
listener, errorListener := ParseDSL(data)
314315

315316
if errorListener.Errors != nil {
@@ -318,3 +319,39 @@ func TransformDSLToJSON(data string) (*pb.AuthorizationModel, error) {
318319

319320
return &listener.authorizationModel, nil
320321
}
322+
323+
// MustTransformDSLToProto - Calls TransformDSLToProto - panics if the error fails
324+
func MustTransformDSLToProto(data string) *pb.AuthorizationModel {
325+
model, err := TransformDSLToProto(data)
326+
if err != nil {
327+
panic(err)
328+
}
329+
330+
return model
331+
}
332+
333+
// TransformDSLToJSON - Converts models authored in FGA DSL syntax to the json syntax accepted by the OpenFGA API
334+
func TransformDSLToJSON(data string) (string, error) {
335+
model, err := TransformDSLToProto(data)
336+
337+
if err != nil {
338+
return "", err
339+
}
340+
341+
bytes, err := protojson.Marshal(model)
342+
if err != nil {
343+
return "", fmt.Errorf("failed to marshal due to %w", err)
344+
}
345+
346+
return string(bytes), nil
347+
}
348+
349+
// MustTransformDSLToJSON - Calls TransformDSLToJSON - panics if the error fails
350+
func MustTransformDSLToJSON(data string) string {
351+
jsonString, err := TransformDSLToJSON(data)
352+
if err != nil {
353+
panic(err)
354+
}
355+
356+
return jsonString
357+
}

pkg/go/transformer/dsltojson_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,18 @@ func TestDSLToJSONTransformerForValidCases(t *testing.T) {
2121
t.Skip()
2222
}
2323

24-
model, err := transformer.TransformDSLToJSON(testCase.DSL)
24+
jsonString, err := transformer.TransformDSLToJSON(testCase.DSL)
2525

2626
require.NoError(t, err)
2727

28-
bytes, err := protojson.Marshal(model)
29-
require.NoError(t, err)
30-
3128
expectedAuthModel := &pb.AuthorizationModel{}
3229
err = protojson.Unmarshal([]byte(testCase.JSON), expectedAuthModel)
3330
require.NoError(t, err)
3431

3532
jsonBytes, err := protojson.Marshal(expectedAuthModel)
3633
require.NoError(t, err)
3734

38-
require.JSONEq(t, string(jsonBytes), string(bytes))
35+
require.JSONEq(t, string(jsonBytes), jsonString)
3936
})
4037
}
4138
}

pkg/js/tests/dsltojson.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { loadDSLSyntaxErrorTestCases, loadValidTransformerTestCases } from "./_testcases";
2-
import { transformDSLToJSON } from "../transformer";
2+
import { transformDSLToJSON, transformDSLToJSONObject } from "../transformer";
33

44
describe("dslToJSON", () => {
55
const validTestCases = loadValidTransformerTestCases();
@@ -8,7 +8,7 @@ describe("dslToJSON", () => {
88
const testFn = testCase.skip ? it.skip : it;
99

1010
testFn(`should transform ${testCase.name} from DSL to JSON`, () => {
11-
const jsonSyntax = transformDSLToJSON(testCase.dsl);
11+
const jsonSyntax = transformDSLToJSONObject(testCase.dsl);
1212
expect(jsonSyntax).toEqual(JSON.parse(testCase.json));
1313
});
1414
});

pkg/js/transformer/dsltojson.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class OpenFgaDslListener extends OpenFGAListener {
141141
ctx.parser?.notifyErrorListeners(
142142
`'${relationName}' is already defined in '${this.currentTypeDef?.type}'`,
143143
ctx.relationName().start,
144-
undefined
144+
undefined,
145145
);
146146
}
147147

@@ -296,11 +296,11 @@ export function parseDSL(data: string): {
296296
}
297297

298298
/**
299-
* transformDSLToJSON - Converts models authored in FGA DSL syntax to the json syntax accepted by the OpenFGA API
299+
* transformDSLToJSONObject - Converts models authored in FGA DSL syntax to the json syntax accepted by the OpenFGA API
300300
* @param {string} data
301301
* @returns {AuthorizationModel}
302302
*/
303-
export function transformDSLToJSON(data: string): AuthorizationModel {
303+
export function transformDSLToJSONObject(data: string): AuthorizationModel {
304304
const { listener, errorListener } = parseDSL(data);
305305

306306
if (errorListener.errors.length) {
@@ -309,3 +309,12 @@ export function transformDSLToJSON(data: string): AuthorizationModel {
309309

310310
return listener.authorizationModel as AuthorizationModel;
311311
}
312+
313+
/**
314+
* transformDSLToJSONObject - Converts models authored in FGA DSL syntax to a stringified json representation
315+
* @param {string} data
316+
* @returns {string}
317+
*/
318+
export function transformDSLToJSON(data: string): string {
319+
return JSON.stringify(transformDSLToJSONObject(data));
320+
}

0 commit comments

Comments
 (0)