Commit bbffc1cd4f29e05f96fa2d1d15e71595b17bfc7f
use class methods for json stuff
Norton Wang committed on 4/27/2018, 4:33:23 PMParent: 5418f16a7749446c341174100524c5ddcec4a6a9
Files changed
index.js | changed |
tests/index.js | changed |
utils.js | changed |
index.js | ||
---|---|---|
@@ -46,8 +46,17 @@ | ||
46 | 46 … | toString () { |
47 | 47 … | return this.id.toString('hex') |
48 | 48 … | } |
49 | 49 … | |
50 … | + toJSON () { | |
51 … | + return `0x${this.toString()}` | |
52 … | + } | |
53 … | + | |
54 … | + static fromJSON (arg) { | |
55 … | + const { fromHex } = require('./utils') | |
56 … | + return new ID(fromHex(arg)) | |
57 … | + } | |
58 … | + | |
50 | 59 … | encodeCBOR (gen) { |
51 | 60 … | return gen.write(new cbor.Tagged(TAGS.id, this.id)) |
52 | 61 … | } |
53 | 62 … | } |
@@ -77,8 +86,33 @@ | ||
77 | 86 … | this.gas |
78 | 87 … | ])) |
79 | 88 … | } |
80 | 89 … | |
90 … | + toJSON (includeParams = false) { | |
91 … | + const json = { | |
92 … | + '@FunctionRef': { | |
93 … | + actorID: this.actorID.toJSON(), | |
94 … | + private: this.identifier[0], | |
95 … | + name: this.identifier[1], | |
96 … | + gas: this.gas | |
97 … | + } | |
98 … | + } | |
99 … | + if (includeParams) { | |
100 … | + json['@FunctionRef'].params = this.params | |
101 … | + } | |
102 … | + return json | |
103 … | + } | |
104 … | + | |
105 … | + static fromJSON (arg) { | |
106 … | + const data = arg['@FunctionRef'] | |
107 … | + return new FunctionRef({ | |
108 … | + identifier: [data.private, data.name], | |
109 … | + actorID: ID.fromJSON(data.actorID), | |
110 … | + params: data.params, | |
111 … | + gas: data.gas | |
112 … | + }) | |
113 … | + } | |
114 … | + | |
81 | 115 … | /** |
82 | 116 … | * Creates a copy of the funcRef |
83 | 117 … | * @returns {FunctionRef} |
84 | 118 … | */ |
@@ -119,8 +153,25 @@ | ||
119 | 153 … | actorID: this.id |
120 | 154 … | }) |
121 | 155 … | } |
122 | 156 … | |
157 … | + toJSON (includeExports = false) { | |
158 … | + const json = { | |
159 … | + '@ModuleRef': { | |
160 … | + id: this.id.toJSON() | |
161 … | + } | |
162 … | + } | |
163 … | + if (includeExports) { | |
164 … | + json['@ModuleRef'].exports = this.exports | |
165 … | + } | |
166 … | + return json | |
167 … | + } | |
168 … | + | |
169 … | + static fromJSON (arg) { | |
170 … | + const data = arg['@ModuleRef'] | |
171 … | + return new ModuleRef(data.exports, ID.fromJSON(data.id)) | |
172 … | + } | |
173 … | + | |
123 | 174 … | encodeCBOR (gen) { |
124 | 175 … | return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id])) |
125 | 176 … | } |
126 | 177 … | } |
tests/index.js | |||
---|---|---|---|
@@ -78,10 +78,13 @@ | |||
78 | 78 … | }), | |
79 | 79 … | Buffer.from([1, 2, 3, 4]) | |
80 | 80 … | ] | |
81 | 81 … | const json = utils.toJSON(obj) | |
82 | - t.deepEquals(JSON.stringify(json), '[{"@ModuleRef":{"id":"0x01"}},{"@FunctionRef":{"id":"0x01","private":false,"name":"main","gas":100}},"0x01020304"]') | ||
82 … | + t.deepEquals(JSON.stringify(json), '[{"@ModuleRef":{"id":"0x01"}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100}},"0x01020304"]') | ||
83 | 83 … | ||
84 … | + const jsonFull = utils.toJSON(obj, true) | ||
85 … | + t.deepEquals(JSON.stringify(jsonFull), '[{"@ModuleRef":{"id":"0x01","exports":{"name":["i32"]}}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100,"params":["i32"]}},"0x01020304"]') | ||
86 … | + | ||
84 | 87 … | const newObj = [ | |
85 | 88 … | new objects.ModuleRef(undefined, id), | |
86 | 89 … | new objects.FunctionRef({ | |
87 | 90 … | identifier: [false, 'main'], | |
@@ -91,8 +94,10 @@ | |||
91 | 94 … | Buffer.from([1, 2, 3, 4]) | |
92 | 95 … | ] | |
93 | 96 … | t.deepEquals(utils.fromJSON(json), newObj) | |
94 | 97 … | ||
98 … | + t.deepEquals(utils.fromJSON(jsonFull), obj) | ||
99 … | + | ||
95 | 100 … | const hashedId01 = utils.actorRefToId([0, 1]) | |
96 | 101 … | t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex')) | |
97 | 102 … | ||
98 | 103 … | t.end() | |
utils.js | ||
---|---|---|
@@ -1,37 +1,24 @@ | ||
1 | -const { ID, FunctionRef, ModuleRef, generateActorId, getType } = require('./') | |
1 … | +const { FunctionRef, ModuleRef, generateActorId, getType } = require('./') | |
2 | 2 … | |
3 | 3 … | const actorRefToId = ref => ref.reduce((parent, curr) => generateActorId({ parent, nonce: curr }), null) |
4 | 4 … | |
5 | 5 … | const toHex = arg => Buffer.isBuffer(arg) ? `0x${arg.toString('hex')}` : arg |
6 | 6 … | |
7 | 7 … | const fromHex = arg => typeof arg !== 'string' ? arg : Buffer.from(arg.slice(0, 2) === '0x' ? arg.slice(2) : arg, 'hex') |
8 | 8 … | |
9 | -const toJSON = arg => { | |
9 … | +const toJSON = (arg, includeOptional = false) => { | |
10 | 10 … | switch (getType(arg)) { |
11 | 11 … | case 'elem': |
12 | - return arg.map(toJSON) | |
12 … | + return arg.map(a => toJSON(a, includeOptional)) | |
13 | 13 … | case 'id': |
14 | - return toJSON(arg.id) | |
15 | 14 … | case 'func': |
16 | - return { | |
17 | - '@FunctionRef': { | |
18 | - id: toJSON(arg.actorID), | |
19 | - private: arg.identifier[0], | |
20 | - name: arg.identifier[1], | |
21 | - gas: arg.gas | |
22 | - } | |
23 | - } | |
24 | 15 … | case 'mod': |
25 | - return { | |
26 | - '@ModuleRef': { | |
27 | - id: toJSON(arg.id) | |
28 | - } | |
29 | - } | |
16 … | + return arg.toJSON(includeOptional) | |
30 | 17 … | case 'link': |
31 | 18 … | return { |
32 | 19 … | '@Link': { |
33 | - '/': toJSON(arg['/']) | |
20 … | + '/': toJSON(arg['/'], includeOptional) | |
34 | 21 … | } |
35 | 22 … | } |
36 | 23 … | case 'data': |
37 | 24 … | default: |
@@ -45,23 +32,11 @@ | ||
45 | 32 … | return arg.map(fromJSON) |
46 | 33 … | } |
47 | 34 … | |
48 | 35 … | if (arg['@FunctionRef']) { |
49 | - const data = arg['@FunctionRef'] | |
50 | - return new FunctionRef({ | |
51 | - identifier: [data.private, data.name], | |
52 | - actorID: new ID(fromJSON(data.id)), | |
53 | - params: data.params, | |
54 | - gas: data.gas | |
55 | - }) | |
36 … | + return FunctionRef.fromJSON(arg) | |
56 | 37 … | } else if (arg['@ModuleRef']) { |
57 | - const data = arg['@ModuleRef'] | |
58 | - return new ModuleRef(data.exports, new ID(fromJSON(data.id))) | |
59 | - } else if (arg['@Link']) { | |
60 | - const data = arg['@Link'] | |
61 | - return { | |
62 | - '/': fromJSON(data['/']) | |
63 | - } | |
38 … | + return ModuleRef.fromJSON(arg) | |
64 | 39 … | } |
65 | 40 … | } |
66 | 41 … | |
67 | 42 … | return fromHex(arg) |
Built with git-ssb-web