Skip to content

Commit

Permalink
Improved type mapping
Browse files Browse the repository at this point in the history
Implemented a simple type lookup mapping file to extend the possible types
  • Loading branch information
MitchPierias committed Apr 19, 2019
1 parent 98a6efc commit 4a7566a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/contracts/typeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as globWithCallbacks from 'glob';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import mapTypes from './typeMap';

const glob = promisify(globWithCallbacks);

Expand All @@ -14,20 +15,15 @@ const pascalCase = (value: string) => {
type IndentedGeneratorLevel = { [key: string]: Array<string> | IndentedGeneratorLevel };
type GeneratorLevel = Array<string | IndentedGeneratorLevel>;

// Everything is a string right now.
const mapParameterType = (eosType: string) => {
/**
* Map Paramater Type
* @desc Parses a C++ type definition into a Typescript definition
* @param eosType
*/
export const mapParameterType = (eosType: string) => {
// Handle array types
const wrapper = eosType.endsWith('[]') ? 'Array' : undefined;
let type;

switch (eosType.replace('[]', '')) {
case 'bool':
type = 'boolean';
break;
default:
type = 'string';
break;
}

const type = mapTypes[eosType.replace('[]','')] || 'string';
if (wrapper) {
return `${wrapper}<${type}>`;
} else {
Expand Down
36 changes: 36 additions & 0 deletions src/contracts/typeMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Type Mappings
* @note I've kept this as a TypeScript file and not JSON cause I wasn't sure if we could
* // do some kind of fancy auto compiling from a TypeScript interface definition to
* // JSON object type map?
*/

interface TypeMapping {
[key:string]:string
}

const types:TypeMapping = {
'string':'string',
'bool':'boolean',
'name':'string|number',
'int8':'number',
'int16':'number',
'int32':'number',
'int64':'number',
'int128':'number',
'int256':'number',
'uint8':'number',
'uint16':'number',
'uint32':'number',
'uint64':'number',
'uint128':'number',
'uint256':'number',
'uint8_t':'number',
'uint16_t':'number',
'uint32_t':'number',
'uint64_t':'number',
'uint128_t':'number',
'uint256_t':'number'
}

export default types;

0 comments on commit 4a7566a

Please sign in to comment.