birchb1024 / yamp

Yet Another Macro Processor - for YAML - Superseded by Goyamp
GNU General Public License v3.0
13 stars 3 forks source link

Can you expand macros in macro values? #13

Closed daykin closed 4 years ago

daykin commented 4 years ago

Let's say I want to do something like this:

defmacro:
  name: foo
  args: [fooarg1, fooarg2, fooarg3]
  value:
      "{{fooarg1}}_{{fooarg2}}_{{fooarg3}}":
        - something: "cool"
          somethingelse: "neat"
---
defmacro:
  name: bar
  args: [arg1, arg2, newArg]
  value:
      "{{arg1}}_{{arg2}}":
        - thisistoplevel: "OK"
        - {foo: {fooarg1: {{arg1}}, fooarg2: {{arg2}}, fooarg3: nested}}

---
- { bar: {arg1: bogus, arg2: example}}
---
#Desired output:
#- bogus_example:
#  - thisistoplevel: OK
#  - bogus_example_nested:
#    - something: cool
#      somethingelse: neat

I get an error attempting this, presumably because it doesn't actually perform macro expansion in the nested invocation:

ERROR: /home/evan/git/alarm_rf/drawer.yaml
<class 'yaml.constructor.ConstructorError'>
while constructing a mapping
  in "/home/evan/git/alarm_rf/drawer.yaml", line 44, column 33
found unacceptable key (unhashable type: 'dict')
  in "/home/evan/git/alarm_rf/drawer.yaml", line 44, column 34

Is this possible? I see a blurb about 'nesting' here but maybe that's a different use case than what I'm imagining.

birchb1024 commented 4 years ago

This input is not valid YAM!. You need some quotes.

Here's the fixed file below.

defmacro:
  name: foo
  args: [fooarg1, fooarg2, fooarg3]
  value:
      "{{fooarg1}}_{{fooarg2}}_{{fooarg3}}":
        - something: "cool"
          somethingelse: "neat"
---
defmacro:
  name: bar
  args: [arg1, arg2, newArg]
  value:
      "{{arg1}}_{{arg2}}":
        - thisistoplevel: "OK"
        - {foo: {fooarg1: "{{arg1}}", fooarg2: "{{arg2}}", fooarg3: nested}}

---
- { bar: {arg1: bogus, arg2: example, newArg: 1}}

Here's output

- bogus_example:
  - thisistoplevel: OK
  - bogus_example_nested:
    - something: cool
      somethingelse: neat

Please try the newest incarnation of this program here: goyamp It's faster and better :-) and runs the yamp code unchanged.