Skip to content

feat(template): make join template function more flexible#5233

Open
TheMeier wants to merge 1 commit into
prometheus:mainfrom
TheMeier:list_join
Open

feat(template): make join template function more flexible#5233
TheMeier wants to merge 1 commit into
prometheus:mainfrom
TheMeier:list_join

Conversation

@TheMeier
Copy link
Copy Markdown
Contributor

@TheMeier TheMeier commented May 10, 2026

Pull Request Checklist

Please check all the applicable boxes.

Which user-facing changes does this PR introduce?

[ENHANCEMENT] TEMPLATE: allow `join` template function to receive slices/arrays of any type

Summary by CodeRabbit

  • New Features

    • More flexible join behavior: accepts varied input types, handles empty/invalid inputs gracefully, and surfaces errors for unsupported inputs.
  • Documentation

    • Updated template function reference to reflect new join behavior.
  • Tests

    • Expanded coverage, including mixed-type inputs and concurrency/thread-safety scenarios.

Review Change Stack

@TheMeier TheMeier requested a review from a team as a code owner May 10, 2026 09:48
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 0c970be4-5bc1-4a91-91fe-1ef62c187ba7

📥 Commits

Reviewing files that changed from the base of the PR and between 9d60bb7 and 4a59b4b.

📒 Files selected for processing (3)
  • docs/notifications.md
  • template/template.go
  • template/template_test.go
✅ Files skipped from review due to trivial changes (1)
  • docs/notifications.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • template/template_test.go

📝 Walkthrough

Walkthrough

The join template function was changed to accept any and use reflection to handle slice/array inputs (including mixed types), returning (string, error). Documentation and tests were updated to reflect and verify the new behavior, including concurrent use.

Changes

Join Function Reflection Support

Layer / File(s) Summary
Core Implementation
template/template.go
DefaultFuncs["join"] now has signature func(sep string, v any) (string, error). It returns ("", nil) for invalid values, uses strings.Join for []string, iterates other slice/array types converting elements with fmt.Sprint, and errors for non-slice/array inputs.
Documentation Update
docs/notifications.md
Strings functions table updated: join second parameter documented as any instead of []string to match implementation.
Test Coverage
template/template_test.go
Added TestTemplateExpansion cases for piping list into join (including mixed-type list) and a concurrent TestTemplateFuncs case to exercise thread-safety with join and list.

Sequence Diagram(s)

sequenceDiagram
  participant TemplateCaller
  participant JoinFunc as join(sep, v)
  participant Reflect as reflect
  participant StringsJoin as strings.Join
  participant FmtSprint as fmt.Sprint

  TemplateCaller->>JoinFunc: invoke with sep + v
  JoinFunc->>Reflect: inspect v (isValid, kind)
  alt v is invalid
    JoinFunc-->>TemplateCaller: return ("", nil)
  else v is []string
    JoinFunc->>StringsJoin: strings.Join(v, sep)
    StringsJoin-->>JoinFunc: concatenated string
    JoinFunc-->>TemplateCaller: return (result, nil)
  else v is slice/array
    JoinFunc->>Reflect: iterate elements
    Reflect->>FmtSprint: convert each element
    FmtSprint-->>JoinFunc: element strings
    JoinFunc->>JoinFunc: join elements with sep
    JoinFunc-->>TemplateCaller: return (result, nil)
  else
    JoinFunc-->>TemplateCaller: return ("", error)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: making the join template function more flexible to accept different input types.
Description check ✅ Passed The description addresses key checklist items for a new feature: tests added, documentation updated, commits signed-off, and a clear release note provided.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/notifications.md`:
- Line 92: Update the docs for the join filter to explicitly state coercion
behavior: clarify that join (described as "join") accepts elements of type any
and will convert non-string elements to their string representation (e.g. via
standard stringification like fmt.Sprint) before concatenation, and that it does
not return an error on conversion (unless the implementation documents
otherwise); reference the existing strings.Join mention for string-only behavior
and note the argument order inversion for templates.

In `@template/template_test.go`:
- Around line 355-359: Add a test case in template_test.go that exercises join
with non-string elements to ensure elements are stringified: add a case similar
to the existing "Template using join with list" entry but with in set to `{{
list 1 true "x" | join "," }}` and exp set to `1,true,x`; place it alongside the
other cases in the same test table so the join behavior for mixed-type lists is
enforced when the test runner iterates over the cases.

In `@template/template.go`:
- Around line 201-204: The join implementation currently enforces elements be
strings by asserting elem.(string) and returning an error; change it to accept
any element type by removing the string type assertion and instead convert each
elem to a string (e.g. via fmt.Sprint or fmt.Sprintf("%v", elem)) before
concatenation so join accepts slices/arrays of any element type; update the code
in the join function where the variable elem is handled to perform this
conversion and remove the error path that checks for non-string types.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: c597f3de-18ea-4227-9cb6-c49c470ec3fa

📥 Commits

Reviewing files that changed from the base of the PR and between 369a175 and 9d60bb7.

📒 Files selected for processing (3)
  • docs/notifications.md
  • template/template.go
  • template/template_test.go

Comment thread docs/notifications.md
| dict | values ...any | Returns a map of string to any, constructed from the variadic list of key-value pairs. The number of arguments must be even, and the keys must be strings. |
| humanizeDuration | number or string | Returns a human-readable string representing the duration, and the error if it happened. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| join | sep string, any | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of the provided slice to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Document coercion behavior explicitly for join

Line 92 updates the argument type to any, but the description should explicitly say non-string elements are converted to strings before joining (and when errors occur, if any). That avoids ambiguous API expectations.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/notifications.md` at line 92, Update the docs for the join filter to
explicitly state coercion behavior: clarify that join (described as "join")
accepts elements of type any and will convert non-string elements to their
string representation (e.g. via standard stringification like fmt.Sprint) before
concatenation, and that it does not return an error on conversion (unless the
implementation documents otherwise); reference the existing strings.Join mention
for string-only behavior and note the argument order inversion for templates.

Comment thread template/template_test.go
Comment thread template/template.go Outdated
Allow `join` template function to accept a slice of any type, not just
strings. The function will convert non-string elements to their string
representation before joining them. This provides more flexibility in
templating, e.g. creating slices with `list` and then joining them.

Signed-off-by: Christoph Maser <christoph.maser+github@gmail.com>
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.

1 participant