Skip to content

Commit

Permalink
feat(binding): support double quoted string in query language
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Nov 9, 2023
1 parent 3822f5a commit f5cc6c7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build/api/binding.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,8 @@ export namespace Parser {
export type EntryPoint = keyof ParserResult;
// (undocumented)
export interface ParserResult {
// (undocumented)
columnValue: AST.ColumnValue;
// (undocumented)
filter: Filter;
// (undocumented)
Expand Down
1 change: 1 addition & 0 deletions packages/binding/src/queryLanguage/CacheStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class CacheStore {
filter: new LRUCache(100),
orderBy: new LRUCache(50),
taggedMap: new LRUCache(50),
columnValue: new LRUCache(500),
}
}
}
5 changes: 5 additions & 0 deletions packages/binding/src/queryLanguage/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ class Parser extends EmbeddedActionsParser {
return image
.substring(1, image.length - 1)
.replace("\\'", "'")
.replace(`\\"`, `"`)
.replace('\\b', '\b')
.replace('\\f', '\f')
.replace('\\n', '\n')
Expand Down Expand Up @@ -863,6 +864,9 @@ class Parser extends EmbeddedActionsParser {
case 'taggedMap':
expression = Parser.parser.taggedMap()
break
case 'columnValue':
expression = Parser.parser.columnValue()
break
default:
throw new QueryLanguageError(`Not implemented entry point '${entry}'`)
}
Expand Down Expand Up @@ -903,6 +907,7 @@ namespace Parser {
filter: Filter // E.g. [author.son.age < 123]
orderBy: OrderBy // E.g. items.order asc, items.content.name asc
taggedMap: ParsedTaggedMap // E.g editUser(id: $entity.id, foo: 'bar')
columnValue: AST.ColumnValue
}

export type EntryPoint = keyof ParserResult
Expand Down
2 changes: 1 addition & 1 deletion packages/binding/src/queryLanguage/tokenList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const tokens = {

StringLiteral: createToken({
name: 'StringLiteral',
pattern: /'(:?[^\\']|\\(:?[bfnrtv'\\/]|u[0-9a-fA-F]{4}))*'/,
pattern: /'(:?[^\\']|\\(:?[bfnrtv"'\\/]|u[0-9a-fA-F]{4}))*'|"(:?[^\\"]|\\(:?[bfnrtv"'\\/]|u[0-9a-fA-F]{4}))*"/,
}),

LeftParenthesis: createToken({
Expand Down
17 changes: 17 additions & 0 deletions packages/binding/tests/cases/unit/queryLanguage/general.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import { Environment } from '../../../../src/dao'
import { Parser } from '../../../../src/queryLanguage'

describe('query language parser', () => {
it('shoud parse column value', () => {
const env = Environment.create()
expect(Parser.parseQueryLanguageExpression('123', 'columnValue', env)).toEqual(123)
expect(Parser.parseQueryLanguageExpression('123.456', 'columnValue', env)).toEqual(123.456)
expect(Parser.parseQueryLanguageExpression('true', 'columnValue', env)).toEqual(true)
expect(Parser.parseQueryLanguageExpression('false', 'columnValue', env)).toEqual(false)
expect(Parser.parseQueryLanguageExpression('null', 'columnValue', env)).toEqual(null)
expect(Parser.parseQueryLanguageExpression("'foo'", 'columnValue', env)).toEqual('foo')
expect(Parser.parseQueryLanguageExpression(`'foo\\'bar'`, 'columnValue', env)).toEqual("foo'bar")
expect(Parser.parseQueryLanguageExpression(`'foo"bar'`, 'columnValue', env)).toEqual(`foo"bar`)
expect(Parser.parseQueryLanguageExpression(`'foo\\"bar'`, 'columnValue', env)).toEqual(`foo"bar`)
expect(Parser.parseQueryLanguageExpression('"foo"', 'columnValue', env)).toEqual('foo')
expect(Parser.parseQueryLanguageExpression(`"foo\\"bar"`, 'columnValue', env)).toEqual('foo"bar')
expect(Parser.parseQueryLanguageExpression(`"foo\\'bar"`, 'columnValue', env)).toEqual("foo'bar")
expect(Parser.parseQueryLanguageExpression(`"foo'bar"`, 'columnValue', env)).toEqual("foo'bar")
})

it('should resolve variables adhering to the principle maximal munch', () => {
const environment = Environment.create().withVariables({
ab: 456,
Expand Down

0 comments on commit f5cc6c7

Please sign in to comment.