Cleaned up Combo file

This commit is contained in:
Ozzie Gooen 2020-02-09 18:47:12 +00:00
parent 573f502a06
commit edaa7a7ca5
2 changed files with 46 additions and 39 deletions

View File

@ -18,6 +18,7 @@ module O = {
let bind = Rationale.Option.bind;
let default = Rationale.Option.default;
let isSome = Rationale.Option.isSome;
let isNone = Rationale.Option.isNone;
let toExn = Rationale.Option.toExn;
let some = Rationale.Option.some;
let firstSome = Rationale.Option.firstSome;

View File

@ -54,19 +54,20 @@ module ValueMap = {
let get = MS.get;
let keys = MS.keysToArray;
let map = MS.map;
let fromArray = MS.fromArray;
let values = t => t |> MS.valuesToArray;
let fromArray = (r): t => MS.fromArray(r);
let values = (t: t) => t |> MS.valuesToArray;
let update = MS.update;
let toArray = MS.toArray;
let fromOptionalMap = (t: MS.t(option(Value.t))): t =>
MS.keep(t, (_, d) => E.O.isSome(d))
->MS.map(d => E.O.toExn("This should not have happened", d));
let fromOptionalArray = (r): t => MS.fromArray(r) |> fromOptionalMap;
};
module TypeWithMetadata = {
// TODO: Figure out a better name for assumptionType
type assumptionType =
| INPUT
| PRIMARY_INPUT
| ASSUMPTION;
type t = {
@ -77,7 +78,7 @@ module TypeWithMetadata = {
assumptionType,
};
type ts = list(t);
type ts = array(t);
// TODO: Change default here
let currentYear = {
@ -89,60 +90,65 @@ module TypeWithMetadata = {
};
let make =
(~name, ~type_, ~id=name, ~description=None, ~assumptionType=INPUT, ()) => {
(
~name,
~type_,
~id=name,
~description=None,
~assumptionType=PRIMARY_INPUT,
(),
) => {
id,
name,
type_,
description,
assumptionType,
};
let toValueMap = (ts: ts) => {
ts
->Array.of_list
->Belt.Array.map((b: t) => (b.name, Type.default(b.type_)))
->ValueMap.fromArray
->ValueMap.fromOptionalMap;
};
};
module Model = {
type t = {
name: string,
author: string,
inputTypes: list(TypeWithMetadata.t),
ouputTypes: list(TypeWithMetadata.t),
inputTypes: array(TypeWithMetadata.t),
outputTypes: array(TypeWithMetadata.t),
};
type inputValues = {
inputs: ValueMap.t,
outputSelection: string,
module InputTypes = {
let keys = (t: t) =>
t.inputTypes |> E.A.fmap((r: TypeWithMetadata.t) => r.name);
};
};
module Combo = {
type combo = {
model: Model.t,
inputValues: ValueMap.t,
outputValues: ValueMap.t,
};
type outputValues = ValueMap.t;
module InputValues = {
let defaults = (t: t): inputValues => {
inputs: t.inputTypes |> TypeWithMetadata.toValueMap,
outputSelection: "",
};
// TODO: This should probably come with a validation or something.
let updateInputs =
let defaults = (t: Model.t) =>
t.inputTypes
|> E.A.fmap((o: TypeWithMetadata.t) =>
(o.name, Type.default(o.type_))
)
|> ValueMap.fromOptionalArray;
let isValid = (t: combo) =>
t.model
|> Model.InputTypes.keys
|> E.A.fmap(ValueMap.get(t.inputValues))
|> Belt.Array.some(_, E.O.isNone);
let update =
(
t: t,
inputValues: inputValues,
t: combo,
key: string,
onUpdate: option(Value.t) => option(Value.t),
) => {
ValueMap.update(inputValues.inputs, key, onUpdate);
};
) =>
ValueMap.update(t.inputValues, key, onUpdate);
};
let run = (inputs: inputValues, f) => f(inputs);
};
module InputValues = {
type t = Model.inputValues;
};
module OutputValues = {
type t = ValueMap.t;
let run = (t: combo, f): ValueMap.t => f(t.inputValues);
};