Java implementation of ANTLR Grammar for the OpenFGA DSL and parser from and to the OpenFGA JSON Syntax
From dev.openfga/openfga-language on MavenCentral
import dev.openfga.language.DslToJsonTransformer;
import dev.openfga.language.JsonToDslTransformer;
...
var dslString = """
model
schema 1.2
type user
type folder
relations
define viewer: [user]
""";
// Transform from DSL to a JSON string
var generatedJsonString = new DslToJsonTransformer().transform(dslString);
// Transform from a JSON string to DSL
var generatedDsl = new JsonToDslTransformer().transform(generatedJsonString);
// Parses DSL returning results DSL without throwing
var result = new DslToJsonTransformer().parseDsl(dslString);
import dev.openfga.language.FgaModTransformer;
...
var modFileContents = """
schema: "1.2"
contents:
- core.fga
- board.fga
- wiki.fga
""";
// Parse fga.mod to a FgaModFile object
var mod = new FgaModTransformer(modFileContents).parse();
// Parse fga.mod to JSON string
var jsonMod = new FgaModTransformer(modFileContents).transform();
Not yet implemented, but the enchancement is in our issue backlog.
import dev.openfga.language.validation.ModelValidator;
import dev.openfga.language.errors.DslErrorsException;
...
var dslString = """
model
schema 1.2
type user
type folder
relations
define viewer: [user]
""";
try {
ModelValidator.validateDsl(dslString);
} catch (DslErrorsException e) {
// Handle generated errors
return e.getErrors();
} catch (IOException e) {
throw new RuntimeException(e);
}
This project is licensed under the Apache-2.0 license. See the LICENSE file for more info.