Repository navigation
Variables js support - #2657
Variables js support#2657
Conversation
|
Looks great @ac360 .... simple yet great addition! G2M 😌 |
| In the above example, you're referencing the entire `myCustomFile.yml` file in the `custom` property. You need to pass the path relative to your service directory. You can also request specific properties in that file as shown in the `schedule` property. It's completely recursive and you can go as deep as you want. | ||
|
|
||
| ## Reference Variables in Javascript Files | ||
| To add dynamic data into your variables, reference javascript files by putting `${file(./myFile.js):someModule}` syntax in your `serverless.yml`. This functionality is recursive, so you can go as deep in that file as you want. Here's an example: |
There was a problem hiding this comment.
Could you add or extend the current example to show that you can also reference sub properties, so you can return a hash in the js file and then reference that sub property (same way you did in the test). Other than that great change!
|
@eahefnawy did you test/validate this on your system as well? Imho small docs change that we can do to show how powerful this is, but this should definitely go out with the next release, don't see anything stopping this being merged after a small docs update. |
|
Damn! This is brilliant! ❤️ Tested it according to the steps in the description. Works as describes. Added just a few comments. After that it's GTM from my side. 👍 |
| ` file "${referencedFileRelativePath}" sub properties`, | ||
| ' Please use ":" or "." to reference sub properties.', | ||
| ` file "${referencedFileRelativePath}".`, | ||
| ' Check your javascript is returning the correct data.', |
There was a problem hiding this comment.
Is an if missing here? --> Check if your JavaScript code is returning the correct data.
| const errorMessage = [ | ||
| 'Invalid variable syntax when referencing', | ||
| ` file "${referencedFileRelativePath}" sub properties`, | ||
| ' Please use ":" or "." to reference sub properties.', |
There was a problem hiding this comment.
Is this error message accurate? Because at the top there's a an if which only checks for :.
|
This is awesome! Could we also have access to |
|
@nicka in that case you should be writing a plugin. With the new |
Not sure if I completely agree on this. Even in the provided example: You need some params/arguments to even reach to a return value of Don't get me wrong! I really like this feature and I see it's potential 👍 |
|
Totally can see your point there, but for this case users really should switch to a simple plugin. This is imho really for very simple things like calculated hash value, expanding a template filename to the actual filename, expanding paths, ... so things that are really simple. We might extend it in the future to do more, but imho it would overlap a lot with the plugin system here and I don't want to duplicate functionality until we really and fully understand what the need is (which at this point at least I'm not 100% certain I have a complete grasp on how what you're proposing would be used and in which situations) |
|
@nicka @flo I thought about what could be passed in, but wanted to think more about this and get feedback before implementation. At the moment, I'm not opposed to overlapping with the Plugin system. If the Here are some potential, powerful use-cases:
I think we should merge this PR without passing anything in, and open another discussion issue for what this could become. |
|
@ac360 agreed. The main concern I have is that we pass something into the variable system and it then starts changing other parts of the config. So imho anything we pass in should be read-only and only help to determine a specific value. And if that isn't easy enough to accomplish through the plugin system then we should make the plugin system simpler. |
Agreed, this feature should not act as a modifier of the original |
What did you implement:
Problem:
Serverless Variables are awesome, but they don't support generating dynamic data.
Use-cases:
serverless.ymlvalue, every time Serverless is run.serverless.yml. But, I need to generate random values in those mocks to make them more realistic and robust. With this, I can.How did you implement it:
Added a few lines of code (and moved existing code around which makes this change look larger than it is) to
Variables.jsto allow for dynamic data via javascript.How can we verify it:
serverless.yml, use this reference:config.jsin your service root, with this code:Todos:
Is this ready for review?: YES