Definitions
Type Definitions
Since Bref supports JSON, we’ll use JSON data for this example.
This is what our data looks like:
{
"title": "Bohemian Rhapsody",
"duration": "5:55",
"streams": 1980000000,
"is_favorite": true
}
What we’re going to do is define the structure of the data upfront.
In the Bref format, we start with a colon :
followed by the type definition.
We can define the structure of our data as follows:
:song { title, duration, streams, is_favorite }
{ "Bohemian Rhapsody", "5:55", 1980000000, true }: song
In scenarios where you will use a type definition only once, you can also place it at the end of the object.
{ "Bohemian Rhapsody", "5:55", 1980000000, true }: { title, duration, streams, is_favorite }
Nested Type Definitions
You can also define nested data structures in the same way. Let’s add some new keys to our data. Here’s the updated version:
{
"title": "Bohemian Rhapsody",
"duration": "5:55",
"genre": "Rock",
"album": {
"title": "A Night at the Opera",
"year": 1975,
"band": {
"name": "Queen",
"country": "UK"
}
},
"streams": 1980000000,
"is_favorite": true
}
In the Bref format, we again apply the same process by defining types.
:song { title, duration, genre, album:album, streams, is_favorite }
:album { title, year, band:band }
:band { name, country }
{
"Bohemian Rhapsody", "5:55", "Rock",
{ "A Night at the Opera", 1975, { "Queen", "UK" } },
1980000000, true
}: song
Array Type Definitions
We can apply exactly the same process we used for objects to define types for arrays.
:song { title, duration, genre, album:album, streams, is_favorite }
:album { title, year, band:band }
:band { name, country }
[
{
"Bohemian Rhapsody", "5:55", "Rock",
{ "A Night at the Opera", 1975, { "Queen", "UK" } },
1980000000, true
},
{
"Smells Like Teen Spirit", "5:01", "Alternative Rock",
{ "Nevermind", 1991, { "Nirvana", "USA" } },
1750000000, true
}
]: song
If you want to define an array inside an object, you can specify it like this:
:song { title, duration, genre, album:album, streams, is_favorite }
:album { title, year, band:band, tracks:track[] }
:band { name, country }
:track { title }
[
{
"Bohemian Rhapsody", "5:55", "Rock",
{
"A Night at the Opera", 1975, { "Queen", "UK" },
[ { "Love of My Life" }, { "You're My Best Friend" } ]
},
1980000000, true
}
]: song
Value Definitions
If we can define keys, we can just as easily define repeating values too. When certain values appear over and over in your data, you can turn them into variables.
Imagine we have the following data.
[
{
"store": "TeknoMarket",
"address": { "country": "Türkiye", "city": "Istanbul", "town": "Maltepe" }
},
{
"store": "Kitap Dünyası",
"address": { "country": "Türkiye", "city": "Istanbul", "town": "Maltepe" }
},
{
"store": "Lezzet Durağı",
"address": { "country": "Türkiye", "city": "Ankara", "town": "Beypazarı" }
},
{
"store": "Moda Giyim",
"address": { "country": "Türkiye", "city": "Ankara", "town": "Beypazarı" }
}
]
In Bref, we can turn repeating values into definitions.
:store { name, address:address }
:address { country, city, town }
:ist { "Türkiye", "Istanbul", "Maltepe" }
:ank { "Türkiye", "Ankara", "Beypazarı" }
[
{ "TeknoMarket", ist },
{ "Kitap Dünyası", ist },
{ "Lezzet Durağı", ank },
{ "Moda Giyim", ank }
]: store