sudoblockio / tackle

Tackle is a programmable configuration language for building modular utilities, code generators, and CLIs with schema validation baked in.
Apache License 2.0
51 stars 2 forks source link

Pre / Post Context #251

Open robcxyz opened 8 months ago

robcxyz commented 8 months ago

Pre / Post Context

Break up the context into pre and post hook parsing groups of data to allow importing hooks

Overview

Right now all context outside of a declarative hook call is called after the declarative hook is executed. This sort of goes against how a typical program would be structured where definitions outside of a function are called by means of importing the module. In tackle, it would be nice if the parsing of that context was optional to have called before a function is imported. Since order of the function and the context makes the most sense to inform what context is parsed before the function is called vs after, this proposal suggests breaking up the parsing into these two sections.

Pros:

Cons:

Examples


pre_context_1: bar

<-:

  foo->: {{pre_context_1}}

method_1<-:

  foo->: {{pre_context_2}}

pre_context_2: bar

method_2<-:

  foo->: {{pre_context_1}}

post_context->: {{foo}}

Would require rearranging some logic about how the functions are parsed so that on one pass we split up the context between pre_context and post_context for the input context. Not too hard to implement.

Implementation

Data objects


def parse_context_2(context: 'Context'):

    """Main entrypoint to parsing a context."""

    # Split the input data so that the pre/post inputs are separated from the hooks

    # TODO: Do on import?

    split_input_data(context=context)

    parse_input(context=context, input=context.data.pre_input)

    parse_input_args_for_hooks(context=context)

    parse_input(context=context, input=context.data.post_input)

Do on import?

Pros:

Cons:

Conclusion -> Don't - Do in one function as above