Small readability/typing fixes on metadata (#1312)

* Small readability/typing fixes on metadata

* Object destructuring

* Alphabetical ordering
This commit is contained in:
Jaco Greeff
2019-08-15 09:59:15 +02:00
committed by GitHub
parent 88f707fa12
commit 411d432193
6 changed files with 77 additions and 87 deletions
+3 -3
View File
@@ -11,7 +11,7 @@ import { RuntimeModuleMetadata } from './Modules';
import { Text } from '../../primitive';
import { FunctionMetadata } from '../v1/Calls';
import { EventMetadata } from '../v1/Events';
import MetadataV1, { ModuleMetadata } from '../v1/Metadata';
import MetadataV1, { ModuleMetadata as ModuleMetadataV1 } from '../v1/Metadata';
import { StorageFunctionMetadata } from '../v1/Storage';
function toV1Calls (modul: RuntimeModuleMetadata): Option<FunctionMetadata> {
@@ -42,13 +42,13 @@ function toV1Storage (modul: RuntimeModuleMetadata): Option<StorageFunctionMetad
*/
export default function toV1 (metadataV0: MetadataV0): MetadataV1 {
return new MetadataV1({
modules: metadataV0.modules.map((modul): ModuleMetadata => {
modules: metadataV0.modules.map((modul): ModuleMetadataV1 => {
// The prefix of this module (capitalized)
const prefix = modul.storage.isSome
? modul.storage.unwrap().prefix.toString()
: stringUpperFirst(modul.prefix.toString()); // If this module doesn't have storage, we just assume the prefix is the name capitalized
return new ModuleMetadata({
return new ModuleMetadataV1({
name: modul.prefix, // Not capitalized
prefix, // Capitalized
storage: toV1Storage(modul),
+18 -21
View File
@@ -6,8 +6,9 @@ import { Option, Vec } from '../../codec';
import MetadataV3 from './Metadata';
import StorageHasher from '../../primitive/StorageHasher';
import MetadataV4 from '../v4';
import { StorageFunctionMetadata as StorageFunctionMetadataV3 } from '../v3/Storage';
import { ModuleMetadata as ModuleMetadataV4 } from '../v4/Metadata';
import { DoubleMapType, MapType, StorageFunctionMetadata, StorageFunctionType } from '../v4/Storage';
import { StorageFunctionMetadata as StorageFunctionMetadataV3 } from './Storage';
/**
* Convert V3 StorageFunction to V4 StorageFunction
@@ -36,11 +37,11 @@ function toV4StorageFunction (storageFn: StorageFunctionMetadataV3): StorageFunc
}), 2];
return new StorageFunctionMetadata({
name: name,
modifier: modifier,
type: new StorageFunctionType(newType, index),
fallback: fallback,
documentation: documentation
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(newType, index)
});
}
@@ -50,20 +51,16 @@ function toV4StorageFunction (storageFn: StorageFunctionMetadataV3): StorageFunc
*/
export default function toV4 (metadataV3: MetadataV3): MetadataV4 {
return new MetadataV4({
// FIXME, this needs typing, not any
modules: metadataV3.modules.map((modul): any => {
return {
name: modul.name,
prefix: modul.prefix,
storage: modul.storage.isSome
? new Option(
Vec.with(StorageFunctionMetadata),
modul.storage.unwrap().map(toV4StorageFunction)
)
: undefined,
calls: modul.calls,
events: modul.events
};
})
modules: metadataV3.modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV4 =>
new ModuleMetadataV4({
calls,
events,
name,
prefix,
storage: storage.isSome
? new Option(Vec.with(StorageFunctionMetadata), storage.unwrap().map(toV4StorageFunction))
: undefined
})
)
});
}
+35 -38
View File
@@ -2,37 +2,38 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import { Option, Vec } from '../../codec';
import Text from '../../primitive/Text';
import MetadataV4 from './Metadata';
import StorageHasher from '../../primitive/StorageHasher';
import MetadataV5 from '../v5';
import { StorageFunctionMetadata as StorageFunctionMetadataV4 } from '../v4/Storage';
import { ModuleMetadata as ModuleMetadataV5 } from '../v5/Metadata';
import { DoubleMapType, StorageFunctionMetadata, StorageFunctionType } from '../v5/Storage';
import { StorageFunctionMetadata as StorageFunctionMetadataV4 } from './Storage';
const hasherMap: Map<string, string> = new Map([
['blake2_128', 'Blake2_128'],
['blake2_256', 'Blake2_256'],
['twox_128', 'Twox128'],
['twox_256', 'Twox256'],
['twox_64_concat', 'Twox64Concat']
]);
function toStorageHasher (text: Text): StorageHasher {
const mapped = hasherMap.get(text.toString());
assert(mapped, `Invalid Storage hasher: ${text.toString()}`);
return new StorageHasher(mapped);
}
/**
* Convert V4 StorageFunction to V5 StorageFunction
*/
function toV5StorageFunction (storageFn: StorageFunctionMetadataV4): StorageFunctionMetadata {
const { documentation, fallback, modifier, name, type } = storageFn;
function toStorageHasher (text: Text): StorageHasher {
switch (text.toString()) {
case 'blake2_128':
return new StorageHasher('Blake2_128');
case 'blake2_256':
return new StorageHasher('Blake2_256');
case 'twox_128':
return new StorageHasher('Twox128');
case 'twox_256':
return new StorageHasher('Twox256');
case 'twox_64_concat':
return new StorageHasher('Twox64Concat');
default:
throw new Error(`Invalid Storage hasher: ${text.toString()}`);
}
}
const [newType, index] = type.isPlainType
? [type, 0]
: type.isMap
@@ -46,11 +47,11 @@ function toV5StorageFunction (storageFn: StorageFunctionMetadataV4): StorageFunc
}), 2];
return new StorageFunctionMetadata({
name: name,
modifier: modifier,
type: new StorageFunctionType(newType, index),
fallback: fallback,
documentation: documentation
documentation,
fallback,
name,
modifier,
type: new StorageFunctionType(newType, index)
});
}
@@ -60,20 +61,16 @@ function toV5StorageFunction (storageFn: StorageFunctionMetadataV4): StorageFunc
*/
export default function toV5 (metadataV4: MetadataV4): MetadataV5 {
return new MetadataV5({
// FIXME, this needs typing, not any
modules: metadataV4.modules.map((modul): any => {
return {
name: modul.name,
prefix: modul.prefix,
storage: modul.storage.isSome
? new Option(
Vec.with(StorageFunctionMetadata),
modul.storage.unwrap().map(toV5StorageFunction)
)
modules: metadataV4.modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV5 =>
new ModuleMetadataV5({
name,
prefix,
storage: storage.isSome
? new Option(Vec.with(StorageFunctionMetadata), storage.unwrap().map(toV5StorageFunction))
: undefined,
calls: modul.calls,
events: modul.events
};
})
calls,
events
})
)
});
}
+11 -11
View File
@@ -4,6 +4,7 @@
import MetadataV5 from './Metadata';
import MetadataV6 from '../v6';
import { ModuleMetadata as ModuleMetadataV6 } from '../v6/Metadata';
/**
* Convert from MetadataV5 to MetadataV6
@@ -11,16 +12,15 @@ import MetadataV6 from '../v6';
*/
export default function toV6 (metadataV5: MetadataV5): MetadataV6 {
return new MetadataV6({
// FIXME, this needs typing, not any
modules: metadataV5.modules.map((modul): any => {
return {
name: modul.name,
prefix: modul.prefix,
storage: modul.storage,
calls: modul.calls,
events: modul.events,
constants: []
};
})
modules: metadataV5.modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV6 =>
new ModuleMetadataV6({
calls,
constants: [],
events,
name,
prefix,
storage
})
)
});
}
+10 -13
View File
@@ -4,6 +4,7 @@
import MetadataV6 from './Metadata';
import MetadataV7 from '../v7';
import { ModuleMetadata as ModuleMetadataV7 } from '../v7/Metadata';
import { StorageMetadata as StorageV7 } from '../v7/Storage';
/**
@@ -11,20 +12,16 @@ import { StorageMetadata as StorageV7 } from '../v7/Storage';
*/
export default function toV7 ({ modules }: MetadataV6): MetadataV7 {
return new MetadataV7({
// FIXME, this needs typing, not any
modules: modules.map(({ calls, constants, events, name, prefix, storage }): any => {
return {
modules: modules.map(({ calls, constants, events, name, prefix, storage }): ModuleMetadataV7 =>
new ModuleMetadataV7({
calls,
constants,
events,
name,
storage: storage.isSome
? new StorageV7({
prefix,
items: storage.unwrap()
})
: null,
calls,
events,
constants
};
})
? new StorageV7({ prefix, items: storage.unwrap() })
: null
})
)
});
}
@@ -5,7 +5,6 @@
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Text from '../../primitive/Text';
import { DoubleMapType, MapType, PlainType, StorageEntryMetadata, StorageEntryMetadataValue, StorageEntryModifier, StorageEntryType } from '../v6/Storage';
// Re-export classes that haven't changed between V6 and V7