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
53 stars 2 forks source link

Loop Variable Update #234

Open robcxyz opened 10 months ago

robcxyz commented 10 months ago

Loop Variable Update

Improve the loop parsing logic to allow declaring variables, ie i in a_list.

Overview

Currently the loop logic inserts temporary variables item and index into the context so that they are available for rendering but this results in overlapping variables in embedded loops. What would be better is if we supported inserting the variable as part of the for key with var_name in a_list and similar semantics. Would require overhauling the parser some custom tokenizer.

Examples

Current - Still Supported


bars:

  - baz

  - bing

foo:

  ->: print {{item}}

  for: bars  

Future

Via Macros

Compact expression pre macro


bars:

  - baz

  - bing

foo:

  ->: print {{bar}}

  for: bar in bars  

Expanded post macro with new keys


bars:

  - baz

  - bing

foo:

  ->: print {{bar}}

  for:

    item: bar

    items:

      ... # Some list or string reference to a list

Pros:

Cons:

Regex Parsing

Pseudo Code


def do(for_string: str):

    split_for = for_string.split(' in ')

    if len(split_for) == 2:

        for_var = split_for[0]

        # Put that var in tmp context

        loop_items = split_for[1]

    elif len(split_for) == 1:

        for_var = 'item'

        loop_items = split_for[0]

    else:

        raise

    # Then do normal stuff

Pros:

Cons:


a:

  b: 1

  c: 2

  d: 3

e->: var {{item}} --for k, v in a

e:

  b: 1

  c: 2

  d: 3