Skip to content

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.

The pattern consists of three parts:

  1. Field declaration with parameters
  2. Generator function that accepts those parameters
  3. Calls section that provides the arguments
Session.dg
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:

Terminal window
datagenc gen Session.dg -n 3

Output:

Terminal window
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}}

Arguments in the calls section are evaluated once at the start and reused for all generated records.

example.dg
calls {
created_at(time.Now(), time.Now().Add(24*time.Hour))
// These timestamps are calculated once, not per record
}

Argument types and counts must match the field signature exactly:

// Correct: Types match
fields {
user_id(min int, max int) int
}
calls {
user_id(1000, 2000)
}
// Incorrect: Type mismatch
calls {
user_id("1000", "2000") // Strings instead of ints
}
// Incorrect: Wrong number of arguments
calls {
user_id(1000) // Missing second argument
}
// Incorrect: Parameters in gens but not in fields
fields {
user_id() int // Missing parameters
}
gens {
func user_id(min int, max int) {
return IntBetween(min, max)
}
}
// Incomplete: No calls entry for parameterized field
fields {
user_id(min int, max int) int
}
gens {
func user_id(min int, max int) {
return IntBetween(min, max)
}
}
// Missing: calls section!
// Misunderstanding: time.Now() is evaluated once, not per record
calls {
timestamp(time.Now(), time.Now().Add(24*time.Hour))
// All records will use the same timestamp range
}
// Use iter for per-record variation instead
gens {
func timestamp(baseTime time.Time, interval time.Duration) {
return baseTime.Add(time.Duration(iter) * interval)
}
}
calls {
timestamp(time.Now(), time.Hour)
}