Skip to content

Commit

Permalink
Merge pull request #8 from tfso/bugfix/odata-with-nullable-values
Browse files Browse the repository at this point in the history
bugfix/OData Methods using nullable values in repository ROC-913
  • Loading branch information
lostfields authored Jan 20, 2023
2 parents 819b1d6 + 250a483 commit f7f3166
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
31 changes: 25 additions & 6 deletions src/linq/expressions/odatavisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export class ODataVisitor extends ReducerVisitor {
let getParams = (expression: IMethodExpression, ...typeofs: Array<string>) => {
let params: Array<any> = null,
getType = (t) => {
if (typeof t == 'object') {
if(t == null)
return 'undefined'

if (t != null && typeof t == 'object') {
if (t.getTime && t.getTime() >= 0)
return 'date';
}
Expand All @@ -50,7 +53,7 @@ export class ODataVisitor extends ReducerVisitor {
if (parameters.every(expression => expression.type == ExpressionType.Literal) == true) {
params = parameters.map(expr => (<LiteralExpression>expr).value);

if (new RegExp('^' + typeofs.map(t => t.endsWith('?') ? '(' + t.slice(0, -1) + ')?' : t).join(';') + ';?$').test(params.map(p => getType(p)).join(';') + ';') == false)
if (new RegExp('^' + typeofs.map(t => t.endsWith('?') ? `(${t.slice(0, -1)})?` : `(${t})`).join(';') + ';?$').test(params.map(p => getType(p)).join(';') + ';') == false)
throw new TypeError(params.map(p => getType(p)).join(', '));
}
else if ((parameters.length == typeofs.length) == false) {
Expand All @@ -67,26 +70,42 @@ export class ODataVisitor extends ReducerVisitor {
switch (expression.name) {
// String Functions
case 'substringof': // bool substringof(string po, string p1)
if ((params = getParams(expression, 'string', 'string')) != null)
if ((params = getParams(expression, 'string|undefined', 'string')) != null) {
if(params[0] == null)
return new LiteralExpression(false)

return new LiteralExpression(String(params[0]).indexOf(String(params[1])) >= 0);
}

break;

case 'endswith': // bool endswith(string p0, string p1)
if ((params = getParams(expression, 'string', 'string')) != null)
if ((params = getParams(expression, 'string|undefined', 'string')) != null) {
if(params[0] == null)
return new LiteralExpression(false)

return new LiteralExpression(String(params[0]).endsWith(String(params[1])));
}

break;

case 'startswith': // bool startswith(string p0, string p1)
if ((params = getParams(expression, 'string', 'string')) != null)
if ((params = getParams(expression, 'string|undefined', 'string')) != null) {
if(params[0] == null)
return new LiteralExpression(false)

return new LiteralExpression(String(params[0]).startsWith(String(params[1])));
}

break;

case 'contains': // bool contains(string p0, string p1)
if ((params = getParams(expression, 'string', 'string')) != null)
if ((params = getParams(expression, 'string|undefined', 'string')) != null) {
if(params[0] == null)
return new LiteralExpression(false)

return new LiteralExpression(String(params[0]).indexOf(String(params[1])) >= 0);
}

break;
case 'length': // int length(string p0)
Expand Down
11 changes: 10 additions & 1 deletion src/test/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("When using Enumerable", () => {

beforeEach(() => {
cars = [
<ICar>{ id: 1, location: 'SKIEN', registrationYear: 2016, type: { make: 'SAAB', model: '9-3' } },
<ICar>{ id: 1, location: 'SKIEN', registrationYear: 2016, optional: null, type: { make: 'SAAB', model: '9-3' } },
<ICar>{ id: 2, location: 'PORSGRUNN', registrationYear: 2010, optional: 'yes', type: { make: 'NISSAN', model: 'QASHQAI' } },
<ICar>{ id: 3, location: 'PORSGRUNN', registrationYear: 2005, type: { make: 'SAAB', model: '9-3' } },
<ICar>{ id: 4, location: 'LANGESUND', registrationYear: 2004, optional: 'yes', type: { make: 'NISSAN', model: 'LEAF' } },
Expand Down Expand Up @@ -345,6 +345,15 @@ describe("When using Enumerable", () => {

let where = query.operations.first(WhereOperator);
})

it("should be able to use methods on optional values", () => {
let query: Enumerable<ICar> = new Enumerable<ICar>();

query.where("contains(optional, 'es')");

let result = query.toArray(cars);
assert.equal(result.length, 4);
})
})

it("should take top 1", () => {
Expand Down

0 comments on commit f7f3166

Please sign in to comment.