UNPKG

@cosmology/ast

Version:
177 lines (176 loc) 6.72 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.fromAminoJsonMethod = exports.fromAminoParseField = void 0; const t = __importStar(require("@babel/types")); const utils_1 = require("../../../utils"); const utils_2 = require("../utils"); const utils_3 = require("./utils"); const proto_1 = require("../../proto"); const needsImplementation = (name, field) => { throw new Error(`need to implement fromAmino (${field.type} rules[${field.rule}] name[${name}])`); }; const fromAminoParseField = ({ context, field, currentProtoPath, scope: previousScope, fieldPath: previousFieldPath, nested, isOptional }) => { const scope = [field.name, ...previousScope]; const fieldPath = [field, ...previousFieldPath]; const args = { context, field, currentProtoPath, scope, fieldPath, nested, isOptional }; // arrays if (field.rule === 'repeated') { switch (field.type) { case 'string': return utils_3.fromAmino.string(args); case 'int64': case 'sint64': case 'uint64': case 'fixed64': case 'sfixed64': return utils_3.fromAmino.scalarArray(args, utils_3.arrayTypes.long); case 'double': case 'float': case 'int32': case 'sint32': case 'uint32': case 'fixed32': case 'sfixed32': case 'bool': case 'bytes': return utils_3.fromAmino.defaultType(args); } switch (field.parsedType.type) { case 'Type': return utils_3.fromAmino.typeArray(args); case 'Enum': return utils_3.fromAmino.enumArray(args); case 'cosmos.base.v1beta1.Coin': return utils_3.fromAmino.arrayFrom(args); } return needsImplementation(field.name, field); } // casting special types if (field.type === 'google.protobuf.Any') { switch (field.options?.['(cosmos_proto.accepts_interface)']) { case 'cosmos.crypto.PubKey': return utils_3.fromAmino.pubkey(args); } } // special types... switch (field.type) { case 'Timestamp': case 'google.protobuf.Timestamp': return utils_3.fromAmino.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 utils_3.fromAmino.height(args); case 'Duration': case 'google.protobuf.Duration': return utils_3.fromAmino.duration(args); default: } // Types/Enums switch (field.parsedType.type) { case 'Type': return utils_3.fromAmino.type(args); case 'Enum': return utils_3.fromAmino.enum(args); } if (field.type === 'bytes') { // bytes [RawContractMessage] if (field.options?.['(gogoproto.casttype)'] === 'RawContractMessage') { return utils_3.fromAmino.rawBytes(args); } // bytes [WASMByteCode] // TODO use a better option for this in proto source if (field.options?.['(gogoproto.customname)'] === 'WASMByteCode') { return utils_3.fromAmino.wasmByteCode(args); } } // scalar types... switch (field.type) { case 'string': return utils_3.fromAmino.string(args); case 'int64': case 'sint64': case 'uint64': case 'fixed64': case 'sfixed64': return utils_3.fromAmino.long(args); case 'double': case 'float': case 'int32': case 'sint32': case 'uint32': case 'fixed32': case 'sfixed32': case 'bool': return utils_3.fromAmino.defaultType(args); case 'bytes': return utils_3.fromAmino.defaultType(args); default: return utils_3.fromAmino.defaultType(args); } }; exports.fromAminoParseField = fromAminoParseField; const fromAminoJsonMethod = ({ context, proto }) => { const fromAminoParams = t.objectPattern(Object.keys(proto.fields).map((field) => t.objectProperty(t.identifier(context.aminoCaseField(proto.fields[field])), t.identifier(context.aminoCaseField(proto.fields[field])), false, true))); fromAminoParams.typeAnnotation = t.tsTypeAnnotation(t.tsIndexedAccessType(t.tsTypeReference(t.identifier(proto.name + 'AminoType')), t.tsLiteralType(t.stringLiteral('value')))); const oneOfs = (0, proto_1.getOneOfs)(proto); const fields = (0, utils_2.protoFieldsToArray)(proto).map((field) => { const isOneOf = oneOfs.includes(field.name); const isOptional = (0, proto_1.getFieldOptionality)(context, field, isOneOf); const aminoField = (0, exports.fromAminoParseField)({ context, field, currentProtoPath: context.ref.filename, scope: [], fieldPath: [], nested: 0, isOptional }); return { ctx: context, field: aminoField }; }); const ctxs = fields.map(({ ctx }) => ctx); ctxs.forEach(ctx => { // console.log('imports, ', ctx.imports) }); return (0, utils_1.arrowFunctionExpression)([ fromAminoParams ], t.blockStatement([ t.returnStatement(t.objectExpression(fields.map(({ field }) => field))) ]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(proto.name)))); }; exports.fromAminoJsonMethod = fromAminoJsonMethod;