Ensure Range/RangeInclusive variant check does not fail on encoding (#4437)

This commit is contained in:
Jaco
2022-01-10 18:19:23 +02:00
committed by GitHub
parent 8cbc36550b
commit 0f67d3e54d
3 changed files with 19 additions and 17 deletions
+1
View File
@@ -4,6 +4,7 @@
Changes:
- Ensure `Range/RangeInclusive` variant check does not fail on encoding
- Ensure non-option calls in api-contract are marked as `@deprecated`
@@ -159,6 +159,8 @@ export function encodeTypeDef (CodecRegistry: CodecRegistry, typeDef: TypeDef):
: encodeType(CodecRegistry, typeDef);
}
export function withTypeString (CodecRegistry: CodecRegistry, typeDef: Omit<TypeDef, 'type'>): TypeDef {
return objectSpread({}, typeDef, { type: encodeType(CodecRegistry, typeDef as TypeDef, false) });
export function withTypeString (CodecRegistry: CodecRegistry, typeDef: Omit<TypeDef, 'type'> & { type?: string }): TypeDef {
return objectSpread({}, typeDef, {
type: encodeType(CodecRegistry, typeDef as TypeDef, false)
});
}
@@ -509,14 +509,15 @@ export class PortableRegistry extends Struct implements ILookup {
}
#extractComposite (lookupIndex: number, { params, path }: SiType, { fields }: SiTypeDefComposite): TypeDef {
const specialVariant = path[0].toString();
const pathFirst = path[0].toString();
const pathLast = path[path.length - 1].toString();
if (path.length === 1 && specialVariant === 'BTreeMap') {
if (path.length === 1 && pathFirst === 'BTreeMap') {
return withTypeString(this.registry, {
info: TypeDefInfo.BTreeMap,
sub: params.map(({ type }) => this.#createSiDef(type.unwrap()))
});
} else if (['Range', 'RangeInclusive'].includes(specialVariant)) {
} else if (['Range', 'RangeInclusive'].includes(pathFirst)) {
return withTypeString(this.registry, {
info: TypeDefInfo.Range,
sub: fields.map(({ name, type, typeName }, index) =>
@@ -530,19 +531,17 @@ export class PortableRegistry extends Struct implements ILookup {
typeName.isSome
? { typeName: sanitize(typeName.unwrap()) }
: null
))
)),
type: pathFirst
});
} else if (['WrapperKeepOpaque', 'WrapperOpaque'].includes(pathLast)) {
return withTypeString(this.registry, {
info: pathLast === 'WrapperKeepOpaque'
? TypeDefInfo.WrapperKeepOpaque
: TypeDefInfo.WrapperOpaque,
sub: this.#createSiDef(params[0].type.unwrap()),
type: pathLast
});
} else if (path.length) {
const last = path[path.length - 1].toString();
if (['WrapperKeepOpaque', 'WrapperOpaque'].includes(last)) {
return withTypeString(this.registry, {
info: last === 'WrapperKeepOpaque'
? TypeDefInfo.WrapperKeepOpaque
: TypeDefInfo.WrapperOpaque,
sub: this.#createSiDef(params[0].type.unwrap())
});
}
}
return PATHS_SET.some((p) => matchParts(p, path))