-
Notifications
You must be signed in to change notification settings - Fork 183
Add random_int function #1475
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 random_int function #1475
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "math/rand" | ||
| "net/url" | ||
| "os" | ||
| "regexp" | ||
|
|
@@ -46,6 +47,10 @@ func loadHelpers(ctx context.Context) template.FuncMap { | |
| "regexp": func(expr string) (*regexp.Regexp, error) { | ||
| return regexp.Compile(expr) | ||
| }, | ||
| // Alias for https://pkg.go.dev/math/rand#Intn. Returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n). | ||
| "randIntn": func(n int) int { | ||
| return rand.Intn(n) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a fine addition, but what's the use case? We're a bit wary of adding template extensions at this point since it may reduce future portability of templates.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lennartkats-db we need this in MLOps Stacks to generate a random int jitter for workflows and monitoring. I know workflows has a plan to add periodic triggers for this use case, but monitoring doesn't, and we don't want to schedule all our monitors to run at once
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack, thanks for the context. |
||
| }, | ||
| // A key value pair. This is used with the map function to generate maps | ||
| // to use inside a template | ||
| "pair": func(k string, v any) pair { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {{print (randIntn 10)}} |
Uh oh!
There was an error while loading. Please reload this page.