Add support for BoundedVec (#3415)

* Add support for BoundedVec

* Update packages/types/src/create/sanitize.ts

* Cleanup sanitize duplication

* Additional cleanups

* Update packages/types/src/create/sanitize.ts

* Box wrap
This commit is contained in:
Jaco
2021-04-21 08:52:56 +02:00
committed by GitHub
parent 7a6077301f
commit 8bce5b4a09
2 changed files with 44 additions and 47 deletions
File diff suppressed because one or more lines are too long
+43 -46
View File
@@ -16,12 +16,14 @@ const mappings: Mapper[] = [
alias(['VecDeque<'], 'Vec<', false),
// <T::Balance as HasCompact>
cleanupCompact(),
// Change BoundedVec<Type, Size> to Vec<Type>
removeBoundedVec(),
// Remove all the trait prefixes
removeTraits(),
// remove PairOf<T> -> (T, T)
removePairOf(),
// remove boxing, `Box<Proposal>` -> `Proposal`
removeWrap('Box'),
removeWrap('Box<'),
// remove generics, `MisbehaviorReport<Hash, BlockNumber>` -> `MisbehaviorReport`
removeGenerics(),
// alias String -> Text (compat with jsonrpc methods)
@@ -61,18 +63,17 @@ export function findClosing (value: string, start: number): number {
}
export function alias (src: string[], dest: string, withChecks = true): Mapper {
return (value: string): string => {
return src.reduce((value, src): string => {
return value
return (value: string): string =>
src.reduce((value, src): string =>
value
.replace(
new RegExp(`(^${src}|${BOX_PRECEDING.map((box) => `\\${box}${src}`).join('|')})`, 'g'),
(src): string =>
withChecks && BOX_PRECEDING.includes(src[0])
? `${src[0]}${dest}`
: dest
);
}, value);
};
), value
);
}
export function cleanupCompact (): Mapper {
@@ -94,9 +95,29 @@ export function cleanupCompact (): Mapper {
}
export function flattenSingleTuple (): Mapper {
return (value: string): string => {
return value.replace(/\(([^,]+)\)/, '$1');
};
return (value: string) =>
value.replace(/\(([^,]+)\)/, '$1');
}
function replaceTagWith (value: string, matcher: string, replacer: (inner: string) => string): string {
for (let index = 0; index < value.length; index++) {
if (value.substr(index, matcher.length) === matcher) {
const start = index + matcher.length;
const end = findClosing(value, start);
value = `${value.substr(0, index)}${replacer(value.substr(start, end - start))}${value.substr(end + 1)}`;
}
}
return value;
}
// remove the BoundedVec wrappers
export function removeBoundedVec (): Mapper {
const replacer = (inner: string) => `Vec<${inner.split(',')[0]}>`;
return (value: string) =>
replaceTagWith(value, 'BoundedVec<', replacer);
}
export function removeColons (): Mapper {
@@ -156,25 +177,16 @@ export function removeGenerics (): Mapper {
// remove the PairOf wrappers
export function removePairOf (): Mapper {
return (value: string): string => {
for (let index = 0; index < value.length; index++) {
if (value.substr(index, 7) === 'PairOf<') {
const start = index + 7;
const end = findClosing(value, start);
const type = value.substr(start, end - start);
const replacer = (inner: string) => `(${inner},${inner})`;
value = `${value.substr(0, index)}(${type},${type})${value.substr(end + 1)}`;
}
}
return value;
};
return (value: string) =>
replaceTagWith(value, 'PairOf<', replacer);
}
// remove the type traits
export function removeTraits (): Mapper {
return (value: string): string => {
return value
return (value: string): string =>
value
// remove all whitespaces
.replace(/\s/g, '')
// anything `T::<type>` to end up as `<type>`
@@ -187,34 +199,19 @@ export function removeTraits (): Mapper {
.replace(/<LookupasStaticLookup>/g, 'Lookup')
// replace `<...>::Type`
.replace(/::Type/g, '');
};
}
// remove wrapping values, i.e. Box<Proposal> -> Proposal
export function removeWrap (_check: string): Mapper {
const check = `${_check}<`;
export function removeWrap (check: string): Mapper {
const replacer = (inner: string) => inner;
return (value: string): string => {
let index = 0;
while (index !== -1) {
index = value.indexOf(check);
if (index !== -1) {
const start = index + check.length;
const end = findClosing(value, start);
value = `${value.substr(0, index)}${value.substr(start, end - start)}${value.substr(end + 1)}`;
}
}
return value;
};
return (value: string) =>
replaceTagWith(value, check, replacer);
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function sanitize (value: String | string, options?: SanitizeOptions): string {
return mappings.reduce((result, fn): string => {
return fn(result, options);
}, value.toString()).trim();
return mappings.reduce((result, fn): string =>
fn(result, options), value.toString()
).trim();
}