Add "randInt" template function for random number#540
Add "randInt" template function for random number#540creativeprojects merged 1 commit intocreativeprojects:masterfrom
Conversation
WalkthroughA new deterministic pseudo-random integer template function, Changes
Sequence Diagram(s)sequenceDiagram
participant Template as Template Engine
participant Funcs as TemplateFuncs Map
participant Rand as randInt Function
participant RNG as PCG Random Generator
Template->>Funcs: Call "randInt" with (min, max, seed)
Funcs->>Rand: Invoke randInt(min, max, seed)
Rand->>Rand: Compute MD5(seed)
Rand->>RNG: Seed PCG RNG with hash
RNG->>Rand: Generate random int in [min, max)
Rand->>Funcs: Return int64 result
Funcs->>Template: Substitute value in template
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
✨ Finishing Touches
🧪 Generate 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
util/templates/functions_test.go (1)
62-64: Enhance randInt edge-case coverageThe implementation always returns min when max = min + 1 (Int64N(1) ⇒ 0). It would be valuable to add at least one test for this single-value range:
• In util/templates/functions_test.go, immediately after the existing cases (around line 64), add:
{template: `{{ "" | randInt 5 6 }}`, expected: `5`}, // single-value range (max = min+1)Optionally, to explicitly verify determinism, you could add a template that calls randInt twice with the same seed and asserts the two outputs are equal—if the test harness allows a boolean assertion.
Please confirm these expected values and test-harness support for repeat-call assertions.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
docs/content/configuration/templates.md(1 hunks)util/templates/functions.go(4 hunks)util/templates/functions_test.go(1 hunks)
🔇 Additional comments (3)
util/templates/functions.go (2)
4-4: LGTM! Import additions are appropriate.The new imports (
crypto/md5,encoding/binary,math/rand/v2) are correctly added and necessary for therandIntfunction implementation.Also applies to: 6-6, 11-11
49-49: LGTM! Function registration and documentation are correct.The
randIntfunction is properly registered in theTemplateFuncsmap and the documentation comment accurately describes the expected usage pattern.Also applies to: 71-71
docs/content/configuration/templates.md (1)
603-605: Documentation is accurate and well-formatted.The documentation correctly describes the
randIntfunction behaviour, matches the implementation signature, and follows the established documentation pattern. The example clearly demonstrates the deterministic nature of the function.
|
Hey! Thanks for this PR 👍🏻 I'm OK with using You can do the same for the |
The `randInt` function added by this change returns a pseudo-random number in a given range and using a seed for repeatable randomness. It can be used, for example, in cron schedules to pick a minute offset based on an environment variable (e.g. hostname).
|
@creativeprojects, thank you for the quick review. I added the |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #540 +/- ##
=======================================
Coverage 79.33% 79.33%
=======================================
Files 136 136
Lines 13305 13316 +11
=======================================
+ Hits 10555 10564 +9
- Misses 2332 2333 +1
- Partials 418 419 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Looks good to me, thanks for this PR 👍🏻 |
The
randIntfunction added by this change returns a pseudo-random number in a given range and using a seed for repeatable randomness. It can be used, for example, in cron schedules to pick a minute offset based on an environment variable (e.g. hostname).