Skip to content

Commit

Permalink
Add stringLiteralUnionFields helper to model module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarno Rantanen committed Apr 24, 2020
1 parent 464c339 commit 321885f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/common/io.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as t from 'io-ts';
import { LiteralType } from 'io-ts';

function defineRegexValidatedStringType(name: string, regex: RegExp) {
const guard = (input: unknown): input is string => typeof input === 'string' && !!input.match(regex);
Expand Down Expand Up @@ -41,3 +42,8 @@ export const generalWellbeing = t.union([t.literal('fine'), t.literal('impaired'

// range of 0–99 days
export const duration = defineRegexValidatedStringType('symptomsDuration', /^[0-9]{1,2}$/);

// @example isStringLiteralType(fever) => false
// @example isStringLiteralType(fever.types[0]) => true
export const isStringLiteralType = (x: unknown): x is LiteralType<string> =>
x instanceof LiteralType && typeof x.value === 'string';
26 changes: 25 additions & 1 deletion src/common/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isRight } from 'fp-ts/lib/Either';
import * as t from 'io-ts';
import { UnionType } from 'io-ts';
import { PathReporter } from 'io-ts/lib/PathReporter';
import { AbuseScore } from '../backend/abuseDetection';
import {
Expand All @@ -10,16 +11,18 @@ import {
gender,
generalWellbeing,
iso8601DateString,
isStringLiteralType,
notAnswered,
postalCode,
uuidString,
yesOrNo,
} from './io';
import { nonNullable } from './types';

// Because AWS Athena prefers lower-case column names (https://docs.aws.amazon.com/athena/latest/ug/tables-databases-columns-names.html),
// we use snake case for some of these models, instead of camel case (https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).

const responseFields = {
export const responseFields = {
fever: fever,
cough: cough,
breathing_difficulties: yesOrNo,
Expand All @@ -38,6 +41,7 @@ const responseFields = {
gender: gender,
postal_code: postalCode,
};
export const responseFieldKeys = (Object.keys(responseFields) as any) as Array<keyof typeof responseFields>; // this is theoretically unsafe (https://stackoverflow.com/a/55012175) but practically a lot safer than going with string[] ¯\_(ツ)_/¯

export const FrontendResponseModel = t.strict(
{
Expand Down Expand Up @@ -84,3 +88,23 @@ export function assertIs<C extends t.ExactC<any>>(codec: C): (x: unknown) => t.T
}
};
}

// Defines tuples describing all response fields that are simple unions of string literals.
// @example [
// [ 'healthcare_contact', [ 'yes', 'no' ] ],
// [ 'general_wellbeing', [ 'fine', 'impaired', 'bad' ] ],
// ...
// ]
// What makes these fields special is that their values are easy to GROUP BY, SUM() etc in queries.
export const stringLiteralUnionFields: [string, string[]][] = responseFieldKeys
.map(key =>
responseFields[key] instanceof UnionType
? ([
key,
(responseFields[key] as any).types // TODO: Assert that responseFields[key] is UnionType<Array<LiteralType>> instead (how?), to get rid of the awkward any
.map((t: unknown) => (isStringLiteralType(t) ? t.value : null))
.filter(nonNullable),
] as [string, string[]])
: null,
)
.filter(nonNullable);

0 comments on commit 321885f

Please sign in to comment.