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

Use Jinja/Curly brackets inside YAML configs #74

Open
kingo55 opened this issue Jun 2, 2020 · 7 comments
Open

Use Jinja/Curly brackets inside YAML configs #74

kingo55 opened this issue Jun 2, 2020 · 7 comments
Labels
enhancement New feature or request

Comments

@kingo55
Copy link
Member

kingo55 commented Jun 2, 2020

The modular builder could be more expressive if we used curly brackets to inject JS/CSS etc than if we hard-coded this logic into specific fields:

https://jinja.palletsprojects.com/en/2.11.x/templates/

It works quite well in Jinja, dbt, Home Assistant and other projects, so it might work well here too. Rather than:

state: staging
sampleRate: 0
id: w474
name: Some test name
css: shared.css
recipes:
  '0':
    name: Control
  '1':
    name: Treatment1
    js: 1.js
trigger: trigger.js

We could have something like:

state: staging
sampleRate: 0
id: w474
name: Some test name
css: {{ refCss('some-file.css') }}
recipes:
  '0':
    name: Control
  '1':
    name: Treatment1
    js: {{ ref('1.js') }}
trigger: {{ ref('trigger.js') }}

This would allow us to support new variable types (e.g. JSON) or fields as they come available e.g. custom functions passed into the option keys, like decisionAdapter, storageAdapter and whatnot.

This would change how we write all experiments' YAML files though, so perhaps we need to think this through properly and map out migration.

@kingo55 kingo55 added the enhancement New feature or request label Jun 2, 2020
@allmywant
Copy link
Member

@kingo55
"curly brackets to inject JS/CSS" looks very useful, the only problem is: currently we use CleanCSS to minify the css before injection, not sure if we can do it using Jinja templates.

@kingo55
Copy link
Member Author

kingo55 commented Jun 2, 2020

Could we parse the contents of the curly brackets into a JS function?

e.g. {{ injectCss('some.css') }}

And the macro reads/parses/minifies the CSS?

@allmywant
Copy link
Member

@kingo55
It looks like we can write the template like this:

state: staging
sampleRate: 0
id: w474
name: Some test name
css: injectCss('{{ 'some-file.css' }}')
recipes:
  '0':
    name: Control
  '1':
    name: Treatment1
    js: ref('{{ '1.js' }}')
trigger: ref('{{ 'trigger.js' }}')

Then Jinja outputs:
css: injectCss('some-file.css')
js: ref('1.js')

Then we can call injectCss function in modular builder to read and minify the CSS files.

@kingo55
Copy link
Member Author

kingo55 commented Jun 11, 2020

That's good to know @allmywant - could users run any arbitrary code in here?

@allmywant
Copy link
Member

@kingo55
It could be, we can limit the the functions in curly brackets must be the ones we supported and throw error if the function name is unknown.

@kingo55
Copy link
Member Author

kingo55 commented Jun 12, 2020

@allmywant - it could be a feature because there's other easier places to run arbitrary code...

This might just enable some unique features we're not aware of just yet. E.g. Fetching a dynamic sampleRate key during ModularBuild, so we don't have to pollute the git history with daily/hourly sampleRate key changes.

@allmywant
Copy link
Member

@kingo55
Good idea! I think we can do it like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants