Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse templates in collection-type variables #1526

Merged
merged 6 commits into from
Mar 10, 2024

Conversation

pd93
Copy link
Member

@pd93 pd93 commented Mar 3, 2024

Fixes #1477
Fixes #1511

This PR overhauls the way templating works in Task. Instead of having methods on templater.Template, we now have package-level functions instead. This allows us to use generics (as generic methods aren't possible right now) which vastly simplifies the calling API. This is especially useful for the "Any Variables" experiment since many types of variable may need to support templating.

For example, we now have the ability to support collection-type variables (slices/maps) and these might contain strings in which we want to use templates. See example below:

version: '3'

vars:
  IMAGE_TAG: 'latest'

tasks:
  default:
    vars:
      works: '{{.IMAGE_TAG}}'
      does:
        map:
          not:
            work: '{{.IMAGE_TAG}}'
    cmds:
      - 'echo {{.works}}'
      - 'echo {{.does.not.work}}'

Running the above Taskfile in v3.35.0 or lower will result in the following:

> TASK_X_ANY_VARIABLES=2 task
task: [default] echo latest
latest
task: [default] echo {{.IMAGE_TAG}}
{{.IMAGE_TAG}}

Not that the .does.not.work variable contains a template, but it is not parsed. After this PR the result will work as expected:

> TASK_X_ANY_VARIABLES=2 task
task: [default] echo latest
latest
task: [default] echo latest
latest

This is possible because any variable can now be passed into the templater.Replace(...) function and it will automatically traverse the value and find all strings that need replacing, whether they are raw strings, or nested strings inside a map/slice.

It's worth mentioning that I have not actually removed the old templater.Template struct. It is now called templater.Cache (as this is its primary use) and it is passed in as a regular function parameter instead. It's possible that we could make changes to this too, but I figured I'd leave that for another day rather than trying to bundle a bunch of error handling changes in the same PR.

@pd93 pd93 mentioned this pull request Mar 3, 2024
3 tasks
@pd93 pd93 force-pushed the 1511-nested-map-variable-templating branch from e485537 to 3109a89 Compare March 4, 2024 18:28
Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 👏 👏

@pd93 pd93 merged commit 08a888d into main Mar 10, 2024
13 checks passed
@pd93 pd93 deleted the 1511-nested-map-variable-templating branch March 10, 2024 17:11
pd93 added a commit that referenced this pull request Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants