Skip to content

Optional Model Sections

Beyond the core sections (fields and gens), datagen provides optional sections for configuration and customization.

The metadata section provides configuration for your model, including default record counts and organizational tags.

User.dg
metadata {
count: 1000
tags: {
"service": "user-management",
"team": "platform",
"environment": "test"
}
}

Sets the default number of records to generate. If no count is specified, defaults to 1 record.

Key-value pairs for organizing and filtering models. Tags are especially useful when working with multiple models in a directory.

Tag Guidelines:

  • Must be valid JSON (quoted string keys and values)
  • Use consistent, lowercase keys (e.g., “domain”, “environment”, “service”)
  • Combine multiple tags to express different facets of a model

Example with Multiple Models:

User.dg
model User {
metadata {
count: 1000
tags: {
"service": "user-management",
"team": "platform",
"priority": "high"
}
}
// ...
}
Order.dg
model Order {
metadata {
count: 5000
tags: {
"service": "order-processing",
"team": "platform",
"priority": "critical"
}
}
// ...
}

The misc section allows you to inject custom Go code into the generated package. This is useful for defining custom types, constants, and helper functions.

  • Constants and variables
  • Custom types and enums
  • Structs and interfaces
  • Helper functions
  • Any valid Go code

Here’s a full model using the misc section:

Product.dg
model Product {
misc {
type Category string
const (
Electronics Category = "electronics"
Clothing Category = "clothing"
Food Category = "food"
Books Category = "books"
)
func randomCategory() Category {
categories := []Category{Electronics, Clothing, Food, Books}
return categories[IntBetween(0, len(categories)-1)]
}
func priceForCategory(cat Category) float32 {
switch cat {
case Electronics:
return FloatBetween(50.0, 2000.0)
case Clothing:
return FloatBetween(10.0, 200.0)
case Food:
return FloatBetween(1.0, 50.0)
case Books:
return FloatBetween(5.0, 100.0)
default:
return 10.0
}
}
}
fields {
id() int
name() string
category() string
price() float32
}
gens {
func id() {
return iter + 1
}
func name() {
return Sentence(3)
}
func category() {
cat := randomCategory()
return string(cat)
}
func price() {
cat := Category(self.category(iter))
return priceForCategory(cat)
}
}
}