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

@TheMeier TheMeier commented May 10, 2026

Copy link
Copy Markdown
Contributor

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

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

coderabbitai Bot commented May 10, 2026

Copy link
Copy Markdown

Review Change Stack

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: 274c0a07-433e-4065-a7a1-b8d8ff88d0be

📥 Commits

Reviewing files that changed from the base of the PR and between 4a59b4b and 78bcdc6.

📒 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 (2)
  • template/template_test.go
  • template/template.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 accurately describes the main change—making the join template function accept flexible input types beyond just strings.
Description check ✅ Passed The description is complete with all required checklist items addressed: new feature with tests, documentation updates, signed commits, and a properly formatted release note.
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.

@coderabbitai coderabbitai Bot 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.

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 9d60bb794842f049a3750ae41670514d7676395d.

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

Comment thread docs/notifications.md Outdated
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