Adjust PortableRegistry name extraction (#4946)

* Adjust PortableRegistry name extraction

* Adjust count

* Adjust

* Adjust .every check

* Adjust ink! test outcomes
This commit is contained in:
Jaco
2022-06-17 17:20:26 +03:00
committed by GitHub
parent 7da9c5c20b
commit 7c5f82404f
3 changed files with 47 additions and 44 deletions
@@ -252,7 +252,7 @@
{ {
"info": "Enum", "info": "Enum",
"lookupIndex": 13, "lookupIndex": 13,
"type": "{\"_enum\":{\"Vacant\":\"InkStorageCollectionsStashVacantEntry\",\"Occupied\":\"MultisigPlainMultisigPlainTransaction\"}}", "type": "{\"_enum\":{\"Vacant\":\"InkStorageCollectionsStashVacantEntry\",\"Occupied\":\"MultisigPlainTransaction\"}}",
"docs": [], "docs": [],
"namespace": "ink_storage::collections::stash::Entry", "namespace": "ink_storage::collections::stash::Entry",
"sub": [ "sub": [
@@ -269,7 +269,7 @@
"docs": [], "docs": [],
"info": "Si", "info": "Si",
"lookupIndex": 14, "lookupIndex": 14,
"lookupName": "MultisigPlainMultisigPlainTransaction", "lookupName": "MultisigPlainTransaction",
"type": "Lookup14", "type": "Lookup14",
"index": 1, "index": 1,
"name": "Occupied" "name": "Occupied"
@@ -279,7 +279,7 @@
{ {
"info": "Struct", "info": "Struct",
"lookupIndex": 14, "lookupIndex": 14,
"lookupName": "MultisigPlainMultisigPlainTransaction", "lookupName": "MultisigPlainTransaction",
"type": "{\"callee\":\"AccountId\",\"selector\":\"[u8;4]\",\"input\":\"Bytes\",\"transferredValue\":\"u128\",\"gasLimit\":\"u64\"}", "type": "{\"callee\":\"AccountId\",\"selector\":\"[u8;4]\",\"input\":\"Bytes\",\"transferredValue\":\"u128\",\"gasLimit\":\"u64\"}",
"docs": [], "docs": [],
"namespace": "multisig_plain::multisig_plain::Transaction", "namespace": "multisig_plain::multisig_plain::Transaction",
@@ -418,7 +418,7 @@
{ {
"info": "Tuple", "info": "Tuple",
"lookupIndex": 21, "lookupIndex": 21,
"type": "(u32,MultisigPlainMultisigPlainConfirmationStatus)", "type": "(u32,MultisigPlainConfirmationStatus)",
"docs": [], "docs": [],
"namespace": "", "namespace": "",
"sub": [ "sub": [
@@ -433,7 +433,7 @@
"docs": [], "docs": [],
"info": "Si", "info": "Si",
"lookupIndex": 22, "lookupIndex": 22,
"lookupName": "MultisigPlainMultisigPlainConfirmationStatus", "lookupName": "MultisigPlainConfirmationStatus",
"type": "Lookup22" "type": "Lookup22"
} }
] ]
@@ -441,7 +441,7 @@
{ {
"info": "Enum", "info": "Enum",
"lookupIndex": 22, "lookupIndex": 22,
"lookupName": "MultisigPlainMultisigPlainConfirmationStatus", "lookupName": "MultisigPlainConfirmationStatus",
"type": "{\"_enum\":{\"Confirmed\":\"Null\",\"ConfirmationsNeeded\":\"u32\"}}", "type": "{\"_enum\":{\"Confirmed\":\"Null\",\"ConfirmationsNeeded\":\"u32\"}}",
"docs": [], "docs": [],
"namespace": "multisig_plain::multisig_plain::ConfirmationStatus", "namespace": "multisig_plain::multisig_plain::ConfirmationStatus",
@@ -23,8 +23,8 @@
{ {
"info": "Enum", "info": "Enum",
"lookupIndex": 4, "lookupIndex": 4,
"lookupName": "EnumExampleEnumExampleVariant", "lookupName": "EnumExampleVariant",
"type": "{\"_enum\":{\"None\":\"Null\",\"Weekday\":\"EnumExampleEnumExampleWeekday\",\"TupleMaybeSigned\":\"EnumExampleEnumExampleTupleMaybeSigned\",\"NamedMaybeSigned\":\"EnumExampleEnumExampleNamedMaybeSigned\",\"Color\":\"EnumExampleEnumExampleColor\"}}", "type": "{\"_enum\":{\"None\":\"Null\",\"Weekday\":\"EnumExampleWeekday\",\"TupleMaybeSigned\":\"EnumExampleTupleMaybeSigned\",\"NamedMaybeSigned\":\"EnumExampleNamedMaybeSigned\",\"Color\":\"EnumExampleColor\"}}",
"docs": [], "docs": [],
"namespace": "enum_example::enum_example::Variant", "namespace": "enum_example::enum_example::Variant",
"sub": [ "sub": [
@@ -38,7 +38,7 @@
"docs": [], "docs": [],
"info": "Si", "info": "Si",
"lookupIndex": 5, "lookupIndex": 5,
"lookupName": "EnumExampleEnumExampleWeekday", "lookupName": "EnumExampleWeekday",
"type": "Lookup5", "type": "Lookup5",
"index": 1, "index": 1,
"name": "Weekday" "name": "Weekday"
@@ -47,7 +47,7 @@
"docs": [], "docs": [],
"info": "Si", "info": "Si",
"lookupIndex": 6, "lookupIndex": 6,
"lookupName": "EnumExampleEnumExampleTupleMaybeSigned", "lookupName": "EnumExampleTupleMaybeSigned",
"type": "Lookup6", "type": "Lookup6",
"index": 2, "index": 2,
"name": "TupleMaybeSigned" "name": "TupleMaybeSigned"
@@ -56,7 +56,7 @@
"docs": [], "docs": [],
"info": "Si", "info": "Si",
"lookupIndex": 7, "lookupIndex": 7,
"lookupName": "EnumExampleEnumExampleNamedMaybeSigned", "lookupName": "EnumExampleNamedMaybeSigned",
"type": "Lookup7", "type": "Lookup7",
"index": 3, "index": 3,
"name": "NamedMaybeSigned" "name": "NamedMaybeSigned"
@@ -65,7 +65,7 @@
"docs": [], "docs": [],
"info": "Si", "info": "Si",
"lookupIndex": 8, "lookupIndex": 8,
"lookupName": "EnumExampleEnumExampleColor", "lookupName": "EnumExampleColor",
"type": "Lookup8", "type": "Lookup8",
"index": 4, "index": 4,
"name": "Color" "name": "Color"
@@ -75,7 +75,7 @@
{ {
"info": "Enum", "info": "Enum",
"lookupIndex": 5, "lookupIndex": 5,
"lookupName": "EnumExampleEnumExampleWeekday", "lookupName": "EnumExampleWeekday",
"type": "{\"_enum\":[\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\",\"Sunday\"]}", "type": "{\"_enum\":[\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\",\"Sunday\"]}",
"docs": [], "docs": [],
"namespace": "enum_example::enum_example::Weekday", "namespace": "enum_example::enum_example::Weekday",
@@ -127,7 +127,7 @@
{ {
"info": "Enum", "info": "Enum",
"lookupIndex": 6, "lookupIndex": 6,
"lookupName": "EnumExampleEnumExampleTupleMaybeSigned", "lookupName": "EnumExampleTupleMaybeSigned",
"type": "{\"_enum\":{\"Signed\":\"i32\",\"Unsigned\":\"u32\"}}", "type": "{\"_enum\":{\"Signed\":\"i32\",\"Unsigned\":\"u32\"}}",
"docs": [], "docs": [],
"namespace": "enum_example::enum_example::TupleMaybeSigned", "namespace": "enum_example::enum_example::TupleMaybeSigned",
@@ -155,7 +155,7 @@
{ {
"info": "Enum", "info": "Enum",
"lookupIndex": 7, "lookupIndex": 7,
"lookupName": "EnumExampleEnumExampleNamedMaybeSigned", "lookupName": "EnumExampleNamedMaybeSigned",
"type": "{\"_enum\":{\"Signed\":\"{\\\"value\\\":\\\"i32\\\"}\",\"Unsigned\":\"{\\\"value\\\":\\\"u32\\\"}\"}}", "type": "{\"_enum\":{\"Signed\":\"{\\\"value\\\":\\\"i32\\\"}\",\"Unsigned\":\"{\\\"value\\\":\\\"u32\\\"}\"}}",
"docs": [], "docs": [],
"namespace": "enum_example::enum_example::NamedMaybeSigned", "namespace": "enum_example::enum_example::NamedMaybeSigned",
@@ -197,7 +197,7 @@
{ {
"info": "Enum", "info": "Enum",
"lookupIndex": 8, "lookupIndex": 8,
"lookupName": "EnumExampleEnumExampleColor", "lookupName": "EnumExampleColor",
"type": "{\"_enum\":{\"Red\":\"Null\",\"Blue\":\"Null\",\"Green\":\"Null\",\"Yellow\":\"Null\",\"Rgb\":\"{\\\"r\\\":\\\"u8\\\",\\\"g\\\":\\\"u8\\\",\\\"b\\\":\\\"u8\\\"}\",\"Rgba\":\"{\\\"r\\\":\\\"u8\\\",\\\"g\\\":\\\"u8\\\",\\\"b\\\":\\\"u8\\\",\\\"a\\\":\\\"u8\\\"}\"}}", "type": "{\"_enum\":{\"Red\":\"Null\",\"Blue\":\"Null\",\"Green\":\"Null\",\"Yellow\":\"Null\",\"Rgb\":\"{\\\"r\\\":\\\"u8\\\",\\\"g\\\":\\\"u8\\\",\\\"b\\\":\\\"u8\\\"}\",\"Rgba\":\"{\\\"r\\\":\\\"u8\\\",\\\"g\\\":\\\"u8\\\",\\\"b\\\":\\\"u8\\\",\\\"a\\\":\\\"u8\\\"}\"}}",
"docs": [], "docs": [],
"namespace": "enum_example::enum_example::Color", "namespace": "enum_example::enum_example::Color",
@@ -164,44 +164,48 @@ function getAliasPath (path: SiPath): string | null {
/** @internal */ /** @internal */
function extractNameFlat (portable: PortableType[], lookupIndex: number, params: SiTypeParameter[], path: AnyString[], isInternal = false): Extract | null { function extractNameFlat (portable: PortableType[], lookupIndex: number, params: SiTypeParameter[], path: AnyString[], isInternal = false): Extract | null {
const last = path.length - 1; const count = path.length;
// if we have no path or determined as a wrapper, we just skip it // if we have no path or determined as a wrapper, we just skip it
if (last === -1 || WRAPPERS.includes(path[last].toString())) { if (count === 0 || WRAPPERS.includes(path[count - 1].toString())) {
return null; return null;
} }
const parts: string[] = []; const camels = new Array<string>(count);
let name = ''; const lowers = new Array<string>(count);
for (let i = 0; i <= last; i++) { // initially just create arrays of the camelCase and lowercase path
const p = stringPascalCase( // parts - we will check these to extract the final values. While
// we have 2 loops here, we also don't do the same operation twice
for (let i = 0; i < count; i++) {
const c = stringPascalCase(
isInternal isInternal
? path[i].replace('pallet_', '') ? path[i].replace('pallet_', '')
: path[i] : path[i]
); );
const l = p.toLowerCase(); const l = c.toLowerCase();
if ( camels[i] = c;
( lowers[i] = l;
// Remove ::{generic, misc, pallet, traits, types}:: }
i !== 1 ||
!PATH_RM_INDEX_1.includes(l) let name = '';
) &&
( for (let i = 0; i < count; i++) {
// sp_runtime::generic::digest::Digest -> sp_runtime::generic::Digest const l = lowers[i];
// sp_runtime::multiaddress::MultiAddress -> sp_runtime::MultiAddress
i === last || // Remove ::{generic, misc, pallet, traits, types}::
l !== path[i + 1].toLowerCase() if (i !== 1 || !PATH_RM_INDEX_1.includes(l)) {
) // sp_runtime::generic::digest::Digest -> sp_runtime::generic::Digest
) { // sp_runtime::multiaddress::MultiAddress -> sp_runtime::MultiAddress
parts.push(p); if (l !== lowers[i + 1]) {
name += p; name += camels[i];
}
} }
} }
// do magic for RawOrigin lookup, e.g. pallet_collective::RawOrigin // do magic for RawOrigin lookup, e.g. pallet_collective::RawOrigin
if (parts.length === 2 && parts[1] === 'RawOrigin' && params.length === 2 && params[1].type.isSome) { if (camels[1] === 'RawOrigin' && count === 2 && params.length === 2 && params[1].type.isSome) {
const instanceType = portable[params[1].type.unwrap().toNumber()]; const instanceType = portable[params[1].type.unwrap().toNumber()];
if (instanceType.type.path.length === 2) { if (instanceType.type.path.length === 2) {
@@ -301,12 +305,11 @@ function removeDupeNames (lookup: PortableRegistry, portable: PortableType[], na
// not just the first // not just the first
// find the first parameter that yields differences // find the first parameter that yields differences
const paramIdx = params.findIndex(({ type }, index) => const paramIdx = params.findIndex(({ type }, index) =>
allSame.every(({ params }) =>
params[index].type.isSome
) &&
allSame.every(({ params }, aIndex) => allSame.every(({ params }, aIndex) =>
aIndex === 0 || params[index].type.isSome && (
!params[index].type.eq(type) aIndex === 0 ||
!params[index].type.eq(type)
)
) )
); );