Skip to content

Add pipeline-friendly regex functions with subject-string-last argument order - #484

Open
ricardbejarano wants to merge 1 commit into
Masterminds:masterfrom
bejaratommy:regex-pipeline-friendly-functions
Open

ricardbejarano wants to merge 1 commit into
Masterminds:masterfrom
bejaratommy:regex-pipeline-friendly-functions

Conversation

@ricardbejarano

Copy link
Copy Markdown

Summary

regexFindAll, regexReplaceAll, regexReplaceAllLiteral and regexSplit take the subject string as a middle argument instead of last. Go template pipelines always pass the previous pipeline value as the last argument to the next function, so these functions are effectively unusable in a pipeline — you have to stash the subject in a variable first instead of writing $s | regexReplaceAll "a(x*)b" "${1}W".

This is Masterminds/sprig#86, acknowledged by maintainer @mattfarina:

So, the path forward would be to have some new regex functions, with new names, that have a different ordering. This would be a feature request. PRs are welcome.

Per the discussion, a breaking change to the existing functions' argument order isn't acceptable (existing templates depend on it), so this PR adds new, additive *Global variants instead, leaving the existing functions untouched:

  • regexFindAllGlobal / mustRegexFindAllGlobal
  • regexReplaceAllGlobal / mustRegexReplaceAllGlobal
  • regexReplaceAllLiteralGlobal / mustRegexReplaceAllLiteralGlobal
  • regexSplitGlobal / mustRegexSplitGlobal

Each mirrors its existing counterpart but with the subject string moved to the last argument, e.g.:

{{ $s | regexReplaceAllGlobal "a(x*)b" "${1}W" }}

regexMatch/mustRegexMatch and regexFind/mustRegexFind already take the subject string as the last argument, so they don't need a *Global counterpart.

Changes

  • regex.go: new *Global functions, implemented by delegating to the existing functions with reordered arguments.
  • functions.go: register the new functions in the template function map.
  • regex_test.go: unit tests for each new function (mirroring the existing test cases) plus a text/template pipeline test proving $s | regexXxxGlobal ... actually works.
  • docs/strings.md: document each new function next to its existing counterpart.

Testing

  • go build ./...
  • go vet ./...
  • go test ./... — passes except for the pre-existing, unrelated TestRound floating-point flake on numeric.go, which also fails on unmodified master.

Fixes #86

…nt order

regexFindAll, regexReplaceAll, regexReplaceAllLiteral and regexSplit take the
subject string as a middle argument, so they cannot be used at the end of a
template pipeline (Go template pipelines always pass the previous value as
the last argument). Users have to fall back to storing the subject in a
variable instead of piping into these functions.

Add non-breaking *Global variants (regexFindAllGlobal,
regexReplaceAllGlobal, regexReplaceAllLiteralGlobal, regexSplitGlobal, and
their must* counterparts) that reorder arguments so the subject string comes
last, making them pipeline-friendly, e.g.:

    $s | regexReplaceAllGlobal "a(x*)b" "${1}W"

Fixes Masterminds#86

Signed-off-by: Porter <porter@bejara.net>

This branch has not been deployed

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

Labels

None yet

1 participant