Skip to content

Instantly share code, notes, and snippets.

@gladimdim
Created December 18, 2017 12:12
Show Gist options
  • Save gladimdim/5272bb06577dbcd2b87f5a268b84b6c4 to your computer and use it in GitHub Desktop.
Save gladimdim/5272bb06577dbcd2b87f5a268b84b6c4 to your computer and use it in GitHub Desktop.
Switch statement for optional
type metricCount = {type_: string};
type metricWithLabel = {
type_: string,
label: string
};
let createCountWithLabel = (label: string) : metricWithLabel => {type_: "COUNT", label};
let countMetric: metricCount = {type_: "COUNT"};
let encodeCountMetric = (m: metricCount) => Json.Encode.(object_([("type", string(m.type_))]));
};
let transformToCount20 = (oldJson) => {
let label = Json.Decode.(oldJson |> optional(field("label", string)));
switch label {
| Some(v) => createCountWithLabel(v) |> MetricEncoder.encodeCountMetricWithLabel
| None => countMetric |> MetricEncoder.encodeCountMetric
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment