Skip to content

Commit

Permalink
add ability to provide global default mapping; add docs for same
Browse files Browse the repository at this point in the history
  • Loading branch information
matmar10 committed Dec 12, 2019
1 parent f634cdb commit cf69a74
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Truffle Object Mapper

Quickly and painless dump all the values from a deployed contract.

See [Object Mapper](https://github.com/wankdanker/node-object-mapper#readme) for full details on output mapping syntax.

## Quick Usage

```JavaScript
Expand All @@ -18,7 +20,7 @@ const mapper = new Mapper();
})();
```

## Customize Output for Types
### Customize Output for Types

```JavaScript
const mapper = new Mapper({
Expand All @@ -32,11 +34,41 @@ const mapper = new Mapper({
const values = await mapper.map('Token', '0xf793db07d9952ff75d5371cceb98c4380277503f');
```

## Change Output Mapping
### Provide Custom Output Mapping

See [Object Mapper](https://github.com/wankdanker/node-object-mapper#readme) for full details on output mapping.

```JavaScript

// all calls to .map will apply these transformations
const mapper = new Mapper({
mapping: {
// example: 'State' is an integer representing a struct
// this will output two keys:
// 'state' - the integer
// 'stateName' - the string representing the struct
State: [
{
key: 'state',
transform: function (val) {
const index = Number(val.toString());
const states = ['Funding', 'Active', 'Matured'];
return states[index];
},
},
'stateName',
],
},
});


```

### Change Output Mapping Each Time

```JavaScript

// this custom mapping only applies to this call of .map
const values = await mapper.map('Token', '0xf793db07d9952ff75d5371cceb98c4380277503f', {
// map the property 'name' to the output key 'tokenName'
name: 'tokenName',
Expand All @@ -62,3 +94,6 @@ const values = await mapper.map('Token', '0xf793db07d9952ff75d5371cceb98c4380277
}],
});
```

## API Methods

38 changes: 38 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const Mapper = require('./');
const Web3 = require('web3');

function bNToString(val) {
return Web3.utils.BN(val).toString();
}

const mapper = new Mapper({
mapping: {
State: [
{
key: 'state',
transform: function (val) {
const index = Number(val.toString());
const states = ['Funding', 'Active', 'Matured'];
return states[index];
},
},
'stateName',
],
},
types: {
int8: bNToString,
uint256: bNToString,
},
workingDirectory: __dirname,
});

(async () => {
try {
const values = await mapper.map('Token', '0x468d834b0FAc4B9D8B2E90bE1cE35A891Ff96Ae9');
console.log(values);
} catch (err) {
console.error(err);
}
})();
8 changes: 5 additions & 3 deletions lib/object-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TruffleObjectMapper {
this.options = merge(defaults, options);
this.artifacts = {};
this._types = merge(TruffleObjectMapper.types, options.types || {});
this._mapping = this.options.mapping || {};
}

getArtifact(contractName) {
Expand Down Expand Up @@ -88,11 +89,12 @@ class TruffleObjectMapper {
const value = await instance[node.name]();
values[key] = value;
const [output] = node.outputs;
map[key] = this.buildOutputMapping(key, output.type, mapping[key]);
const customMapping = this._mapping[key] ?
this._mapping[key] :
mapping[key];
map[key] = this.buildOutputMapping(key, output.type, customMapping);
});

console.log(map);

return objectMapper(values, map);
}

Expand Down

0 comments on commit cf69a74

Please sign in to comment.