Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve failing issues with checkDSL validation on the new grammar #57

Merged
merged 6 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"author": "OpenFGA",
"dependencies": {
"@openfga/sdk": "^0.0.2",
"lodash": "^4.17.21",
"nearley": "^2.20.1"
},
"devDependencies": {
Expand Down
267 changes: 123 additions & 144 deletions src/check-dsl.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import * as _ from "lodash";

import { Keywords } from "./keywords";
import { parseDSL } from "./parse-dsl";
import { parseDSL, RewriteType } from "./parse-dsl";
import { report } from "./reporters";

const readTypeData = (r: any, lines: any) => {
const typeName = _.trim(_.flattenDeep(r[0][2]).join(""));
const typeDefinition = _.flattenDeep([r[0][1], r[0][2]]).join("");
const typeDefinitionLine = _.findIndex(lines, (l: string) => _.trim(l) === typeDefinition) + 1;
return { typeName, typeDefinition, typeDefinitionLine };
// return the line number for the specified relation
const getLineNumber = (relation: string, lines: string[], skipIndex?: number) => {
if (!skipIndex) {
skipIndex = 0;
}
return (
lines
.slice(skipIndex)
.findIndex((line: string) => line.trim().replace(/ {2,}/g, " ").startsWith(`define ${relation}`)) + skipIndex
);
};

const defaultError = (lines: any) => {
return {
// monaco.MarkerSeverity.Error,
severity: 8,
startColumn: 0,
endColumn: Number.MAX_SAFE_INTEGER,
startLineNumber: 0,
endLineNumber: lines.length,
message: "Invalid syntax",
source: "linter",
};
};

export const checkDSL = (codeInEditor: string) => {
Expand All @@ -17,164 +33,127 @@ export const checkDSL = (codeInEditor: string) => {
const reporter = report({ lines, markers });

try {
const results = parseDSL(codeInEditor);
const relationsPerType: Record<string, string[]> = {};
const globalRelations = [Keywords.SELF as string];
const parserResults = parseDSL(codeInEditor);
const relationsPerType: Record<string, Record<string, boolean>> = {};
const globalRelations: Record<string, boolean> = { [Keywords.SELF]: true };

// Reading relations per type
_.forEach(results, (r) => {
const { typeName, typeDefinitionLine } = readTypeData(r, lines);
// Looking at the types
parserResults.forEach((typeDef) => {
const typeName = typeDef.type;

// Include keyword
const relations = [Keywords.SELF as string];
const encounteredRelationsInType: Record<string, boolean> = { [Keywords.SELF]: true };

_.forEach(r[2], (r2, idx: number) => {
const relation = _.trim(_.flattenDeep(r2).join("")).match(/define\s+(.*)\s+as\s/)?.[1];
if (!relation) {
return;
}
const lineIdx = typeDefinitionLine + idx + 1;
typeDef.relations.forEach((relationDef) => {
const { relation: relationName } = relationDef;

if (relations.includes(relation)) {
reporter.duplicateDefinition({ lineIndex: lineIdx, value: relation });
// Check if we have any duplicate relations
if (encounteredRelationsInType[relationName]) {
miparnisari marked this conversation as resolved.
Show resolved Hide resolved
// figure out what is the lineIdx in question
const initialLineIdx = getLineNumber(relationName, lines);
const duplicateLineIdx = getLineNumber(relationName, lines, initialLineIdx + 1);
reporter.duplicateDefinition({ lineIndex: duplicateLineIdx, value: relationName });
}

relations.push(relation);
globalRelations.push(relation);
encounteredRelationsInType[relationName] = true;
globalRelations[relationName] = true;
});

relationsPerType[typeName] = relations;
relationsPerType[typeName] = encounteredRelationsInType;
});

_.forEach(results, (r) => {
const { typeName, typeDefinitionLine } = readTypeData(r, lines);

_.forEach(r[2], (r2, idx: number) => {
const lineIndex = typeDefinitionLine + idx + 1;
let definition: any[] = _.flatten(r2[0][1]);

if (!definition[0].includes(Keywords.DEFINE)) {
definition = _.flatten(definition);
}

const definitionName = _.flattenDeep(definition[2]).join("");

if (definition[0].includes(Keywords.DEFINE)) {
const clauses = _.slice(definition, 7, definition.length);

_.forEach(clauses, (clause) => {
let value = _.trim(_.flattenDeep(clause).join(""));

if (value.indexOf(Keywords.OR) === 0) {
value = value.replace(`${Keywords.OR} `, "");
}

if (value.indexOf(Keywords.AND) === 0) {
value = value.replace(`${Keywords.AND} `, "");
}

const hasFrom = value.includes(Keywords.FROM);
const hasButNot = value.includes(Keywords.BUT_NOT);

if (hasButNot) {
const butNotValue = _.trim(_.last(value.split(Keywords.BUT_NOT)));

if (definitionName === butNotValue) {
reporter.invalidButNot({
lineIndex,
value: butNotValue,
clause: value,
});
} else {
reporter.invalidRelationWithinClause({
typeName,
value: butNotValue,
validRelations: relationsPerType,
clause: value,
reverse: true,
lineIndex,
});
}
} else if (hasFrom) {
// Checking: `define share as owner from parent`
const values = value.split(Keywords.FROM).map((v) => _.trim(v));

reporter.invalidRelationWithinClause({
typeName,
reverse: false,
value: values[0],
validRelations: globalRelations,
clause: value,
lineIndex,
});

if (definitionName === values[1]) {
// Checking: `define owner as writer from owner`
if (clauses.length < 2) {
reporter.invalidFrom({
lineIndex,
value: values[1],
clause: value,
});
}
} else {
reporter.invalidRelationWithinClause({
typeName,
value: values[1],
validRelations: relationsPerType,
clause: value,
reverse: true,
lineIndex,
});
}
} else if (definitionName === value) {
// Checking: `define owner as owner`
parserResults.forEach((typeDef) => {
const typeName = typeDef.type;

// parse through each of the relations to do validation
typeDef.relations.forEach((relationDef) => {
const { relation: relationName } = relationDef;
const validateTargetRelation = (typeName: string, relationName: string, target: any) => {
if (!target) {
// no need to continue to parse if there is no target
return;
}
if (relationName === target.target) {
if (target.rewrite != RewriteType.TupleToUserset) {
// the error case will be relation require self reference (i.e., define owner as owner)
const lineIndex = getLineNumber(relationName, lines);
reporter.useSelf({
lineIndex,
value,
value: relationName,
});
} else {
// Checking: `define owner as self`
reporter.invalidRelation({
} else if (relationDef.definition.targets?.length === 1) {
// define owner as writer from owner
const lineIndex = getLineNumber(relationName, lines);
reporter.invalidFrom({
lineIndex,
value,
validRelations: globalRelations,
value: target.target,
clause: target.target,
});
}
});
}

if (target.target && !globalRelations[target.target]) {
// the target relation is not defined (i.e., define owner as foo) where foo is not defined
const lineIndex = getLineNumber(relationName, lines);
const value = target.target;
reporter.invalidRelation({
lineIndex,
value,
validRelations: Object.keys(globalRelations),
});
}

if (target.from && !relationsPerType[typeName][target.from]) {
// The "from" is not defined for the current type `define owner as member from writer`
const lineIndex = getLineNumber(relationName, lines);
const value = target.from;
reporter.invalidRelationWithinClause({
typeName,
value: target.from,
validRelations: relationsPerType,
clause: value,
reverse: true,
lineIndex,
});
}
};

relationDef.definition.targets?.forEach((target) => {
validateTargetRelation(typeName, relationName, target);
});

// check the but not
if (relationDef.definition.base) {
validateTargetRelation(typeName, relationName, relationDef.definition.base);
}
if (relationDef.definition.diff) {
validateTargetRelation(typeName, relationName, relationDef.definition.diff);
}
});
});
} catch (e: any) {
if (!_.isUndefined(e.offset)) {
const line = Number.parseInt(e.message.match(/line\s([0-9]*)\scol\s([0-9]*)/m)[1]);
const column = Number.parseInt(e.message.match(/line\s([0-9]*)\scol\s([0-9]*)/m)[2]);

const marker = {
// monaco.MarkerSeverity.Error,
severity: 8,
startColumn: column - 1,
endColumn: lines[line - 1].length,
startLineNumber: column === 0 ? line - 1 : line,
endLineNumber: column === 0 ? line - 1 : line,
message: "Invalid syntax",
source: "linter",
};

markers.push(marker);
if (typeof e.offset !== "undefined") {
try {
let line = Number.parseInt(e.message.match(/line\s([0-9]*)\scol\s([0-9]*)/m)[1]);
const column = Number.parseInt(e.message.match(/line\s([0-9]*)\scol\s([0-9]*)/m)[2]);
line = line <= lines.length ? line : lines.length;

const marker = {
// monaco.MarkerSeverity.Error,
severity: 8,
startColumn: column - 1 < 0 ? 0 : column - 1,
endColumn: lines[line - 1].length,
startLineNumber: column === 0 ? line - 1 : line,
endLineNumber: column === 0 ? line - 1 : line,
message: "Invalid syntax",
source: "linter",
};
markers.push(marker);
} catch (e: any) {
markers.push(defaultError(lines));
}
} else {
const marker = {
// monaco.MarkerSeverity.Error,
severity: 8,
startColumn: 0,
endColumn: Number.MAX_SAFE_INTEGER,
startLineNumber: 0,
endLineNumber: lines.length,
message: "Invalid syntax",
source: "linter",
};

markers.push(marker);
markers.push(defaultError(lines));
}
}

Expand Down
Loading