UNPKG

@openfga/syntax-transformer

Version:

Javascript implementation of ANTLR Grammar for the OpenFGA DSL and parser from and to the OpenFGA JSON Syntax

51 lines (50 loc) 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getModuleForObjectTypeRelation = getModuleForObjectTypeRelation; exports.isRelationAssignable = isRelationAssignable; /** * getModuleForObjectTypeRelation - returns the module for the given object type and relation in that type. * @param typeDef - A TypeDefinition object which contains metadata about the type. * @param relation - A string representing the relation whose module is to be retrieved. * @return string - A string representing the module for the given object type and relation. * @error error - An error if the relation does not exist. */ function getModuleForObjectTypeRelation(typeDef, relation) { var _a, _b, _c; if (!((_a = typeDef.relations) === null || _a === void 0 ? void 0 : _a[relation])) { throw new Error(`relation ${relation} does not exist in type ${typeDef.type}`); } const relationsMetadata = ((_b = typeDef === null || typeDef === void 0 ? void 0 : typeDef.metadata) === null || _b === void 0 ? void 0 : _b.relations) || {}; const relationMetadata = relationsMetadata[relation]; if (!relationMetadata || !relationMetadata.module) { return ((_c = typeDef === null || typeDef === void 0 ? void 0 : typeDef.metadata) === null || _c === void 0 ? void 0 : _c.module) || undefined; } return relationMetadata.module; } /** * isRelationAssignable - returns true if the relation is assignable, as in the relation definition has a key "this" or any of its children have a key "this". * @param relDef - A Userset object representing a relation definition. * @return boolean - A boolean representing whether the relation definition has a key "this". */ function isRelationAssignable(relDef) { for (const key of Object.keys(relDef)) { const val = relDef[key]; if (key === "this") { return true; } if (key === "union" || key === "intersection") { for (const item of val.child) { if (isRelationAssignable(item)) { return true; } } } if (key === "difference") { if (isRelationAssignable(val.base) || isRelationAssignable(val.subtract)) { return true; } } } // ComputedUserset and TupleToUserset are not assignable return false; }