Placeholders¶
There are two types of placeholder; document placeholders and faker placeholders.
Document placeholders are used when we want to refer to a value in the generated document.
Faker placeholders are used when we want to refer to a faker key that is not directly exposed by an operator or when we want to combine multiple faker keys together.
Document Placeholders¶
The basic syntax for document placeholders is ${ <document key> }
.
{
"firstName": "$first",
"lastName": "$last",
"fullName": "${firstName} ${lastName}"
}
{
"firstName": "Jacques",
"lastName": "Strap",
"fullName": "Jacques Strap"
}
Emdedded Fields¶
Dot-notation is supported in document placeholders so, we can reference embedded fields:
{
"person": {
"name": "$first",
"age": 26
},
"repeatName": "${person.name}"
}
{
"person": {
"name": "John",
"age": 26
},
"repeatName": "John"
}
Arrays¶
Dot-notation is also supported for referencing array elements by their index.
Note: Arrays are zero-indexed in mgenerate
{
"names": ["Shaina", "Callie", "Maximilian", "Mackenzie", "Bob"],
"element0": "${names.0}",
"element2": "${names.2}"
}
{
"names": ["Shaina", "Callie", "Maximilian", "Mackenzie", "Bob"],
"element0": "Shaina",
"element2": "Maximilian"
}
If the placeholder refers to a field which exists more than once, an array will be returned:
{
"people": [
{ "name": "Shaina" },
{ "name": "Callie" },
{ "name": "Maximilian" }
],
"names": "${people.name}"
}
{
"people": [
{ "name": "Shaina" },
{ "name": "Callie" },
{ "name": "Maximilian" }
],
"names": ["Shaina", "Callie", "Maximilian"]
}
N-depth nesting with mixed array and document references will also work:
{
"people": [
{
"name": "Shaina",
"addresses": [
{ "city": "$city" },
{ "city": "$city" }
]
},
{
"name": "Callie",
"addresses": { "city": "$city" }
},
{
"name": "Maximilian"
}
],
"cities": "${people.addresses.city}"
}
{
"people": [
{
"name": "Shaina",
"addresses": [
{ "city": "London" },
{ "city": "Cardiff" }
]
},
{
"name": "Callie",
"addresses": { "city": "New York" }
},
{
"name": "Maximilian"
}
],
"cities": ["London", "Cardiff", "New York"]
}
And, of course, document placeholders can be passed to an operator.
{
"scores": [
{ "type": "homework", "score": 54 },
{ "type": "exam", "score": 99 },
{ "type": "coursework", "score": 13 }
],
"topScore": { "$max": "${scores.score}" }
}
{
"scores": [
{ "type": "homework", "score": 54 },
{ "type": "exam", "score": 99 },
{ "type": "coursework", "score": 13 }
],
"topScore": 99
}
Faker Placeholders¶
The syntax for document placeholders is #{ <faker key> }
.
Faker keys can be found in the faker library resources.
{ "email": "#{name.first_name}.#{name.last_name}@github.com" }
{ "email": "Seymour.Butz@github.com" }