Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to pass a full convert() function definition #355

Closed
simonw opened this issue Dec 10, 2021 · 4 comments
Closed

Allow users to pass a full convert() function definition #355

simonw opened this issue Dec 10, 2021 · 4 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Dec 10, 2021

I think the fix for this is to change the rules about what code is accepted in both the - mode and the literal code string mode: you can pass in a Python expression, OR a fragment that gets turned into a function, OR code that implements its own def convert(value) function. So this would work too:

sqlite-utils convert my.db mytable col1 '
def convert(value):
    return value.upper()
'

Originally posted by @simonw in #353 (comment)

@simonw
Copy link
Owner Author

simonw commented Dec 11, 2021

Relevant code:

# If single line and no 'return', add the return
if "\n" not in code and not code.strip().startswith("return "):
code = "return {}".format(code)
where_args = dict(param) if param else []
# Compile the code into a function body called fn(value)
new_code = ["def fn(value):"]
for line in code.split("\n"):
new_code.append(" {}".format(line))

One way to implement this would be to look to see if the code starts with def ... - but that's not going to work for proper module that start with a docstring or imports.

@simonw
Copy link
Owner Author

simonw commented Dec 11, 2021

Maybe attempt to compile their code, and if it fails try again after adding def fn(value): to the start?

@simonw
Copy link
Owner Author

simonw commented Dec 11, 2021

Ideally I'd like to show the perfect syntax error messages to the user - but I don't know if it's possible to do this cleanly because the error might occur with their originally entered code OR it might occur after I add def fn(value) to it.

I'm going to punt on that for the moment and tolerate slightly confusing syntax errors.

@simonw simonw closed this as completed in 500a35a Dec 11, 2021
@simonw
Copy link
Owner Author

simonw commented Dec 11, 2021

Here's an example of the new (slightly confusing) error message:

sqlite-utils convert fixtures.db roadside_attractions name '
def foo(value)  
  bar
  baz
'
Error: Syntax error in code:

    def foo(value)

invalid syntax

Another:

sqlite-utils convert fixtures.db roadside_attractions name '$'
Error: Syntax error in code:

    return $

invalid syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant