Skip to content

feat: add key/value pair template functions#756

Open
JamBalaya56562 wants to merge 1 commit into
nginx-proxy:mainfrom
JamBalaya56562:feat/319-kv-template-funcs
Open

feat: add key/value pair template functions#756
JamBalaya56562 wants to merge 1 commit into
nginx-proxy:mainfrom
JamBalaya56562:feat/319-kv-template-funcs

Conversation

@JamBalaya56562

@JamBalaya56562 JamBalaya56562 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds two template helpers originally proposed in #319:

  • splitKeyValuePairs $string $listSep $kvpSep [$defaultKey] — splits a string into a map[string]string. Each item (delimited by $listSep) is split into a key/value pair by $kvpSep. Items that do not contain $kvpSep fall back to $defaultKey as the key (or to the item itself when $defaultKey is empty or omitted).
  • groupByMultiKeyValuePairs $containers $fieldPath $listSep $kvpSep [$defaultKey] — like groupByMulti, but the field value is parsed into key/value pairs by splitKeyValuePairs and the containers are grouped by key.

Motivating example: parsing an env value such as VIRTUAL_PORT="443:3000,3000:3000".

Notes

This reimplements the stale PR #319 against the current internal/ layout, reusing the existing generalizedGroupByKey machinery. Two issues in the original were fixed while porting:

  • It used strings.Split(kvp, kvpSep) and indexed [0]/[1], silently dropping extra segments when the value itself contained the separator. It now uses strings.SplitN(kvp, kvpSep, 2), so a value such as url=http://a=b keeps http://a=b intact.
  • Its test cast entries to the value type RuntimeContainer; the current code stores *RuntimeContainer, so the ported test uses the pointer type.

Tests

Added unit tests for both functions (including a case that exercises the SplitN fix) and documented both in the README.

Reimplements #319

🤖 Generated with Claude Code

@JamBalaya56562 JamBalaya56562 force-pushed the feat/319-kv-template-funcs branch from e95699d to 3c0d803 Compare July 11, 2026 19:59
@buchdag

buchdag commented Jul 12, 2026

Copy link
Copy Markdown
Member

@JamBalaya56562 please flag AI-generated PR explicitly in the PR description (ie "🤖 Generated with Claude Code" like in this PR) and add the AI agent as co-author of the commits.

@JamBalaya56562 JamBalaya56562 force-pushed the feat/319-kv-template-funcs branch from 3c0d803 to eab4ccd Compare July 12, 2026 22:38
@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

Done — added the Co-Authored-By: Claude Opus 4.8 trailer to the commit and the 🤖 Generated with [Claude Code](https://claude.com/claude-code) note to the PR description. I also rebased onto the latest main. Thanks for the review!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds two new template helper functions to parse key/value pair lists from strings and to group containers by the parsed keys, extending docker-gen’s existing groupBy* family for common env patterns like VIRTUAL_PORT="443:3000,3000:3000".

Changes:

  • Added splitKeyValuePairs (string → map[string]string) for parsing delimited key/value lists.
  • Added groupByMultiKeyValuePairs to group containers by keys parsed from a delimited field value.
  • Documented the new helpers in the README and added unit tests covering core behavior (including SplitN value preservation).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
README.md Documents the two new template helpers and their arguments.
internal/template/template.go Registers the new helpers in the template function map.
internal/template/groupby.go Implements splitKeyValuePairs and groupByMultiKeyValuePairs using existing grouping machinery.
internal/template/groupby_test.go Adds unit tests for both new helper functions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/template/groupby.go Outdated
Comment on lines +65 to +71
func groupByMultiKeyValuePairs(entries interface{}, key, listSep, kvpSep, defaultKey string) (map[string][]interface{}, error) {
return generalizedGroupByKey("groupByMultiKeyValuePairs", entries, key, func(groups map[string][]interface{}, value interface{}, v interface{}) {
for k := range splitKeyValuePairs(value.(string), listSep, kvpSep, defaultKey) {
groups[k] = append(groups[k], v)
}
})
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — fixed. groupByMultiKeyValuePairs now takes defaultKey ...string and forwards it as defaultKey..., so the documented optional [$defaultKey] works and a 4-argument template call no longer fails at runtime. Added TestGroupByMultiKeyValuePairsDefaultKeyOmitted to cover the omitted-key case.

Comment on lines +51 to +60
var key, value string
if strings.Contains(kvp, kvpSep) {
parts := strings.SplitN(kvp, kvpSep, 2)
key, value = parts[0], parts[1]
} else if len(defaultKey) == 0 || defaultKey[0] == "" {
key, value = kvp, kvp
} else {
key, value = defaultKey[0], kvp
}
output[key] = value

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — splitKeyValuePairs now uses a single strings.SplitN(kvp, kvpSep, 2) and checks len(parts) == 2, instead of strings.Contains followed by SplitN, so each segment is scanned once.

@JamBalaya56562 JamBalaya56562 force-pushed the feat/319-kv-template-funcs branch from eab4ccd to 1352017 Compare July 13, 2026 09:11
@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

Thanks — applied both Copilot suggestions:

  • Made defaultKey variadic on groupByMultiKeyValuePairs and forward it as defaultKey..., so the documented optional [$defaultKey] actually works (a 4-argument template call no longer fails at runtime). Added a test for the omitted-key case.
  • splitKeyValuePairs now uses a single strings.SplitN + length check instead of strings.Contains followed by SplitN, avoiding the redundant scan.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@JamBalaya56562 JamBalaya56562 force-pushed the feat/319-kv-template-funcs branch from 1352017 to b138587 Compare July 13, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants