Codec Fallback on createType (along with pedantic) (#929)

* Codec Fallback on createType (along with pedantic)

* Updates for numbers (fix failing tests)

* Add isPedantic to linked
This commit is contained in:
Jaco Greeff
2019-06-12 16:45:45 +12:00
committed by Ian He
parent 0aefc24bc2
commit f9318fe013
11 changed files with 75 additions and 75 deletions
+4 -6
View File
@@ -24,14 +24,14 @@ function formattingTests (version: string, storage: Storage, encodedValues: [Str
beforeEach(() => {
provider = {
send: jest.fn((method, params) =>
Promise.resolve('0x0102')
Promise.resolve('0x01020000000000000000000000000000')
),
subscribe: jest.fn((type, method, params, subscription) =>
subscription(null, {
block: '0x1234',
changes: [
[ENC_ONE, '0x0102'],
[ENC_TWO, '0x0201']
[ENC_ONE, '0x01020000000000000000000000000000'],
[ENC_TWO, '0x02010000000000000000000000000000']
]
})
)
@@ -41,9 +41,7 @@ function formattingTests (version: string, storage: Storage, encodedValues: [Str
it('encodes key (with params), decoding response', () => {
return api.state
.getStorage(
[storage.balances.freeBalance, ADDR_ONE]
)
.getStorage([storage.balances.freeBalance, ADDR_ONE])
.then((value) => {
expect(
provider.send
+17 -32
View File
@@ -217,28 +217,23 @@ export default class Rpc implements RpcInterface {
// single return value (via state.getStorage), decode the value based on the
// outputType that we have specified. Fallback to Data on nothing
const type = key.outputType || 'Data';
const Clazz = createClass(type);
const meta = key.meta || EMPTY_META;
let created: Codec;
if (meta.type.isMap && meta.type.asMap.isLinked) {
// linked map
created = new Clazz(base);
} else {
created = meta.modifier.isOptional
? new Option(Clazz, isNull ? null : new Clazz(base))
: new Clazz(base);
return createType(type, base, true);
} else if (meta.modifier.isOptional) {
return new Option(
createClass(type),
isNull ? null : createType(type, base, true)
);
}
// TODO Check that the encoded created value matches that what was supplied
return created;
return createType(type, base, true);
}
private formatStorageSet (key: StorageKey, base: StorageChangeSet): Codec | undefined {
// Fallback to Data (i.e. just the encoding) if we don't have a specific type
const type = key.outputType || 'Data';
const Clazz = createClass(type);
// see if we have a result value for this specific key
const hexKey = key.toHex();
@@ -247,27 +242,17 @@ export default class Rpc implements RpcInterface {
) || { value: null };
const meta = key.meta || EMPTY_META;
// if we don't have a value, do not fill in the entry, it will be up to the
// caller to sort this out, either ignoring or having a cache for older values
let created: Codec | undefined;
if (value) {
if (meta.type.isMap && meta.type.asMap.isLinked) {
// linked map
created = new Clazz(value.unwrapOr(null));
} else if (meta.modifier.isOptional) {
// create option either with the existing value, or empty when
// there is no value returned
created = new Option(Clazz, value.isNone ? null : new Clazz(value.unwrap()));
} else {
// for `null` we fallback to the default value, or create an empty type,
// otherwise we return the actual value as retrieved
created = new Clazz(value.unwrapOr(meta.fallback));
}
// TODO Check that the encoded created value matches that what was supplied
if (!value) {
return;
} else if (meta.type.isMap && meta.type.asMap.isLinked) {
return createType(type, value.unwrapOr(null), true);
} else if (meta.modifier.isOptional) {
return new Option(
createClass(type),
value.isNone ? null : createType(type, value.unwrap(), true)
);
}
return created;
return createType(type, value.unwrapOr(meta.fallback), true);
}
}