sanic-org / sanic-routing

Internal handler routing for Sanic beginning with v21.3.
https://sanicframework.org/
MIT License
13 stars 11 forks source link

Context managed code generation #42

Closed ahopkins closed 1 year ago

ahopkins commented 3 years ago

Re code generation: maybe take that a step forward and use actual inline Python code instead of string literals. Code that is syntactically valid can be loaded even if the variables don't exist, provided that the particular path never gets executed. Annotations or some other hacks could be used to inject parts into it. Well, you get the idea but this is becoming some true black magic by now.

# This is router code
if need_foo:
    with codegen:
        # This is generated code
        if route == "foo":
            return handler_foo

Originally posted by @Tronic in https://github.com/sanic-org/sanic-routing/issues/37#issuecomment-851521674