@cosmology/ast
Version:
Cosmos TypeScript AST generation
589 lines (586 loc) • 28.3 kB
JavaScript
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.types = exports.encode = void 0;
const t = __importStar(require("@babel/types"));
const types_1 = require("../types");
const __1 = require("..");
const types_2 = require("../../types");
const utils_1 = require("../../../utils");
const notUndefined = (prop) => {
return t.binaryExpression('!==', t.memberExpression(t.identifier('message'), t.identifier(prop)), t.identifier('undefined'));
};
const ifNotUndefined = (prop, stmt) => {
return t.ifStatement(notUndefined(prop), t.blockStatement([
stmt
]));
};
const notEmptyString = (prop) => {
return t.binaryExpression('!==', t.memberExpression(t.identifier('message'), t.identifier(prop)), t.stringLiteral(''));
};
const nullCheckAndCondition = (inputProp, inputExpression) => {
return t.logicalExpression('&&', t.memberExpression(t.identifier('message'), t.identifier(inputProp)), inputExpression);
};
const lengthNotZero = (prop) => {
return t.binaryExpression('!==', t.memberExpression(t.memberExpression(t.identifier('message'), t.identifier(prop)), t.identifier('length')), t.numericLiteral(0));
};
const ifTrue = (prop) => {
return t.binaryExpression('===', t.memberExpression(t.identifier('message'), t.identifier(prop)), t.booleanLiteral(true));
};
const notZero = (prop) => {
return t.binaryExpression('!==', t.memberExpression(t.identifier('message'), t.identifier(prop)), t.numericLiteral(0));
};
// TODO research, shouldn't we AND these two tests?
const wrapOptional = (prop, test, isOptional, args) => {
if (isOptional || args?.context?.options?.prototypes?.allowEncodeDefaultScalars) {
return notUndefined(prop);
}
return test;
};
const scalarType = (num, prop, type, args) => {
let valueExpression = t.memberExpression(t.identifier('message'), t.identifier(prop));
switch (type) {
case 'int64':
case 'sint64':
case 'uint64':
case 'fixed64':
case 'sfixed64':
utils_1.TypeLong.addUtil(args.context);
break;
}
return t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier(type)), [
valueExpression
]))
]);
};
const customType = (num, prop, type, customType, args) => {
switch (customType) {
case "github.com/cosmos/cosmos-sdk/types.Dec":
case "cosmossdk.io/math.LegacyDec":
default:
args.context.addUtil("Decimal");
return t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier(type)), [
t.memberExpression(t.callExpression(t.memberExpression(t.identifier('Decimal'), t.identifier('fromUserInput')), [
t.memberExpression(t.identifier('message'), t.identifier(prop)),
t.numericLiteral(18)
]), t.identifier('atomics'))
]))
]);
}
};
exports.encode = {
string(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.string(num, prop, args.isOptional, args);
},
double(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.double(num, prop, args.isOptional, args);
},
float(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.float(num, prop, args.isOptional, args);
},
int32(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.int32(num, prop, args.isOptional, args);
},
sint32(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.sint32(num, prop, args.isOptional, args);
},
uint32(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.uint32(num, prop, args.isOptional, args);
},
fixed32(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.fixed32(num, prop, args.isOptional, args);
},
sfixed32(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.sfixed32(num, prop, args.isOptional, args);
},
int64(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.int64(num, prop, args.isOptional, args);
},
sint64(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.sint64(num, prop, args.isOptional, args);
},
uint64(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.uint64(num, prop, args.isOptional, args);
},
fixed64(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.fixed64(num, prop, args.isOptional, args);
},
sfixed64(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.sfixed64(num, prop, args.isOptional, args);
},
bool(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.bool(num, prop, args.isOptional, args);
},
type(args) {
const prop = args.field.name;
const name = args.context.getTypeName(args.field);
const num = (0, types_1.getTagNumber)(args.field);
let isAnyType = false;
if (!args.context.options.aminoEncoding.useLegacyInlineEncoding &&
args.context.options.interfaces.enabled &&
args.field.type === 'google.protobuf.Any' &&
args.field.options['(cosmos_proto.accepts_interface)']) {
isAnyType = true;
}
const isGlobalRegistry = args.context.options.interfaces?.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry;
return exports.types.type(num, prop, name, isAnyType, isGlobalRegistry);
},
enum(args) {
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.enum(args.context, num, args.field, args.isOptional, args.isOneOf);
},
bytes(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.bytes(num, prop, args.isOptional);
},
timestamp(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
const timestampFormat = args.context.pluginValue('prototypes.typingsFormat.timestamp');
switch (timestampFormat) {
case 'timestamp':
return exports.types.timestamp(num, prop);
case 'date':
default:
args.context.addUtil('toTimestamp');
return exports.types.timestampDate(num, prop);
}
},
duration(args) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
const durationFormat = args.context.pluginValue('prototypes.typingsFormat.duration');
switch (durationFormat) {
case 'string':
args.context.addUtil('toDuration');
return exports.types.duration(num, prop);
case 'duration':
default:
return exports.encode.type(args);
}
},
forkDelimArray(args, expr) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.forkDelimArray(num, prop, expr);
},
array(args, expr) {
const prop = args.field.name;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.array(num, prop, expr);
},
typeArray(args) {
const prop = args.field.name;
const name = args.context.getTypeName(args.field);
const num = (0, types_1.getTagNumber)(args.field);
let isAnyType = false;
if (!args.context.options.aminoEncoding.useLegacyInlineEncoding &&
args.context.options.interfaces.enabled &&
args.field.type === 'google.protobuf.Any' &&
args.field.options['(cosmos_proto.accepts_interface)']) {
isAnyType = true;
}
const isGlobalRegistry = args.context.options.interfaces?.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry;
return exports.types.typeArray(num, prop, name, isAnyType, isGlobalRegistry);
},
keyHash(args) {
const prop = args.field.name;
const name = args.typeName;
const num = (0, types_1.getTagNumber)(args.field);
return exports.types.keyHash(num, prop, name);
}
};
exports.types = {
/*
if (message.sender && message.sender !== "") {
writer.uint32(10).string(message.sender);
}
*/
string(num, prop, isOptional, args) {
const useCosmosSDKDec = args.context.pluginValue('prototypes.typingsFormat.customTypes.useCosmosSDKDec');
const isCosmosSDKDec = (args.field.options?.['(gogoproto.customtype)'] ==
'github.com/cosmos/cosmos-sdk/types.Dec') ||
(args.field.options?.['(gogoproto.customtype)'] ==
'cosmossdk.io/math.LegacyDec');
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notEmptyString(prop)) : notEmptyString(prop), isOptional, args), useCosmosSDKDec && isCosmosSDKDec
? customType(num, prop, 'string', args.field.options?.['(gogoproto.customtype)'], args)
: scalarType(num, prop, 'string'));
},
/*
if (message.doubleValue && message.doubleValue !== 0) {
writer.uint32(41).double(message.doubleValue);
}
*/
double(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notZero(prop)) : notZero(prop), isOptional, args), scalarType(num, prop, 'double'));
},
/*
if (message.floatValue && message.floatValue !== 0) {
writer.uint32(41).float(message.floatValue);
}
*/
float(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notZero(prop)) : notZero(prop), isOptional, args), scalarType(num, prop, 'float'));
},
// if (message.int32Value && message.int32Value !== 0) {
// writer.uint32(24).int32(message.int32Value);
// }
int32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notZero(prop)) : notZero(prop), isOptional, args), scalarType(num, prop, 'int32'));
},
// if (message.sint32Value && message.sint32Value !== 0) {
// writer.uint32(24).sint32(message.sint32Value);
// }
sint32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notZero(prop)) : notZero(prop), isOptional, args), scalarType(num, prop, 'sint32'));
},
// if (message.int32Value && message.int32Value !== 0) {
// writer.uint32(24).uint32(message.int32Value);
// }
uint32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notZero(prop)) : notZero(prop), isOptional, args), scalarType(num, prop, 'uint32'));
},
fixed32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notZero(prop)) : notZero(prop), isOptional, args), scalarType(num, prop, 'fixed32'));
},
sfixed32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, notZero(prop)) : notZero(prop), isOptional, args), scalarType(num, prop, 'sfixed32'));
},
// if (message.int64Value && !message.int64Value.isZero()) {
// writer.uint32(24).int64(message.int64Value);
// }
int64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, utils_1.TypeLong.getLongNotZero(prop, args.context)) : utils_1.TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, 'int64', args));
},
// if (message.sint64Value && !message.sint64Value.isZero()) {
// writer.uint32(24).sint64(message.sint64Value);
// }
sint64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, utils_1.TypeLong.getLongNotZero(prop, args.context)) : utils_1.TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, 'sint64', args));
},
// if (message.int64Value && !message.int64Value.isZero()) {
// writer.uint32(24).uint64(message.int64Value);
// }
uint64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, utils_1.TypeLong.getLongNotZero(prop, args.context)) : utils_1.TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, 'uint64', args));
},
fixed64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, utils_1.TypeLong.getLongNotZero(prop, args.context)) : utils_1.TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, 'fixed64', args));
},
sfixed64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue('prototypes.enforceNullCheck') ? nullCheckAndCondition(prop, utils_1.TypeLong.getLongNotZero(prop, args.context)) : utils_1.TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, 'sfixed64', args));
},
// if (message.disableMacros === true) {
// writer.uint32(32).bool(message.disableMacros);
// }
bool(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, ifTrue(prop), isOptional, args), scalarType(num, prop, 'bool'));
},
type(num, prop, name, isAnyType, isGlobalRegistry) {
let messageProp = t.memberExpression(t.identifier('message'), t.identifier(prop));
if (isAnyType) {
if (isGlobalRegistry) {
messageProp = t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("wrapAny")), [
messageProp
]);
}
else {
messageProp = t.tsAsExpression(messageProp, t.tsTypeReference(t.identifier('Any')));
}
}
return t.ifStatement(notUndefined(prop), t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(name), t.identifier('encode')), [
messageProp,
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('fork')), [])
]), t.identifier('ldelim')), []))
]));
},
// if (message.singleField !== 0) {
// writer.uint32(24).int32(message.singleField);
// }
enum(context, num, field, isOptional, isOneOf) {
const prop = field.name;
return t.ifStatement(wrapOptional(prop, t.binaryExpression('!==', t.memberExpression(t.identifier('message'), t.identifier(field.name)), (0, types_2.getDefaultTSTypeFromProtoType)(context, field, isOneOf)), isOptional), scalarType(num, prop, 'int32'));
},
/*
if (message.queryData.length !== 0) {
writer.uint32(18).bytes(message.queryData);
}
*/
bytes(num, prop, isOptional) {
return t.ifStatement(wrapOptional(prop, lengthNotZero(prop), isOptional), scalarType(num, prop, 'bytes'));
},
// if (message.periodReset !== undefined) {
// Timestamp.encode(toTimestamp(message.periodReset), writer.uint32(18).fork()).ldelim();
// }
timestamp(num, prop) {
return ifNotUndefined(prop, t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('Timestamp'), t.identifier('encode')), [
t.memberExpression(t.identifier('message'), t.identifier(prop)),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('fork')), [])
]), t.identifier('ldelim')), [])));
},
timestampDate(num, prop) {
return ifNotUndefined(prop, t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('Timestamp'), t.identifier('encode')), [
t.callExpression(t.identifier('toTimestamp'), [
t.memberExpression(t.identifier('message'), t.identifier(prop))
]),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('fork')), [])
]), t.identifier('ldelim')), [])));
},
// if (message.period !== undefined) {
// Duration.encode(toDuration(message.period), writer.uint32(18).fork()).ldelim();
// }
duration(num, prop) {
return t.ifStatement(notUndefined(prop), t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('Duration'), t.identifier('encode')), [
t.callExpression(t.identifier('toDuration'), [
t.memberExpression(t.identifier('message'), t.identifier(prop))
]),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('fork')), [])
]), t.identifier('ldelim')), []))
]));
},
/*
writer.uint32(10).fork();
for (const v of message.codeIds) {
writer.uint64(v);
}
writer.ldelim();
*/
forkDelimArray(num, prop, expr) {
return [
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('fork')), [])),
t.forOfStatement(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('v'), null)
]), t.memberExpression(t.identifier('message'), t.identifier(prop)), t.blockStatement([
expr
])),
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('ldelim')), []))
];
},
array(num, prop, expr) {
return [
t.forOfStatement(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('v'), null)
]), t.memberExpression(t.identifier('message'), t.identifier(prop)), t.blockStatement([
expr
]))
];
},
typeArray(num, prop, name, isAnyType, isGlobalRegistry) {
// "v!" just means it's NOT NULLABLE
let nestedProp = t.tsNonNullExpression(t.identifier('v'));
if (isAnyType) {
if (isGlobalRegistry) {
nestedProp = t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("wrapAny")), [
nestedProp
]);
}
else {
nestedProp = t.tsAsExpression(nestedProp, t.tsTypeReference(t.identifier('Any')));
}
}
return [
t.forOfStatement(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('v'), null)
]), t.memberExpression(t.identifier('message'), t.identifier(prop)), t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(name), t.identifier('encode')), [
nestedProp,
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('fork')), [])
]), t.identifier('ldelim')), []))
]))
];
},
// Object.entries(message.labels).forEach(([key, value]) => {
// LogEntry_LabelsEntry.encode({
// key: (key as any),
// value
// }, writer.uint32(106).fork()).ldelim();
// });
keyHash(num, prop, name) {
return t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('Object'), t.identifier('entries')), [
t.memberExpression(t.identifier('message'), t.identifier(prop))
]), t.identifier('forEach')), [
t.arrowFunctionExpression([
t.arrayPattern([
t.identifier('key'),
t.identifier('value')
]),
], t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier((0, __1.getKeyTypeEntryName)(name, prop)), t.identifier('encode')), [
t.objectExpression([
t.objectProperty(t.identifier('key'), t.tsAsExpression(t.identifier('key'), t.tsAnyKeyword())),
t.objectProperty(t.identifier('value'), t.identifier('value'), false, true)
]),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('fork')), [])
]), t.identifier('ldelim')), []))
]))
]));
}
};
exports.arrayTypes = {
double() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('double')), [
t.identifier('v')
]));
},
bool() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('bool')), [
t.identifier('v')
]));
},
float() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('float')), [
t.identifier('v')
]));
},
int32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('int32')), [
t.identifier('v')
]));
},
sint32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('sint32')), [
t.identifier('v')
]));
},
uint32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.identifier('v')
]));
},
fixed32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('fixed32')), [
t.identifier('v')
]));
},
sfixed32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('sfixed32')), [
t.identifier('v')
]));
},
long(type, args) {
let valueExpression = t.identifier('v');
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier(type)), [
valueExpression
]));
},
int64(args) {
return exports.arrayTypes.long('int64', args);
},
sint64(args) {
return exports.arrayTypes.long('sint64', args);
},
uint64(args) {
return exports.arrayTypes.long('uint64', args);
},
fixed64(args) {
return exports.arrayTypes.long('fixed64', args);
},
sfixed64(args) {
return exports.arrayTypes.long('sfixed64', args);
},
string(args) {
const useCosmosSDKDec = args.context.pluginValue('prototypes.typingsFormat.customTypes.useCosmosSDKDec');
const isCosmosSDKDec = (args.field.options?.['(gogoproto.customtype)'] ==
'github.com/cosmos/cosmos-sdk/types.Dec') ||
(args.field.options?.['(gogoproto.customtype)'] ==
'cosmossdk.io/math.LegacyDec');
const num = (0, types_1.getTagNumber)(args.field);
let valueExpression = t.tsNonNullExpression(t.identifier('v'));
if (useCosmosSDKDec && isCosmosSDKDec) {
args.context.addUtil("Decimal");
valueExpression = t.memberExpression(t.callExpression(t.memberExpression(t.identifier('Decimal'), t.identifier('fromUserInput')), [
valueExpression,
t.numericLiteral(18)
]), t.identifier('atomics'));
}
return t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('string')), [
valueExpression
]));
},
bytes(args) {
const num = (0, types_1.getTagNumber)(args.field);
return t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('writer'), t.identifier('uint32')), [
t.numericLiteral(num)
]), t.identifier('bytes')), [
t.tsNonNullExpression(t.identifier('v'))
]));
},
enum() {
return exports.arrayTypes.int32();
}
};
;