-
-
Save dckc/60caeb9c352e8e6186215735b7e719e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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