Skip to content

Commit

Permalink
Use destructuring for optional parameters to mapParameterType and upd…
Browse files Browse the repository at this point in the history
…ate tests to pass.
  • Loading branch information
thekevinbrown authored and dallasjohnson committed Oct 9, 2019
1 parent 0077f38 commit 5832aca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
30 changes: 17 additions & 13 deletions src/contracts/typeGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { eosIsReady, startEos, buildAll, stopContainer } from '../cli/utils';
import { mapParameterType } from './typeGenerator';

/**
* Javascript only supports number, so CPP integer types need to be mapped accordingly
* Javascript only supports 64 bit floating point numbers natively, so CPP integer types need to be mapped accordingly
*/
const numberTypes = [
'int8',
Expand Down Expand Up @@ -43,52 +43,56 @@ const stringNumberTypes = [
describe('type generator', function() {
context('map parameter types', function() {
it(`should map 'string' to 'string'`, function() {
assert.equal(mapParameterType('string'), 'string', `'string' types should map to 'string'`);
assert.equal(
mapParameterType({ eosType: 'string' }),
'string',
`'string' types should map to 'string'`
);
});

it(`should map 'bool' to 'boolean'`, function() {
assert.equal(mapParameterType('bool'), 'boolean');
assert.equal(mapParameterType({ eosType: 'bool' }), 'boolean');
});

context('eos types', function() {
it(`should map name types to 'string|number'`, function() {
stringNumberTypes.map(type =>
stringNumberTypes.map(eosType =>
assert.equal(
mapParameterType(type),
mapParameterType({ eosType }),
'string|number',
`'${type}' type should map to 'string' or 'number'`
`'${eosType}' type should map to 'string' or 'number'`
)
);
});

it(`should map 'checksum' to 'string'`, function() {
assert.equal(
mapParameterType('checksum'),
mapParameterType({ eosType: 'checksum' }),
'string',
`'checksum' type should map to 'string'`
);
});
});

context('big numbers', function() {
numberTypes.forEach(type => {
it(`should map '${type}' to 'number'`, function() {
numberTypes.forEach(eosType => {
it(`should map '${eosType}' to 'number'`, function() {
assert.equal(
mapParameterType(type),
mapParameterType({ eosType }),
'number',
`Integer type '${type}' should map to 'number'`
`Integer type '${eosType}' should map to 'number'`
);
});
});
});

context('complex types', function() {
it(`should handle array types`, function() {
assert.equal(mapParameterType('bool[]'), 'Array<boolean>');
assert.equal(mapParameterType({ eosType: 'bool[]' }), 'Array<boolean>');
});

xit(`should handle vector types`, function() {
assert.equal(mapParameterType('vector<string>'), 'Array<string>');
assert.equal(mapParameterType({ eosType: 'vector<string>' }), 'Array<string>');
});
});
});
Expand Down
39 changes: 30 additions & 9 deletions src/contracts/typeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,26 @@ type GeneratorLevel = Array<string | IndentedGeneratorLevel>;
* Parses a C++ type definition into a Typescript definition
* @author Kevin Brown <github.com/thekevinbrown>
* @author Mitch Pierias <github.com/MitchPierias>
* @param eosType
* @param eosType The type from the ABI we want to map over to a Typescript type
* @param contractName (optional) The name of the contract to prefix the mapped type with if this is a contract struct type
* @param contractStructs (optional) Structs in the contract used to match against, falling back to built in types if not found
*/
export const mapParameterType = (contractName: string, contractStructs: any, eosType: string) => {
export const mapParameterType = ({
eosType,
contractName,
contractStructs,
}: {
eosType: string;
contractName?: string;
contractStructs?: any;
}) => {
// Handle array types
const wrapper = eosType.endsWith('[]') ? 'Array' : undefined;
const parameterType = eosType.replace('[]', '');
const type = contractStructs[parameterType]
? pascalCase(contractName) + pascalCase(parameterType)
: mapTypes[parameterType] || 'string';
const type =
contractStructs && contractName && contractStructs[parameterType]
? pascalCase(contractName) + pascalCase(parameterType)
: mapTypes[parameterType] || 'string';
if (wrapper) {
return `${wrapper}<${type}>`;
} else {
Expand Down Expand Up @@ -93,8 +104,10 @@ export const generateTypes = async (contractIdentifier: string) => {
// Generate structs from ABI
for (const key in contractStructs) {
const structInterface = {
[`export interface ${pascalCase(contractName)}${pascalCase(key)}`]: contractStructs[key].fields.map(
(field: any) => `${field.name}: ${mapParameterType(contractName, {}, field.type)};`
[`export interface ${pascalCase(contractName)}${pascalCase(key)}`]: contractStructs[
key
].fields.map(
(field: any) => `${field.name}: ${mapParameterType({ contractName, eosType: field.type })};`
),
};

Expand All @@ -105,7 +118,12 @@ export const generateTypes = async (contractIdentifier: string) => {
const generatedContractActions = contractActions.map((action: any) => {
// With a function for each action
const parameters = contractStructs[action.name].fields.map(
(parameter: any) => `${parameter.name}: ${mapParameterType(contractName, contractStructs, parameter.type)}`
(parameter: any) =>
`${parameter.name}: ${mapParameterType({
contractName,
contractStructs,
eosType: parameter.type,
})}`
);
// Optional parameter at the end on every contract method.
parameters.push('options?: { from?: Account, auths?: ActorPermission[] }');
Expand All @@ -115,7 +133,10 @@ export const generateTypes = async (contractIdentifier: string) => {
// Generate tables
const generatedTables = contractTables.map(
(table: any) =>
`${camelCase(table.name) + 'Table'}(options?: GetTableRowsOptions): Promise<TableRowsResult<${pascalCase(contractName)}${pascalCase(table.type)}>>;`
`${camelCase(table.name) +
'Table'}(options?: GetTableRowsOptions): Promise<TableRowsResult<${pascalCase(
contractName
)}${pascalCase(table.type)}>>;`
);
// Generate the contract interface with actions and tables
const contractInterface = {
Expand Down

0 comments on commit 5832aca

Please sign in to comment.