Giter Club home page Giter Club logo

pyrql's People

Contributors

pjwerneck avatar sirex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pyrql's Issues

Make parser() to unparser(), unparser() to parser() idempotent

I would expect a parse action to produce output that could also be unparsed (see examples 1 and 4 specifically). Examples 5 and 6 expectedly fail but are included just for completeness. If 1 and 4 are indeed invalid queries, then the exception should be triggered at the parser().

from pyrql.parser import Parser
from pyrql.unparser import Unparser

rql_string_test_1 = r"eq(accountId,(123456789))&eq(yyyy,(2020))&eq(mm,(10))"
rql_string_test_2 = r"eq(accountId,123456789)&eq(yyyy,2020)&eq(mm,10)"
rql_string_test_3 = r"and(eq(accountId,123456789), eq(yyyy,2020), eq(mm,10))"
rql_string_test_4 = r"and(eq(accountId,(123456789)), eq(yyyy,(2020)), eq(mm,(10)))"
rql_string_test_5 = r"and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))"
rql_string_test_6 = r"eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))"

parser = Parser()
unparser = Unparser()

# Returns ERROR: can only concatenate str (not "int") to str
print(f"RQL test {rql_string_test_1}")
print("Parsing test 1")
try:
    parsed_1 = parser.parse(rql_string_test_1)
    print(parsed_1)
    print("Unparsing test 1")
    try:
        unparsed_1 = unparser.unparse(parsed_1)
        print(unparsed_1)
    except Exception as e:
        print(f"ERROR: {e}")
except Exception as e:
    print(f"ERROR: {e}")
print()
print()

# OUTPUT
# RQL test eq(accountId,(123456789))&eq(yyyy,(2020))&eq(mm,(10))
# Parsing test 1
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', (123456789,)]}, {'name': 'eq', 'args': ['yyyy', (2020,)]}, {'name': 'eq', 'args': ['mm', (10,)]}]}
# Unparsing test 1
# ERROR: can only concatenate str (not "int") to str


# Returns good result
print(f"RQL test {rql_string_test_2}")
print("Parsing test 2")
try:
    parsed_2 = parser.parse(rql_string_test_2)
    print(parsed_2)
    print("Unparsing test 2")
    try:
        unparsed_2 = unparser.unparse(parsed_2)
        print(unparsed_2)
    except Exception as e:
        print(f"ERROR: {e}")
except Exception as e:
    print(f"ERROR: {e}")
print()
print()

# OUTPUT
# RQL test eq(accountId,123456789)&eq(yyyy,2020)&eq(mm,10)
# Parsing test 2
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', 123456789]}, {'name': 'eq', 'args': ['yyyy', 2020]}, {'name': 'eq', 'args': ['mm', 10]}]}
# Unparsing test 2
# and(eq(accountId,123456789),eq(yyyy,2020),eq(mm,10))

# Returns good result
print(f"RQL test {rql_string_test_3}")
print("Parsing test 3")
try:
    parsed_3 = parser.parse(rql_string_test_3)
    print(parsed_3)
    print("Unparsing test 3")
    try:
        unparsed_3 = unparser.unparse(parsed_3)
        print(unparsed_3)
    except Exception as e:
        print(f"ERROR: {e}")
except Exception as e:
    print(f"ERROR: {e}")
print()
print()

# OUTPUT
# RQL test and(eq(accountId,123456789), eq(yyyy,2020), eq(mm,10))
# Parsing test 3
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', 123456789]}, {'name': 'eq', 'args': ['yyyy', 2020]}, {'name': 'eq', 'args': ['mm', 10]}]}
# Unparsing test 3
# and(eq(accountId,123456789),eq(yyyy,2020),eq(mm,10))

# Returns ERROR: can only concatenate str (not "int") to str
print(f"RQL test {rql_string_test_4}")
print("Parsing test 4")
try:
    parsed_4 = parser.parse(rql_string_test_4)
    print(parsed_4)
    print("Unparsing test 4")
    try:
        unparsed_4 = unparser.unparse(parsed_4)
        print(unparsed_4)
    except Exception as e:
        print(f"ERROR: {e}")
except Exception as e:
    print(f"ERROR: {e}")
print()
print()

# OUTPUT
# RQL test and(eq(accountId,(123456789)), eq(yyyy,(2020)), eq(mm,(10)))
# Parsing test 4
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', (123456789,)]}, {'name': 'eq', 'args': ['yyyy', (2020,)]}, {'name': 'eq', 'args': ['mm', (10,)]}]}
# Unparsing test 4
# ERROR: can only concatenate str (not "int") to str

# Returns ERROR: ('and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))', 6, 'Expected ")"')
print(f"RQL test {rql_string_test_5}")
print("Parsing test 5")
try:
    parsed_5 = parser.parse(rql_string_test_5)
    print(parsed_5)
    print("Unparsing test 5")
    try:
        unparsed_5 = unparser.unparse(parsed_5)
        print(unparsed_5)
    except Exception as e:
        print(f"ERROR: {e}")
except Exception as e:
    print(f"ERROR: {e}")
print()
print()

# OUTPUT
# RQL test and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))
# Parsing test 5
# ERROR: ('and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))', 6, 'Expected ")"')

# Returns ERROR: ('eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))', 12, 'Expected ")"')
print(f"RQL test {rql_string_test_6}")
print("Parsing test 6")
try:
    parsed_6 = parser.parse(rql_string_test_6)
    print(parsed_6)
    print("Unparsing test 6")
    try:
        unparsed_6 = unparser.unparse(parsed_6)
        print(unparsed_6)
    except Exception as e:
        print(f"ERROR: {e}")
except Exception as e:
    print(f"ERROR: {e}")
print()
print()

# OUTPUT
# RQL test eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))
# Parsing test 6
# ERROR: ('eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))', 12, 'Expected ")"')

Invalid query doesn't raise exception

This query, structured properly works:

>>> parsed = pyrql.parse("in(foo,(foo,bar))&sort(+foo)&eq(userid,user)")
>>> parsed
{'name': 'and', 'args': [{'name': 'in', 'args': ['foo', ('foo', 'bar')]}, {'name': 'sort', 'args': [('+', 'foo')]}, {'name': 'eq', 'args': ['userid', 'user']}]}

However, if there is an extra paranthesis in the query such as:

"in(foo,(foo,bar))&sort(+foo))&eq(userid,user)"

Instead of throwing an error, it just ignores the rest of the query making it unsafe to use when appending query elements to sanitize user provided queries.

>>> parsed = pyrql.parse("in(foo,(foo,bar))&sort(+foo))&eq(userid,user)")
>>> parsed
{'name': 'and', 'args': [{'name': 'in', 'args': ['foo', ('foo', 'bar')]}, {'name': 'sort', 'args': [('+', 'foo')]}]}

Is there any quick/easy fix for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.