UNPKG

@cosmology/ast

Version:
94 lines (93 loc) 7.26 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.createRegisterAminoProtoMapping = exports.createRegisterObject = exports.createObjectWithMethods = void 0; const t = __importStar(require("@babel/types")); const from_partial_1 = require("./proto/from-partial"); const decode_1 = require("./proto/decode"); const encode_1 = require("./proto/encode"); const from_json_1 = require("./proto/from-json"); const to_json_1 = require("./proto/to-json"); const from_amino_1 = require("./proto/from-amino"); const to_amino_1 = require("./proto/to-amino"); const to_sdk_1 = require("./proto/to-sdk"); const from_sdk_1 = require("./proto/from-sdk"); const proto_1 = require("./proto"); const is_1 = require("./proto/is"); const utils_1 = require("../utils"); const utils_2 = require("@cosmology/utils"); const createObjectWithMethods = (context, name, proto) => { const methodsAndProps = [ (context.pluginValue('prototypes.addTypeUrlToObjects') || context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry')) && (0, proto_1.createTypeUrlProperty)(context, proto), (context.pluginValue('prototypes.addAminoTypeToObjects') || context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry')) && (0, proto_1.createAminoTypeProperty)(context, proto), context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry') && (0, is_1.isMethod)({ context, name, proto }), context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry') && context.pluginValue('useSDKTypes') && (0, is_1.isMethod)({ context, name, proto, methodName: "isSDK", getFieldName: utils_1.getSdkFieldName }), context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry') && context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') && (0, is_1.isMethod)({ context, name, proto, methodName: "isAmino", getFieldName: utils_1.getAminoFieldName }), context.pluginValue('prototypes.methods.encode') && (0, encode_1.encodeMethod)(context, name, proto), context.pluginValue('prototypes.methods.decode') && (0, decode_1.decodeMethod)(context, name, proto), context.pluginValue('prototypes.methods.fromJSON') && (0, from_json_1.fromJSONMethod)(context, name, proto), context.pluginValue('prototypes.methods.toJSON') && (0, to_json_1.toJSONMethod)(context, name, proto), context.pluginValue('prototypes.methods.fromPartial') && (0, from_partial_1.fromPartialMethod)(context, name, proto), context.pluginValue('prototypes.methods.fromSDK') && (0, from_sdk_1.fromSDKMethod)(context, name, proto), context.pluginValue('prototypes.methods.fromSDKJSON') && (0, proto_1.fromSDKJSONMethod)(context, name, proto), context.pluginValue('prototypes.methods.toSDK') && (0, to_sdk_1.toSDKMethod)(context, name, proto), (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.fromAmino')) && (0, from_amino_1.fromAminoJSONMethod)(context, name, proto), (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toAmino')) && (0, to_amino_1.toAminoJSONMethod)(context, name, proto), !context.pluginValue('aminoEncoding.disableMsgTypes') && (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.fromAmino')) && (0, from_amino_1.fromAminoMsgMethod)(context, name, proto), !context.pluginValue('aminoEncoding.disableMsgTypes') && (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toAmino')) && (0, to_amino_1.toAminoMsgMethod)(context, name, proto), (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.fromProto')) && (0, proto_1.fromProtoMsgMethod)(context, name, proto), (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toProto')) && (0, proto_1.toProtoMethod)(context, name, proto), (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toProto')) && (0, proto_1.toProtoMsgMethod)(context, name, proto), ].filter(Boolean); return t.exportNamedDeclaration(t.variableDeclaration('const', [ t.variableDeclarator(t.identifier(name), t.objectExpression(methodsAndProps)) ])); }; exports.createObjectWithMethods = createObjectWithMethods; const createRegisterObject = (context, name, proto) => { if (name === 'Any') { return; } context.addUtil("GlobalDecoderRegistry"); const typeUrl = (0, utils_2.getTypeUrl)(context.ref.proto, proto); if (!typeUrl) return; return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("register")), [ t.memberExpression(t.identifier(name), t.identifier("typeUrl")), t.identifier(name), ])); }; exports.createRegisterObject = createRegisterObject; const createRegisterAminoProtoMapping = (context, name, proto) => { context.addUtil("GlobalDecoderRegistry"); const str = (0, utils_2.getAminoTypeName)(context, context.ref.proto, proto); if (!str || str.startsWith('/')) return; return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("registerAminoProtoMapping")), [ t.memberExpression(t.identifier(name), t.identifier("aminoType")), t.memberExpression(t.identifier(name), t.identifier("typeUrl")), ])); }; exports.createRegisterAminoProtoMapping = createRegisterAminoProtoMapping;