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 bind = Rationale.Option.bind;
let default = Rationale.Option.default; let default = Rationale.Option.default;
let isSome = Rationale.Option.isSome; let isSome = Rationale.Option.isSome;
let isNone = Rationale.Option.isNone;
let toExn = Rationale.Option.toExn; let toExn = Rationale.Option.toExn;
let some = Rationale.Option.some; let some = Rationale.Option.some;
let firstSome = Rationale.Option.firstSome; let firstSome = Rationale.Option.firstSome;

View File

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