Skip to content

Commit

Permalink
Merge pull request #88 from gisce/domain-context-helpers
Browse files Browse the repository at this point in the history
Add helpers to domain and context
  • Loading branch information
mguellsegarra authored Sep 6, 2023
2 parents 6baef8b + 542fa61 commit e7408dc
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gisce/ooui",
"version": "0.22.2",
"version": "0.22.3",
"main": "./dist/ooui.umd.js",
"module": "./dist/ooui.es.js",
"types": "./dist/index.d.ts",
Expand Down
55 changes: 55 additions & 0 deletions src/helpers/contextParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,61 @@ export const parseContext = ({
}
};

export const parseContextFields = (context:string): string[] => {
const fields: string[] = [];
try {
if (!context) return fields;

if (isObject(context)) {
return fields;
}

if (typeof context !== "string") {
return fields;
}

const parsedContextInJson = tryParseJSON(context);
if (parsedContextInJson !== null) {
return [];
}

if (context.trim().length === 0) {
return fields;
}

// TODO: maybe this can be accomplished more performant and elegant with regex
const singleQuotesReplace = context.replace(/\"/g, "'");
const strNoWhitespaces = singleQuotesReplace.replace(/\s/g, "");
var replaceTrue = strNoWhitespaces.replace(/True/g, "true");
var replaceFalse = replaceTrue.replace(/False/g, "false");
const strNoClauLeft = replaceFalse.replace(/\{/g, "");
const strNoClauRight = strNoClauLeft.replace(/\}/g, "");

const entryValues = strNoClauRight.split(",");
const valuesSplitted = entryValues.map((entry) => {
return entry.split(":");
});

const parsedContext: any = {};

valuesSplitted.forEach((entry) => {
const fieldName = entry[1];

if (
entry[1].indexOf("'") === -1 &&
entry[1] !== "true" &&
entry[1] !== "false"
) {
fields.push(entry[1].replace(/'/g, ""))
}
});

return fields;
} catch (e) {
}
return fields;
}

function tryParseJSON(str: string): any | null {
try {
const parsedJSON = JSON.parse(str.replace(/'/g, '"'));
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/domainParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ const transformDomainForChildWidget = ({
return transformedDomain;
};

export const parseDomainFields = (domain: string | boolean): string[] => {
if (typeof domain != 'string') {
return [];
}
return domain.replace(/[()\[\]]/g, "")
.split(',')
.map(i => i.trim())
.filter(i => i.indexOf("'") === -1)
}

export { transformDomainForChildWidget };
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import ButtonGroup from "./ButtonGroup";
import Reference from "./Reference";
import Binary from "./Binary";
import Image from "./Image";
import { parseContext } from "./helpers/contextParser";
import { transformDomainForChildWidget } from "./helpers/domainParser";
import { parseContext, parseContextFields } from "./helpers/contextParser";
import { transformDomainForChildWidget, parseDomainFields } from "./helpers/domainParser";
import Timeline from "./Timeline";
import Indicator from "./Indicator";
import Dashboard from "./Dashboard";
Expand Down Expand Up @@ -96,7 +96,9 @@ export {
Binary,
Image,
parseContext,
parseContextFields,
transformDomainForChildWidget,
parseDomainFields,
Timeline,
Indicator,
Dashboard,
Expand Down
22 changes: 19 additions & 3 deletions src/spec/contextParser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseContext } from "../helpers/contextParser";
import { parseContext, parseContextFields } from "../helpers/contextParser";

describe("A Context Parser", () => {
describe("in parseContext method", () => {
Expand Down Expand Up @@ -129,10 +129,26 @@ describe("A Context Parser", () => {
expect(parsedContext!["person"].name).toBe("John Doe");
expect(JSON.stringify(parsedContext)).toBe(JSON.stringify(ctx));
});

it.only("should parse context with simple single quotes in a string", () => {
it("should parse context with simple single quotes in a string", () => {
const string = "{'contract_id': 1, 'contract_ids': [1, 3]}";
const parsedContext = parseContext({ context: string });
expect(parsedContext!["contract_id"]).toBe(1);
});
describe("Getting evaluable fields from context", () => {
test("If not context is empty evaluable fields should be empty", () => {
const context: string = "{'power': potencia, 'tarifa_id': tarifa, 'o2m': tensio_o2m, 'tensio_id': tensio_normalitzada, 'model': 'giscedata.polissa', 'field': 'potencia'}";
const fields = parseContextFields(context);
expect(fields).toEqual([
'potencia',
'tarifa',
'tensio_o2m',
'tensio_normalitzada'
])
});
test("If context is all json an empty list of fields must be returned", () => {
const context: string = "{'from_model': 'res.partner'}";
const fields = parseContextFields(context);
expect(fields).toEqual([])
});
});
});
13 changes: 12 additions & 1 deletion src/spec/domainParser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformDomainForChildWidget } from "../helpers/domainParser";
import { transformDomainForChildWidget, parseDomainFields } from "../helpers/domainParser";

describe("A Domain Parser", () => {
it("should properly transform domain for inner widgets", () => {
Expand Down Expand Up @@ -38,3 +38,14 @@ describe("A Domain Parser", () => {
expect(domainForStageId![0][2]).toBe("backlog");
});
});

describe("Getting evaluable fields for context", () => {
test("Should return evaluable fields", () => {
const domain: string = "['|', ('a', '=', 'foo'), ('niu', '=', nau), ('state', 'in', ('open', 'draft'))]";
const fields = parseDomainFields(domain);
expect(fields).toEqual(['nau'])
})
test("If domain is false fields must be an empty list", () => {
expect(parseDomainFields(false)).toEqual([])
})
});

0 comments on commit e7408dc

Please sign in to comment.