Skip to content

Instantly share code, notes, and snippets.

@dckc
Created January 14, 2022 04:02
Show Gist options
  • Save dckc/60caeb9c352e8e6186215735b7e719e3 to your computer and use it in GitHub Desktop.
Save dckc/60caeb9c352e8e6186215735b7e719e3 to your computer and use it in GitHub Desktop.
import './rnode-openapi-schema';
const { isArray } = Array;
const { fromEntries } = Object;
const Nil = { ExprPar: { data: [] } };
/**
* @param {X[]} xs
* @param {Y[]} ys
* @returns {Array<[X, Y]>}
* @template X
* @template Y
*/
const zip = (xs, ys) => xs.map((x, ix) => [x, ys[ix]]);
/**
* @param {Encoding} encoding
* @param {boolean=} shouldIndent
* @returns {RhoExpr}
*/
const decodeToRhoTypes = encoding => {
const recur = decodeToRhoTypes;
switch (typeof encoding) {
case 'boolean':
return { ExprBool: { data: encoding } };
case 'number':
if (!Number.isSafeInteger(encoding)) throw Error('not a safe integer');
return { ExprInt: { data: encoding } };
case 'bigint': {
const data = Number(encoding);
if (!Number.isSafeInteger(data)) throw Error('not a safe integer');
return { ExprInt: { data } };
}
case 'string':
return { ExprString: { data: encoding } };
case 'object':
if (encoding === null) return Nil;
if ('@qclass' in encoding) {
switch (encoding['@qclass']) {
case 'undefined':
case 'NaN':
case 'Infinity':
case '-Infinity':
throw Error('TODO');
case 'tagged': {
const { tag, payload } = encoding;
switch (tag) {
case 'copySet':
return { ExprSet: { data: payload.map(recur) } };
case 'copyMap': {
/** @type {{ keys: Encoding[], values: Encoding[] }} */
const { keys, values } = payload;
if (keys.filter(x => typeof x !== 'string').length > 0)
throw Error('TODO: non-string keys');
const entries = zip(keys.map(recur), values.map(recur));
return { ExprMap: { data: fromEntries(entries) } };
}
default:
throw Error('TODO');
}
}
default:
throw Error('TODO');
}
} else if (isArray(encoding)) {
return { EList: { data: encoding.map(recur) } };
}
break;
default:
throw Error('TODO');
}
};
harden(decodeToRhoTypes);
export { decodeToRhoTypes };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment