@cosmology/ast
Version:
Cosmos TypeScript AST generation
86 lines (85 loc) • 3.84 kB
JavaScript
import * as t from '@babel/types';
import { slugify } from '@cosmology/utils';
import { arrowFunctionExpression, identifier } from '../../../utils';
import { BinaryCoder } from '../../../utils/binary-coder-expression';
const getMapFromTypeUrlMap = (urlMap, name) => {
return (urlMap?.[name]?.reduce((m, v) => {
v.types.forEach((type) => {
m[type.importAs] = type.typeUrl;
});
return m;
}, {}) ?? {});
};
const firstUpper = (s) => (s = s.charAt(0).toUpperCase() + s.slice(1));
export const getInterfaceDecoderName = (str) => {
return firstUpper(slugify(str) + '_InterfaceDecoder');
};
export const createInterfaceDecoder = (context, ref, interfaceName) => {
const typeMap = context.store.getTypeUrlMap(ref);
const typeRefs = typeMap[interfaceName];
return createInterfaceDecoderHelper(context, getInterfaceDecoderName(interfaceName), typeRefs);
};
export const createInterfaceDecoderHelper = (context, functionName, typeRefs) => {
let useUseInterfacesParams = context.pluginValue("interfaces.useUseInterfacesParams");
BinaryCoder.addUtil(context);
// MARKED AS NOT DRY
const allTypes = typeRefs?.reduce((m, typeRef) => {
return [...m, ...typeRef.types];
}, []) ?? [];
const returnTypes = allTypes.map((type) => type.importAs);
const decodeMessages = allTypes.map((type) => type.typeUrl);
const switches = returnTypes.map((returnType, i) => {
let params = [
t.memberExpression(t.identifier('data'), t.identifier('value')),
];
if (useUseInterfacesParams) {
params.push(t.identifier('undefined'));
params.push(t.booleanLiteral(true));
}
return t.switchCase(t.stringLiteral(decodeMessages[i]), [
t.returnStatement(t.callExpression(t.memberExpression(t.identifier(returnType), t.identifier('decode')), params))
]);
});
let decodeParams = [
t.identifier('reader'),
t.callExpression(t.memberExpression(t.identifier('reader'), t.identifier('uint32') // NOTE is it always uint32?
), [])
];
if (useUseInterfacesParams) {
decodeParams.push(t.booleanLiteral(true));
}
return t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(functionName), arrowFunctionExpression([
identifier('input', t.tsTypeAnnotation(t.tsUnionType([
BinaryCoder.getReaderTypeRef(context),
t.tsTypeReference(t.identifier('Uint8Array'))
])), false)
],
// body
t.blockStatement([
/// READER
t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('reader'), t.conditionalExpression(t.binaryExpression('instanceof', t.identifier('input'), BinaryCoder.getReaderMemberExp(context)), t.identifier('input'), t.newExpression(BinaryCoder.getReaderMemberExp(context), [
t.identifier('input')
])))
]),
// DATA
t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('data'), t.callExpression(t.memberExpression(t.identifier('Any'), t.identifier('decode')), decodeParams))
]),
// SWITCH
t.switchStatement(t.memberExpression(t.identifier('data'), t.identifier('typeUrl')), [
...switches,
/////
t.switchCase(null, [t.returnStatement(t.identifier('data'))])
])
]),
// return type
t.tsTypeAnnotation(
// do we need to use interfaces.useUnionTypes here ?
t.tsUnionType([
...returnTypes.map((type) => t.tsTypeReference(t.identifier(type))),
t.tsTypeReference(t.identifier('Any'))
]))))
]));
};