@graphprotocol/graph-cli
Version:
CLI for building for and deploying to The Graph
57 lines (56 loc) • 3.29 kB
JavaScript
import immutable from 'immutable';
import TYPE_CONVERSIONS from './conversions.js';
// Conversion utilities
const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
const conversions = TYPE_CONVERSIONS.getIn([fromTypeSystem, toTypeSystem]);
if (conversions === undefined) {
throw new Error(`Conversions from '${fromTypeSystem}' to '${toTypeSystem}' are not supported`);
}
return conversions;
};
const objectifyConversion = (fromTypeSystem, toTypeSystem, conversion) => {
return immutable.fromJS({
from: {
typeSystem: fromTypeSystem,
type: conversion.get(0),
},
to: {
typeSystem: toTypeSystem,
type: conversion.get(1),
},
convert: conversion.get(2),
});
};
const findConversionFromType = (fromTypeSystem, toTypeSystem, fromType) => {
const conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem);
const conversion = conversions.find(conversion => typeof conversion.get(0) === 'string'
? conversion.get(0) === fromType
: !!fromType.match(conversion.get(0)));
if (conversion === undefined) {
throw new Error(`Conversion from '${fromTypeSystem}' to '${toTypeSystem}' for ` +
`source type '${fromType}' is not supported`);
}
return objectifyConversion(fromTypeSystem, toTypeSystem, conversion);
};
const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
const conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem);
const conversion = conversions.find(conversion => typeof conversion.get(1) === 'string'
? conversion.get(1) === toType
: !!toType.match(conversion.get(1)));
if (conversion === undefined) {
throw new Error(`Conversion from '${fromTypeSystem}' to '${toTypeSystem}' for ` +
`target type '${toType}' is not supported`);
}
return objectifyConversion(fromTypeSystem, toTypeSystem, conversion);
};
// High-level type system API
export const ascTypeForProtocol = (protocol, protocolType) => findConversionFromType(protocol, 'AssemblyScript', protocolType).getIn(['to', 'type']);
// TODO: this can be removed/replaced by the function above
export const ascTypeForEthereum = (ethereumType) => ascTypeForProtocol('ethereum', ethereumType);
export const ethereumTypeForAsc = (ascType) => findConversionFromType('AssemblyScript', 'ethereum', ascType).getIn(['to', 'type']);
export const ethereumToAsc = (code, ethereumType, internalType) => findConversionFromType('ethereum', 'AssemblyScript', ethereumType).get('convert')(code, internalType);
export const ethereumFromAsc = (code, ethereumType) => findConversionToType('AssemblyScript', 'ethereum', ethereumType).get('convert')(code);
export const ascTypeForValue = (valueType) => findConversionFromType('Value', 'AssemblyScript', valueType).getIn(['to', 'type']);
export const valueTypeForAsc = (ascType) => findConversionFromType('AssemblyScript', 'Value', ascType).getIn(['to', 'type']);
export const valueToAsc = (code, valueType) => findConversionFromType('Value', 'AssemblyScript', valueType).get('convert')(code);
export const valueFromAsc = (code, valueType) => findConversionToType('AssemblyScript', 'Value', valueType).get('convert')(code);