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

add byMeta function #1237

Closed
wants to merge 2 commits into from
Closed

add byMeta function #1237

wants to merge 2 commits into from

Conversation

bofm
Copy link
Contributor

@bofm bofm commented Jul 21, 2019

byMeta

Takes a list of services returned by the service or
services and returns a map that groups services by ServiceMeta values.
Multiple service meta keys can be passed as a comma separated string. |int can be added to
a meta key to convert numbers from service meta values to padded numbers in printf "%05d" % value format (useful for sorting as Go Template sorts maps by keys).

Example:

If we have the following services registered in Consul:

{
  "Services": [
     {
       "ID": "redis-dev-1",
       "Name": "redis",
       "ServiceMeta": {
         "environment": "dev",
         "shard_number": "1"
       },
       ...
     },
     {
       "ID": "redis-prod-1",
       "Name": "redis",
       "ServiceMeta": {
         "environment": "prod",
         "shard_number": "1"
       },
       ...
     },
     {
       "ID": "redis-prod-2",
       "Name": "redis",
       "ServiceMeta": {
         "environment": "prod",
         "shard_number": "2",
       },
       ...
     }
   ]
}
{{ service "redis|any" | byMeta "environment,shard_number|int" | toJson }}

The code above will produce a map of services grouped by meta:

{
  "dev_00001": [
    {
      "ID": "redis-dev-1",
      ...
    }
  ],
  "prod_00001": [
    {
      "ID": "redis-prod-1",
      ...
    }
  ],
  "prod_00002": [
    {
      "ID": "redis-prod-2",
      ...
    }
  ]
}

@eikenb
Copy link
Contributor

eikenb commented Aug 21, 2019

Thanks for the PR @bofm.

I'll happily accept it with one caveat... Would you mind adding a simple set of tests for this? An executable version of your example would be enough. Just something to exercise the code a bit and demonstrate its API in use.

I know as is the functions don't really have tests, but I'd really like to start addressing that. There is one example in place that you can use for some guidance.

Thanks.

@bofm
Copy link
Contributor Author

bofm commented Aug 21, 2019

Added test. Rebased onto master.

@eikenb
Copy link
Contributor

eikenb commented Aug 21, 2019

Great. Thanks.

@eikenb eikenb added this to the 0.22.0 milestone Aug 21, 2019
@eikenb eikenb closed this in 2734e51 Sep 10, 2019
@bofm bofm deleted the byMeta branch October 28, 2019 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants