UNPKG

@cosmology/ast

Version:
322 lines (321 loc) 12.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayTypes = exports.fromSDK = void 0; const t = __importStar(require("@babel/types")); const utils_1 = require("../../../utils"); const types_1 = require("../../types"); exports.fromSDK = { scalar(args) { const { propName, origName } = (0, types_1.getFieldNames)(args.field); return t.objectProperty(t.identifier(propName), t.optionalMemberExpression(t.identifier('object'), t.identifier(origName), false, true)); }, string(args) { return exports.fromSDK.scalar(args); }, number(args) { return exports.fromSDK.scalar(args); }, double(args) { return exports.fromSDK.scalar(args); }, float(args) { return exports.fromSDK.scalar(args); }, int32(args) { return exports.fromSDK.scalar(args); }, sint32(args) { return exports.fromSDK.scalar(args); }, uint32(args) { return exports.fromSDK.scalar(args); }, fixed32(args) { return exports.fromSDK.scalar(args); }, sfixed32(args) { return exports.fromSDK.scalar(args); }, bool(args) { return exports.fromSDK.scalar(args); }, long(args) { return exports.fromSDK.scalar(args); }, int64(args) { return exports.fromSDK.scalar(args); }, uint64(args) { return exports.fromSDK.scalar(args); }, sint64(args) { return exports.fromSDK.scalar(args); }, fixed64(args) { return exports.fromSDK.scalar(args); }, sfixed64(args) { return exports.fromSDK.scalar(args); }, type(args) { const { propName, origName } = (0, types_1.getFieldNames)(args.field); let name = args.context.getTypeName(args.field); if (!args.context.options.aminoEncoding.useLegacyInlineEncoding && args.context.options.interfaces.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry && args.field.type === 'google.protobuf.Any' && args.field.options['(cosmos_proto.accepts_interface)']) { name = 'GlobalDecoderRegistry'; } return t.objectProperty(t.identifier(propName), t.conditionalExpression(t.memberExpression(t.identifier('object'), t.identifier(origName)), t.callExpression(t.memberExpression(t.identifier(name), t.identifier('fromSDK')), [ t.memberExpression(t.identifier('object'), t.identifier(origName)) ]), t.identifier('undefined'))); }, enum(args) { const { propName, origName } = (0, types_1.getFieldNames)(args.field); const setDefaultEnumToUnrecognized = args.context.pluginValue('prototypes.typingsFormat.setDefaultEnumToUnrecognized'); args.context.addUtil('isSet'); const fromSDKFuncName = args.context.getFromEnum(args.field); return t.objectProperty(t.identifier(propName), t.conditionalExpression(t.callExpression(t.identifier('isSet'), [ t.memberExpression(t.identifier('object'), t.identifier(origName)) ]), t.callExpression(t.identifier(fromSDKFuncName), [ t.memberExpression(t.identifier('object'), t.identifier(origName)) ]), args.isOptional ? t.identifier('undefined') : t.numericLiteral(!setDefaultEnumToUnrecognized ? 0 : -1))); }, bytes(args) { return exports.fromSDK.scalar(args); }, duration(args) { return exports.fromSDK.type(args); }, timestamp(args) { let timestampFormat = args.context.pluginValue('prototypes.typingsFormat.timestamp'); const env = args.context.pluginValue('env'); if (!env || env == 'default') { timestampFormat = 'timestamp'; } switch (timestampFormat) { case 'timestamp': return exports.fromSDK.type(args); case 'date': default: return exports.fromSDK.timestampDate(args); } }, timestampDate(args) { const { propName, origName } = (0, types_1.getFieldNames)(args.field); return t.objectProperty(t.identifier(propName), t.logicalExpression('??', t.memberExpression(t.identifier('object'), t.identifier(origName)), t.identifier('undefined'))); }, // labels: isObject(object.labels) ? Object.entries(object.labels).reduce<{ // [key: string]: string; // }>((acc, [key, value]) => { // acc[key] = String(value); // return acc; // }, {}) : {}, // referenceMap: isObject(object.referenceMap) ? Object.entries(object.referenceMap).reduce<{ // [key: Long]: Reference; // }>((acc, [key, value]) => { // acc[Number(key)] = Reference.fromSDK(value); // return acc; // }, {}) : {}, keyHash(args) { const { propName, origName } = (0, types_1.getFieldNames)(args.field); const keyType = args.field.keyType; const valueType = args.field.parsedType.name; args.context.addUtil('isObject'); let fromSDK = null; // valueTypeType: string for identifier let valueTypeType = valueType; switch (valueType) { case 'string': fromSDK = t.callExpression(t.identifier('String'), [ t.identifier('value') ]); break; case 'int32': case 'uint32': valueTypeType = 'number'; fromSDK = t.callExpression(t.identifier('Number'), [ t.identifier('value') ]); break; case 'int64': case 'uint64': case 'sint64': case 'fixed64': case 'sfixed64': utils_1.TypeLong.addUtil(args.context); valueTypeType = utils_1.TypeLong.getPropType(args.context); fromSDK = utils_1.TypeLong.getFromValueWithArgs(args.context, t.tsAsExpression(t.identifier('value'), t.tsUnionType([ t.tsTypeReference(utils_1.TypeLong.getPropIdentifier(args.context)), t.tsStringKeyword() ]))); break; default: fromSDK = t.callExpression(t.memberExpression(t.identifier(valueType), t.identifier('fromSDK')), [ t.identifier('value') ]); } let wrapKey = null; let keyTypeType = null; switch (keyType) { case 'string': wrapKey = (a) => a; keyTypeType = t.tsStringKeyword(); break; case 'int64': case 'uint64': case 'sint64': case 'fixed64': case 'sfixed64': wrapKey = (a) => t.callExpression(t.identifier('Number'), [ a ]); utils_1.TypeLong.addUtil(args.context); keyTypeType = t.tsTypeReference(utils_1.TypeLong.getPropIdentifier(args.context)); break; case 'uint32': case 'int32': wrapKey = (a) => t.callExpression(t.identifier('Number'), [ a ]); keyTypeType = t.tsTypeReference(t.identifier('number')); break; default: throw new Error('keyHash requires new type. Ask maintainers.'); } return t.objectProperty(t.identifier(propName), t.conditionalExpression(t.callExpression(t.identifier('isObject'), [ t.memberExpression(t.identifier('object'), t.identifier(origName)) ]), (0, utils_1.callExpression)(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('entries')), [ t.memberExpression(t.identifier('object'), t.identifier(origName)) ]), t.identifier('reduce')), [ t.arrowFunctionExpression([ t.identifier('acc'), t.arrayPattern([ t.identifier('key'), t.identifier('value') ]) ], t.blockStatement([ t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier('acc'), wrapKey(t.identifier('key')), true), fromSDK)), t.returnStatement(t.identifier('acc')) ])), t.objectExpression([]) ], t.tsTypeParameterInstantiation([ t.tsTypeLiteral([ t.tsIndexSignature([ (0, utils_1.identifier)('key', t.tsTypeAnnotation(keyTypeType)) ], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(valueTypeType)))) ]) ])), t.objectExpression([]))); }, // codeIds: Array.isArray(object?.codeIds) ? object.codeIds.map((e: any) => Long.fromString(e)) : [], array(args, expr) { const { propName, origName } = (0, types_1.getFieldNames)(args.field); return t.objectProperty(t.identifier(propName), t.conditionalExpression(t.callExpression(t.memberExpression(t.identifier('Array'), t.identifier('isArray')), [ t.optionalMemberExpression(t.identifier('object'), t.identifier(origName), false, true) ]), t.callExpression(t.memberExpression(t.memberExpression(t.identifier('object'), t.identifier(origName)), t.identifier('map')), [ t.arrowFunctionExpression([ (0, utils_1.identifier)('e', t.tsTypeAnnotation(t.tsAnyKeyword())) ], expr, false) ]), t.arrayExpression([]))); } }; exports.arrayTypes = { scalar() { return t.identifier('e'); }, string() { return exports.arrayTypes.scalar(); }, bool() { return exports.arrayTypes.scalar(); }, bytes(args) { return exports.arrayTypes.scalar(); }, long() { return exports.arrayTypes.scalar(); }, uint64() { return exports.arrayTypes.scalar(); }, int64() { return exports.arrayTypes.scalar(); }, sint64() { return exports.arrayTypes.scalar(); }, fixed64() { return exports.arrayTypes.scalar(); }, sfixed64() { return exports.arrayTypes.scalar(); }, number() { return exports.arrayTypes.scalar(); }, uint32() { return exports.arrayTypes.scalar(); }, int32() { return exports.arrayTypes.scalar(); }, sint32() { return exports.arrayTypes.scalar(); }, fixed32() { return exports.arrayTypes.scalar(); }, sfixed32() { return exports.arrayTypes.scalar(); }, double() { return exports.arrayTypes.scalar(); }, float() { return exports.arrayTypes.scalar(); }, enum(args) { const fromSDKFuncName = args.context.getFromEnum(args.field); return t.callExpression(t.identifier(fromSDKFuncName), [ t.identifier('e') ]); }, // tokenInMaxs: Array.isArray(object?.tokenInMaxs) ? object.tokenInMaxs.map((e: any) => Coin.fromSDK(e)) : [] type(args) { let name = args.context.getTypeName(args.field); if (!args.context.options.aminoEncoding.useLegacyInlineEncoding && args.context.options.interfaces.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry && args.field.type === 'google.protobuf.Any' && args.field.options['(cosmos_proto.accepts_interface)']) { name = 'GlobalDecoderRegistry'; } return t.callExpression(t.memberExpression(t.identifier(name), t.identifier('fromSDK')), [ t.identifier('e') ]); } };