UNPKG

@cosmology/ast

Version:
140 lines (139 loc) 5.49 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.makeAminoTypeInterface = exports.renderAminoField = void 0; const t = __importStar(require("@babel/types")); const utils_1 = require("../utils"); const utils_2 = require("./utils"); const proto_1 = require("../../proto"); ; const renderAminoField = ({ context, field, currentProtoPath, isOptional }) => { const args = { context, field, currentProtoPath, isOptional }; if (field.rule === 'repeated') { switch (field.parsedType.type) { case 'Type': return utils_2.aminoInterface.typeArray(args); case 'Enum': return utils_2.aminoInterface.enumArray(args); default: return utils_2.aminoInterface.array(args); } } // special "native" types... // above Type,Enum since they're Types switch (field.type) { // TODO check can we just // make pieces optional and avoid hard-coding this type? case 'ibc.core.client.v1.Height': case 'Height': return utils_2.aminoInterface.height(args); case 'Timestamp': case 'google.protobuf.Timestamp': return utils_2.aminoInterface.timestamp(args); case 'Duration': case 'google.protobuf.Duration': return utils_2.aminoInterface.duration(args); case 'Any': case 'google.protobuf.Any': switch (field.options?.['(cosmos_proto.accepts_interface)']) { case 'cosmos.crypto.PubKey': return utils_2.aminoInterface.pubkey(args); } } switch (field.parsedType.type) { case 'Type': return utils_2.aminoInterface.type(args); case 'Enum': return utils_2.aminoInterface.enum(args); } // scalar types... switch (field.type) { case 'string': return utils_2.aminoInterface.defaultType(args); case 'int64': case 'sint64': case 'uint64': case 'fixed64': case 'sfixed64': return utils_2.aminoInterface.long(args); case 'double': case 'bool': case 'bytes': case 'int32': case 'sint32': case 'uint32': case 'fixed32': case 'sfixed32': return utils_2.aminoInterface.defaultType(args); // // TODO check can we just // // make pieces optional and avoid hard-coding this type? // case 'ibc.core.client.v1.Height': // case 'Height': // return aminoInterface.height(args); // case 'Timestamp': // case 'google.protobuf.Timestamp': // return aminoInterface.timestamp(args); // case 'Duration': // case 'google.protobuf.Duration': // return aminoInterface.duration(args); default: return utils_2.aminoInterface.defaultType(args); } }; exports.renderAminoField = renderAminoField; ; const makeAminoTypeInterface = ({ context, proto }) => { context.addUtil('AminoMsg'); const TypeName = proto.name; const aminoType = (0, utils_1.getAminoTypeName)(context, context.ref.proto, proto); const oneOfs = (0, proto_1.getOneOfs)(proto); const fields = (0, utils_1.protoFieldsToArray)(proto).map((field) => { const isOneOf = oneOfs.includes(field.name); const isOptional = (0, proto_1.getFieldOptionality)(context, field, isOneOf); const aminoField = (0, exports.renderAminoField)({ context, field, currentProtoPath: context.ref.filename, isOptional }); return { ctx: context, field: aminoField }; }); const annotation = !context.options.aminoEncoding.useLegacyInlineEncoding ? t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName + 'Amino'))) : t.tsTypeAnnotation(t.tsTypeLiteral(fields.map(({ field }) => field))); return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(TypeName + 'AminoType'), null, [t.tsExpressionWithTypeArguments(t.identifier('AminoMsg'))], t.tSInterfaceBody([ t.tSPropertySignature(t.identifier('type'), t.tsTypeAnnotation(t.tSLiteralType(t.stringLiteral(aminoType)))), t.tSPropertySignature(t.identifier('value'), annotation) ]))); }; exports.makeAminoTypeInterface = makeAminoTypeInterface;