Brightspace / serverless-plugin-for-each

Serverless plugin that adds $forEach syntax to reduce code duplication and allow creating dynamic templates
Apache License 2.0
1 stars 5 forks source link

ForEach in forEach #61

Closed throrin19 closed 1 year ago

throrin19 commented 1 year ago

Hello, Is it possible to made a $forEach inside another $forEach ?

In my case I have this configuration in my custom part :

custom
  foo:
    # Content of barOne and barTwo are retrived from ssm/secret-manager
    # barOne: ${ssm:/aws/reference/secretsmanager/bar-one}
    # barTwo: ${ssm:/aws/reference/secretsmanager/bar-two}
    barOne:
        DB_PASSWORD_BAR_ONE: value
        DB_HOST_BAR_ONE: value
    barTwo:
        DB_PASSWORD_BAR_TWO: value
        DB_HOST_BAR_TWO: value

And I want made this in provider.environment :

provider:
  environment:
    DB_PASSWORD_BAR_ONE: value
    DB_HOST_BAR_ONE: value
    DB_PASSWORD_BAR_TWO: value
    DB_HOST_BAR_TWO: value

I've try this with $forEach plugin :

provider:
  environment:
    $forEach:
      iterator: ${self:custom.foo}
      template:
        $forEach:
          iterator: $forEach.value
          template:
            $forEach.key: $forEach.value

But I have this error :

Error: provider.environment.$forEach.template/$forEach/iterator must be array, provider.environment.$forEach.template/$forEach/iterator must be object, provider.environment.$forEach.template/$forEach/iterator must match exactly one schema in oneOf
AntonBazhal commented 1 year ago

I don't think this is currently possible. Nesting $forEach is supported, but the plugin does a depth-first traversal to avoid ambiguity.

throrin19 commented 1 year ago

There is no solution to made this, or an example for nesting $forEach ?

throrin19 commented 1 year ago

I try to remove ambiguity with $forEach_named but the problem is the same. This plugin does not respond to this use case....

AntonBazhal commented 1 year ago

As I mentioned above, the plugin supports nesting, but it interpolates nested $forEach before the parent one. There are two tests that show how this works for objects and arrays.

throrin19 commented 1 year ago

I resolve my problem using @cruglobal/serverless-merge-config with this plugin :

custom
  foo:
    $<<:
      - DB_PASSWORD_BAR_ONE: value
        DB_HOST_BAR_ONE: value
      - DB_PASSWORD_BAR_TWO: value
        DB_HOST_BAR_TWO: value

provider:
  environment:
    $forEach:
      iterator: ${self:custom.foo}
      template:
        $forEach.key: $forEach.value

plugins:
  - '@cruglobal/serverless-merge-config'
  - serverless-plugin-for-each