con2 / emrichen

A Template engine for YAML & JSON
MIT License
107 stars 11 forks source link

Co-operate with formats that use tags of their own such as CloudFormation #11

Open japsu opened 6 years ago

japsu commented 6 years ago

CloudFormation has its own functions such as !Subst and !Join etc. In order to be able to input and output CloudFormation templates, Emrichen needs a way to co-operate with foreign tags.

(Bad) options:

  1. Keep disallowing foreign tags and require CloudFormation users to use the { "Fn::Join": […] } form of CFN functions instead of the tag-based !Join form
  2. Prefix our own tags with some prefix, reducing the risk of collision, and pass foreign tags to the output as-is.
  3. Require foreign tags to be prefixed and strip the prefix upon processing
akx commented 6 years ago
  1. have a per-template pragma sort of thing to determine which tags are handled within emrichen and which not (e.g. !Pragma cloudformation)
japsu commented 3 years ago

@zbyte64 in #53 suggested another use case for this: creating Emrichen templates with Emrichen.

If we were to go by approach 2, it might look like something like this:

Template:

foo: !Tag,FooTagWillBeCopiedVerbatimToOutput 5

Output:

foo: !FooTagWillBeCopiedVerbatimToOutput 5

Here I'm shamelessly reusing the , separator we currently are using for tag composition. tag_name.startswith("Tag,") would have to be special-cased in emrichen.input.yaml:construct_tagged_object.

Implementation could use a Tag internal tag similarly to Compose, but how would it communicate the custom tag to PyYAML? Ad-hoc class that extends YAMLObject and sets tag = "FooTagWillBeCopiedVerbatimToOutput" as class variable?

zbyte64 commented 3 years ago

For my use I actually want to spit back Tags I would otherwise process with emrichen, but as a second stage. Would option 2 look something like this (adapted from my use case)?

 chart:
      profile: "foobar"
      chartConfigUrl:
        !Tag,AsDocument
        filename: ./data_overview.json
        content: !Tag,With
          template: !Tag,Include ./atemplate.template.yaml
          vars:
            path: s3://...../
            columns:
              - application_gap_ratio
              - application_gap

This tweaked my brain at first, but now it seems very satisfying.