I’m developing a website that should list dance events. The backend will return something like:
{
"locations": [
{
"id": 1,
"name": "Chico Mendès",
"address": "Pontstraße 74-76, 52062 Aachen"
},
{
"id": 2,
"name": "Sencillito",
"address": "Alexander Strasse 109, 52066 Aachen"
}
],
"events": [
{
"id": 1,
"name": "Anfängerkurs",
"teaser": "Für diejenigen, die Lindy Hop ausprobieren möchten.",
"description": "Unter Anleitung werden dir die Grundschritte des Lindy Hops beigebracht.",
"occurrences": [
{
"id": 1,
"start": 1554140700,
"duration": 45,
"location": 1
},
{
"id": 2,
"start": 1554748200,
"duration": 90,
"location": 2
}
]
},
{
"id": 2,
"name": "Social Dance",
"teaser": "Einfach Lindy Hop tanzen.",
"description": "Hier triffst du viele andere Menschen, die ebenfalls Lust haben, Lindy Hop zu tanzen.",
"occurrences": [
{
"id": 3,
"start": 1554143400,
"duration": 90,
"location": 1
},
{
"id": 4,
"start": 1554748200,
"duration": 90,
"location": 2
}
]
}
]
}
The troublesome part is that each occurrence’s location is referenced by id. The only choices I see is to inline the location into each occurrence, or look up a occurrence’s location by id each time I need it. Inlining causes redundancy and prevents me from changing a location in a central place. Keeping the ids means, that I always need to deal with the possibility that the id is invalid.
This is a case, where a reference would be nice, but as can be seen in Rust, doing this safely requires a lot of additional concepts like lifetimes. I could hack around the issue, by keeping the ids and using a Dict Id Location
that upon each lookup I chain into a Maybe.withDefault someGarbageLocation
. But this is a hack, and I was wondering whether there was a more elegant approach to this issue?