Hi,
I would like to get recommendations on how to approach this problem.
I’m writing a client for an API (tumblr). For example, this API let me retrieve all the posts for a given tumblr blog.
Yet, those posts can be of different types such as text
, photo
, video
, audio
, … Any post will have one of these type but they all share some common properties like a date, the number of likes, etc. Given their nature, they also have specific properties. Let’s say that audio
and video
posts have a duration
property but text posts don’t.
The way you know that a post is of type X is by checking the type
that is one of the common properties.
Until today, I focused on the properties in common between all types and I created a Post struct to represent them. Now I would like to have a way to represent all specific types and ideally without copy-pasting the current Post struct everywhere.
What are my options to represent all types too ?
EDIT: If I define all properties of all post types into the one I currently have, this is what I get:
@type t :: %MODULE{
blog_name: String.t,
id: non_neg_integer,
post_url: String.t,
type: String.t,
timestamp: non_neg_integer,
date: String.t,
format: String.t,
reblog_key: String.t,
tags: [String.t],
bookmarklet: boolean,
mobile: boolean,
source_url: String.t, #MOVE TO QUOTE POSTS? see “Notes” column in docs
source_title: String.t, #MOVE TO QUOTE POSTS?
liked: boolean,
state: String.t,
#FOR TEXT POSTS
title: String.t,
body: String.t,
#FOR PHOTO POSTS
photos: , #todo: list of photos objects
caption: String.t,
width: number(),
height: number(),
#FOR QUOTE POSTS
text: String.t,
source: String.t,
#FOR LINK POSTS
title: String.t, #fixme: already defined for text posts
url: String.t,
author: String.t,
excerpt: String.t,
publisher: String.t,
photos: , #fixme: also defined for photo posts
description: String.t,
#FOR CHAT POSTS
title: String.t, #fixme: also defined for link and text posts
body: String.t, #fixme: also defined for text posts
dialogue: , #todo: define a type for the contained objects
#FOR AUDIO POSTS
caption: String.t, #fixme: also defined for photo posts
player: String.t,
plays: non_neg_integer,
album_art: String.t,
artist: String.t,
album: String.t,
track_name: String.t,
track_number: non_neg_integer,
year: non_neg_integer,
#FOR VIDEO POSTS
caption: String.t, #fixme: also defined for audio and photo posts
player: , #fixme: also defined for audio posts AND IT’S A DIFFERENT TYPE!!!
#FOR ANSWERS POSTS
asking_name: String.t,
asking_url: String.t,
question: String.t,
answer: String.t