Skip to content

Instantly share code, notes, and snippets.

@Anahkiasen
Created June 24, 2018 19:22
Show Gist options
  • Save Anahkiasen/2f273bcd46252f04242a87d41083e15a to your computer and use it in GitHub Desktop.
Save Anahkiasen/2f273bcd46252f04242a87d41083e15a to your computer and use it in GitHub Desktop.
module Models = {
type actor = {
id: int,
name: string,
character: option(string),
link: string,
thumbnail: option(string),
hash: string,
seen: list(watchable),
watchables: list(watchable),
}
[@bs.deriving abstract]
and watchable = {
id: int,
identifier: option(string),
name: string,
overview: option(string),
show: option(string),
link: string,
actors: list(actor),
released_at: option(string),
watched_at: option(string),
credit: option(string),
type_: string,
};
};
module Decode = {
let emptyToOption = value => value === "" ? None : Some(value);
let optionalNullableField = (key, json) : option(string) =>
Json.Decode.(json |> field(key, nullable(string)) |> Js.nullToOption);
let rec watchable = json : Models.watchable =>
Json.Decode.{
id: json |> field("id", int),
identifier: json |> optional(field("identifier", string)),
name: json |> field("name", string),
overview: json |> optionalNullableField("overview"),
link: json |> field("link", string),
released_at: json |> optionalNullableField("released_at"),
watched_at: json |> optionalNullableField("watched_at"),
credit: json |> optional(field("credit", string)),
show: json |> optional(field("show", string)),
actors: json |> field("actors", list(actor)),
type_: json |> field("type", string),
}
and actor = json : Models.actor =>
Json.Decode.{
id: json |> field("id", int),
name: json |> field("name", string),
character: json |> optional(field("character", string)),
link: json |> field("link", string),
thumbnail: json |> field("thumbnail", string) |> emptyToOption,
hash: json |> field("hash", string),
seen: json |> field("seen", list(watchable)),
watchables: json |> field("watchables", list(watchable)),
};
};
@hsavit1
Copy link

hsavit1 commented Jun 24, 2018

what does the "and" keyword mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment