@cosmology/ast
Version:
Cosmos TypeScript AST generation
296 lines (295 loc) • 11.1 kB
JavaScript
import * as t from '@babel/types';
import { callExpression, identifier, TypeLong } from '../../../utils';
import { getFieldNames } from '../../types';
export const fromSDK = {
scalar(args) {
const { propName, origName } = getFieldNames(args.field);
return t.objectProperty(t.identifier(propName), t.optionalMemberExpression(t.identifier('object'), t.identifier(origName), false, true));
},
string(args) {
return fromSDK.scalar(args);
},
number(args) {
return fromSDK.scalar(args);
},
double(args) {
return fromSDK.scalar(args);
},
float(args) {
return fromSDK.scalar(args);
},
int32(args) {
return fromSDK.scalar(args);
},
sint32(args) {
return fromSDK.scalar(args);
},
uint32(args) {
return fromSDK.scalar(args);
},
fixed32(args) {
return fromSDK.scalar(args);
},
sfixed32(args) {
return fromSDK.scalar(args);
},
bool(args) {
return fromSDK.scalar(args);
},
long(args) {
return fromSDK.scalar(args);
},
int64(args) {
return fromSDK.scalar(args);
},
uint64(args) {
return fromSDK.scalar(args);
},
sint64(args) {
return fromSDK.scalar(args);
},
fixed64(args) {
return fromSDK.scalar(args);
},
sfixed64(args) {
return fromSDK.scalar(args);
},
type(args) {
const { propName, origName } = 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 } = 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 fromSDK.scalar(args);
},
duration(args) {
return 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 fromSDK.type(args);
case 'date':
default:
return fromSDK.timestampDate(args);
}
},
timestampDate(args) {
const { propName, origName } = 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 } = 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':
TypeLong.addUtil(args.context);
valueTypeType = TypeLong.getPropType(args.context);
fromSDK = TypeLong.getFromValueWithArgs(args.context, t.tsAsExpression(t.identifier('value'), t.tsUnionType([
t.tsTypeReference(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
]);
TypeLong.addUtil(args.context);
keyTypeType = t.tsTypeReference(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))
]), 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([
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 } = 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([
identifier('e', t.tsTypeAnnotation(t.tsAnyKeyword()))
], expr, false)
]), t.arrayExpression([])));
}
};
export const arrayTypes = {
scalar() {
return t.identifier('e');
},
string() {
return arrayTypes.scalar();
},
bool() {
return arrayTypes.scalar();
},
bytes(args) {
return arrayTypes.scalar();
},
long() {
return arrayTypes.scalar();
},
uint64() {
return arrayTypes.scalar();
},
int64() {
return arrayTypes.scalar();
},
sint64() {
return arrayTypes.scalar();
},
fixed64() {
return arrayTypes.scalar();
},
sfixed64() {
return arrayTypes.scalar();
},
number() {
return arrayTypes.scalar();
},
uint32() {
return arrayTypes.scalar();
},
int32() {
return arrayTypes.scalar();
},
sint32() {
return arrayTypes.scalar();
},
fixed32() {
return arrayTypes.scalar();
},
sfixed32() {
return arrayTypes.scalar();
},
double() {
return arrayTypes.scalar();
},
float() {
return 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')
]);
}
};