@cosmology/ast
Version:
Cosmos TypeScript AST generation
37 lines (36 loc) • 1.85 kB
JavaScript
import * as t from '@babel/types';
import { identifier, objectMethod } from '../../../utils';
import { SymbolNames } from '../../types';
import { getTypeUrl } from '../../amino';
export const toProtoMsgMethod = (context, name, proto) => {
const varName = 'message';
const ReturnType = SymbolNames.ProtoMsg(name);
const TypeName = SymbolNames.Msg(name);
const typeUrl = getTypeUrl(context.ref.proto, proto);
if (!typeUrl)
return;
const body = [];
// body
body.push(t.returnStatement(t.objectExpression([
t.objectProperty(t.identifier('typeUrl'), t.stringLiteral(typeUrl)),
t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(TypeName), t.identifier('encode')), [
t.identifier(varName)
]), t.identifier('finish')), []))
])));
return objectMethod('method', t.identifier('toProtoMsg'), [
identifier(varName, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName))))
], t.blockStatement(body), false, false, false, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ReturnType))));
};
export const toProtoMethod = (context, name, proto) => {
const varName = 'message';
const ReturnType = 'Uint8Array';
const TypeName = SymbolNames.Msg(name);
const body = [];
// body
body.push(t.returnStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(TypeName), t.identifier('encode')), [
t.identifier('message')
]), t.identifier('finish')), [])));
return objectMethod('method', t.identifier('toProto'), [
identifier(varName, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName))))
], t.blockStatement(body), false, false, false, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ReturnType))));
};