Skip to content

Commit

Permalink
fix: nullable values in models #deploy_branch
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfields committed Feb 29, 2024
1 parent 2670940 commit df7037d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion js-enumerable.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
}
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": false
"source.fixAll.eslint": "never"
},
"eslint.validate": [
"typescript",
Expand Down
47 changes: 37 additions & 10 deletions src/linq/peg/odatavisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export class ODataVisitor extends ReducerVisitor {
let getParams = (expression: IMethodExpression, ...typeofs: Array<string>) => {
let params: Array<any> | undefined,
getType = (t: any) => {
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 @@ -45,7 +48,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 @@ -62,28 +65,44 @@ 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)
if((params = getParams(expression, 'string')) != null)
return new LiteralExpression(String(params[0]).length)
Expand All @@ -109,14 +128,22 @@ export class ODataVisitor extends ReducerVisitor {
break

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

return new LiteralExpression(String(params[0]).toLowerCase())
}

break

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

return new LiteralExpression(String(params[0]).toUpperCase())
}

break

Expand Down
5 changes: 3 additions & 2 deletions src/linq/peg/reducervisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ export class ReducerVisitor extends ExpressionVisitor {
let identifier = (<IIdentifierExpression>expression),
currentScope = Object.assign({}, global ?? {}, scope ?? {})


// this object
if(isRecord(currentScope) && identifier.name in currentScope && (value = currentScope[identifier.name]) !== undefined) {
if(isRecord(currentScope) && identifier.name in currentScope) {
value = currentScope[identifier.name]

if(value == null)
return new LiteralExpression(null)

Expand Down
20 changes: 19 additions & 1 deletion src/test/odatavisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe('When using OData for ExpressionVisitor', () => {
vars = {
number: 5,
stringhavingdate: '2018-05-30Z',
stringthatisnull: '' || null,
stringthatisundefined: '' || undefined,
string: 'abc',
decimal: 5.50,
dateonly: new Date('2020-01-28Z'),
Expand Down Expand Up @@ -89,7 +91,23 @@ describe('When using OData for ExpressionVisitor', () => {

assert.equal(expr.type, Expr.ExpressionType.Literal)
assert.equal((<Expr.LiteralExpression>expr).value, false)
})
})

it('should evaluate a expression with contains where property may be null', () => {
let reduced = reducer.parseOData('contains(stringthatisnull, \'bc\')'),
expr = reducer.evaluate(reduced, vars)

assert.equal(expr.type, Expr.ExpressionType.Literal)
assert.equal((<Expr.LiteralExpression>expr).value, false)
})

it('should evaluate a expression with contains where property may be undefined', () => {
let reduced = reducer.parseOData('contains(stringthatisundefined, \'bc\')'),
expr = reducer.evaluate(reduced, vars)

assert.equal(expr.type, Expr.ExpressionType.Literal)
assert.equal((<Expr.LiteralExpression>expr).value, false)
})

it('should evaluate a expression with date as type (v4)', () => {
let reduced = reducer.parseOData('date ge 2017-05-01Z'),
Expand Down

0 comments on commit df7037d

Please sign in to comment.