Function Calls (Parameterized Fields)
When a field generator function accepts parameters, you must provide those arguments in a calls section. This enables you to create flexible, configurable field generators while keeping the generation logic clean and reusable.
Basic Structure
Section titled “Basic Structure”The pattern consists of three parts:
- Field declaration with parameters
- Generator function that accepts those parameters
- Calls section that provides the arguments
model Session { // 1. Declare field with parameters fields { created_at(start time.Time, end time.Time) time.Time }
// 2. Define generator function gens { func created_at(start time.Time, end time.Time) { return DateBetween(start, end) } }
// 3. Provide arguments calls { created_at(time.Now().AddDate(-1, 0, 0), time.Now()) }}Running the example:
datagenc gen Session.dg -n 3Output:
Session{created_at:{wall:205915000 ext:63892967191 loc:0x10140eb40}}Session{created_at:{wall:205915000 ext:63884181274 loc:0x10140eb40}}Session{created_at:{wall:205915000 ext:63890140992 loc:0x10140eb40}}How It Works
Section titled “How It Works”Argument Evaluation
Section titled “Argument Evaluation”Arguments in the calls section are evaluated once at the start and reused for all generated records.
calls { created_at(time.Now(), time.Now().Add(24*time.Hour)) // These timestamps are calculated once, not per record}Type Matching
Section titled “Type Matching”Argument types and counts must match the field signature exactly:
// Correct: Types matchfields { user_id(min int, max int) int}calls { user_id(1000, 2000)}
// Incorrect: Type mismatchcalls { user_id("1000", "2000") // Strings instead of ints}
// Incorrect: Wrong number of argumentscalls { user_id(1000) // Missing second argument}Common Pitfalls
Section titled “Common Pitfalls”Forgetting to Declare Parameters
Section titled “Forgetting to Declare Parameters”// Incorrect: Parameters in gens but not in fieldsfields { user_id() int // Missing parameters}
gens { func user_id(min int, max int) { return IntBetween(min, max) }}Missing calls Entry
Section titled “Missing calls Entry”// Incomplete: No calls entry for parameterized fieldfields { user_id(min int, max int) int}
gens { func user_id(min int, max int) { return IntBetween(min, max) }}
// Missing: calls section!Dynamic Evaluation Assumption
Section titled “Dynamic Evaluation Assumption”// Misunderstanding: time.Now() is evaluated once, not per recordcalls { timestamp(time.Now(), time.Now().Add(24*time.Hour)) // All records will use the same timestamp range}
// Use iter for per-record variation insteadgens { func timestamp(baseTime time.Time, interval time.Duration) { return baseTime.Add(time.Duration(iter) * interval) }}
calls { timestamp(time.Now(), time.Hour)}See Also
Section titled “See Also”- Examples - Calls - Practical calls examples
- Data Model - Core model concepts
- iter Variable - Using iter with parameterized fields