Skip to content

Commit 0f67d3e

Browse files
authored
Ensure Range/RangeInclusive variant check does not fail on encoding (#4437)
1 parent 8cbc365 commit 0f67d3e

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
Changes:
66

7+
- Ensure `Range/RangeInclusive` variant check does not fail on encoding
78
- Ensure non-option calls in api-contract are marked as `@deprecated`
89

910

packages/types-create/src/util/encodeTypes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ export function encodeTypeDef (CodecRegistry: CodecRegistry, typeDef: TypeDef):
159159
: encodeType(CodecRegistry, typeDef);
160160
}
161161

162-
export function withTypeString (CodecRegistry: CodecRegistry, typeDef: Omit<TypeDef, 'type'>): TypeDef {
163-
return objectSpread({}, typeDef, { type: encodeType(CodecRegistry, typeDef as TypeDef, false) });
162+
export function withTypeString (CodecRegistry: CodecRegistry, typeDef: Omit<TypeDef, 'type'> & { type?: string }): TypeDef {
163+
return objectSpread({}, typeDef, {
164+
type: encodeType(CodecRegistry, typeDef as TypeDef, false)
165+
});
164166
}

packages/types/src/metadata/PortableRegistry/PortableRegistry.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,15 @@ export class PortableRegistry extends Struct implements ILookup {
509509
}
510510

511511
#extractComposite (lookupIndex: number, { params, path }: SiType, { fields }: SiTypeDefComposite): TypeDef {
512-
const specialVariant = path[0].toString();
512+
const pathFirst = path[0].toString();
513+
const pathLast = path[path.length - 1].toString();
513514

514-
if (path.length === 1 && specialVariant === 'BTreeMap') {
515+
if (path.length === 1 && pathFirst === 'BTreeMap') {
515516
return withTypeString(this.registry, {
516517
info: TypeDefInfo.BTreeMap,
517518
sub: params.map(({ type }) => this.#createSiDef(type.unwrap()))
518519
});
519-
} else if (['Range', 'RangeInclusive'].includes(specialVariant)) {
520+
} else if (['Range', 'RangeInclusive'].includes(pathFirst)) {
520521
return withTypeString(this.registry, {
521522
info: TypeDefInfo.Range,
522523
sub: fields.map(({ name, type, typeName }, index) =>
@@ -530,19 +531,17 @@ export class PortableRegistry extends Struct implements ILookup {
530531
typeName.isSome
531532
? { typeName: sanitize(typeName.unwrap()) }
532533
: null
533-
))
534+
)),
535+
type: pathFirst
536+
});
537+
} else if (['WrapperKeepOpaque', 'WrapperOpaque'].includes(pathLast)) {
538+
return withTypeString(this.registry, {
539+
info: pathLast === 'WrapperKeepOpaque'
540+
? TypeDefInfo.WrapperKeepOpaque
541+
: TypeDefInfo.WrapperOpaque,
542+
sub: this.#createSiDef(params[0].type.unwrap()),
543+
type: pathLast
534544
});
535-
} else if (path.length) {
536-
const last = path[path.length - 1].toString();
537-
538-
if (['WrapperKeepOpaque', 'WrapperOpaque'].includes(last)) {
539-
return withTypeString(this.registry, {
540-
info: last === 'WrapperKeepOpaque'
541-
? TypeDefInfo.WrapperKeepOpaque
542-
: TypeDefInfo.WrapperOpaque,
543-
sub: this.#createSiDef(params[0].type.unwrap())
544-
});
545-
}
546545
}
547546

548547
return PATHS_SET.some((p) => matchParts(p, path))

0 commit comments

Comments
 (0)