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

Templating and Bind/Export documentation example should show uses of #each's @first and @last #3805

Closed
burtlo opened this Issue Oct 17, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@burtlo
Contributor

burtlo commented Oct 17, 2017

When templatizing configuration that iterates over a collection it becomes important to know how to optionally include delimiters with @last.

server {
    enabled = true
    # Nodes to expect before bootstrapping cluster
    bootstrap_expect = {{cfg.nomad.bootstrap_expect}}
    retry_join = [{{#eachAlive bind.servers.members as |member|}}"{{member.sys.ip}}"{{#unless @last}}, {{/unless}} {{/eachAlive}}]
    retry_interval = "30s"
    retry_max = 0 
}

In the above example the ip addresses of the alive members returned by the servers binding is comma-separated. The logic check {{#unless @last}}, {{/unless}} at the end ensures that the comma is written after each element except the last element.

When configuration only expects a single entry for a member, like a single database source, it is useful to use @first.

{
  "mongo": {
    {{~#eachAlive bind.database.members as |member|}}
      {{#if @first}}
      "host" : "{{member.sys.ip}}",
      "port"   : "{{member.cfg.port}}"
      {{/if}}
    {{~/eachAlive}}
    }
}

In the above example there is only one mongo database within the service group bound to this `database binding. JSON data does not support repetition of the same keys so allowing multiple entries would cause the application to fail to launch. This solution above opts to select the first member in the collection. It could have also been the last member.

@burtlo

This comment has been minimized.

Contributor

burtlo commented Oct 17, 2017

@davidwrede this one's for you!

@eeyun eeyun added this to the Accepted Minor milestone Oct 17, 2017

@davidwrede davidwrede self-assigned this Oct 17, 2017

@davidwrede

This comment has been minimized.

Collaborator

davidwrede commented Oct 17, 2017

Thanks @burtlo!

@chefsalim chefsalim added this to Ready For Analysis in Core Engineering Oct 27, 2017

@davidwrede davidwrede moved this from Ready For Analysis to In Analysis in Core Engineering Nov 21, 2017

@davidwrede davidwrede moved this from In Analysis to In Development in Core Engineering Nov 22, 2017

@davidwrede davidwrede moved this from In Development to Ready for Review in Core Engineering Nov 22, 2017

@chefsalim chefsalim moved this from Ready for Review to In Acceptance in Core Engineering Nov 28, 2017

@davidwrede davidwrede closed this Nov 29, 2017

@tashimi tashimi removed this from In Acceptance in Core Engineering Dec 18, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment