Skip to content

Commit aec1ac4

Browse files
authored
fix incorrect constmap submap implementation (#809)
1 parent 80bbcd9 commit aec1ac4

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

core/base/__tests__/mapping.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const channelId = [
2828
["Osmosis", { a: "d", b: 4, c: BigInt(4) }],
2929
],
3030
],
31-
["Devnet", []],
3231
] as const satisfies RoArray<
3332
readonly [
3433
Network,
@@ -56,9 +55,8 @@ const networkChainCosmwasmChainIds = [
5655
>;
5756

5857
describe("Mapping tests", function () {
59-
let cm;
6058
it("should correctly create a mapping", function () {
61-
cm = constMap(channelId, [0, [1, 2]]);
59+
const cm = constMap(channelId, [0, [1, 2]]);
6260
const vals = cm("Mainnet");
6361
expect(vals.length).toEqual(2);
6462
});
@@ -67,6 +65,16 @@ describe("Mapping tests", function () {
6765
const vals = chainIdToNetworkChainPair("evmos_9000-4");
6866
expect(vals).toEqual(["Testnet", "Evmos"]);
6967
});
68+
it("should correctly create a submap", function () {
69+
const mainMap = constMap(networkChainCosmwasmChainIds);
70+
const subMap = mainMap.subMap("Mainnet");
71+
expect(subMap.has("Cosmoshub")).toBeTruthy();
72+
expect(subMap.has("Evmos")).toBeTruthy();
73+
expect(subMap.get("Cosmoshub")).toEqual("cosmoshub-4");
74+
expect(subMap.get("Evmos")).toEqual("evmos_9001-2");
75+
expect(subMap("Cosmoshub")).toEqual("cosmoshub-4");
76+
expect(subMap("Evmos")).toEqual("evmos_9001-2");
77+
});
7078
});
7179

7280
describe("Contract tests", function () {

core/base/src/utils/mapping.ts

+19-17
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ type GenericMappingFunc<M extends Mapped, D extends number> =
338338
type SubMap<M extends Mapped, D extends number, K extends PropertyKey> =
339339
K extends keyof M
340340
? M[K] extends Mapped
341-
? ConstMap<M[K], Depth[D]>
341+
? ConstMapImpl<M[K], Depth[D]>
342342
: never
343343
: never;
344344

@@ -365,14 +365,14 @@ type HasGetFuncs<M extends Mapped, D extends number> =
365365
? { readonly has: (...args: P) => boolean, readonly get: (...args: P) => R | undefined }
366366
: never;
367367

368-
type ConstMap<M extends Mapped, D extends number> =
368+
type ConstMapImpl<M extends Mapped, D extends number> =
369369
D extends 1
370370
? GenericMappingFunc<M, D> & HasGetFuncs<M, D>
371371
: GenericMappingFunc<M, D> & HasGetFuncs<M, D> & SubMapFunc<M, D>;
372372

373-
type ToConstMap<M extends MappingEntries, S extends ShapeLike | undefined = undefined> =
373+
type ConstMap<M extends MappingEntries, S extends ShapeLike | undefined = undefined> =
374374
ToMappingAndDepth<M, S> extends [infer TM extends Mapped, infer D extends Depth[number]]
375-
? ConstMap<TM, D>
375+
? ConstMapImpl<TM, D>
376376
: never;
377377

378378
const isRecursiveTuple = (arr: RoArray) =>
@@ -383,7 +383,7 @@ export const cartesianRightRecursive =
383383
arr.length === 0
384384
? []
385385
: Array.isArray(arr[0])
386-
? (arr as MappingEntries).map(([key, val]) =>
386+
? (arr as MappingEntries).map(([key, val]: readonly [MappableKey, unknown]) =>
387387
Array.isArray(val)
388388
? (isRecursiveTuple(val) ? cartesianRightRecursive(val as unknown as RoTuple) : val)
389389
.map(ele => [key, ele].flat())
@@ -484,18 +484,20 @@ const toMapping = <
484484
export function constMap<
485485
const M extends MappingEntries,
486486
const S extends ShapeLike | undefined = undefined,
487-
>(mappingEntries: M, shape?: S): ToConstMap<M, S> {
488-
const mapping = toMapping(mappingEntries, shape);
489-
const genericMappingFunc = ((...args: MappableKey[]) =>
490-
args.reduce((subMapping: any, key) =>
491-
subMapping ? subMapping[key.toString()] ?? undefined : undefined,
492-
mapping
493-
));
494-
return Object.assign(genericMappingFunc, {
495-
has: (...args: RoArray<MappableKey>) => genericMappingFunc(...args) !== undefined,
496-
get: (...args: RoArray<MappableKey>) => genericMappingFunc(...args),
497-
subMap: (key: MappableKey) => (mapping as any)[key.toString()],
498-
}) as unknown as ToConstMap<M, S>;
487+
>(mappingEntries: M, shape?: S): ConstMap<M, S> {
488+
const toConstMap = (mapping: Mapped): ConstMap<M, S> => {
489+
const genericMappingFunc = ((...args: MappableKey[]) =>
490+
args.reduce((subMapping: any, key) =>
491+
subMapping ? subMapping[key.toString()] ?? undefined : undefined,
492+
mapping
493+
));
494+
return Object.assign(genericMappingFunc, {
495+
has: (...args: RoArray<MappableKey>) => genericMappingFunc(...args) !== undefined,
496+
get: (...args: RoArray<MappableKey>) => genericMappingFunc(...args),
497+
subMap: (key: MappableKey) => toConstMap((mapping as any)[key.toString()]),
498+
}) as unknown as ConstMap<M, S>;
499+
};
500+
return toConstMap(toMapping(mappingEntries, shape));
499501
}
500502

501503
//--- find a bunch of "tests" below

0 commit comments

Comments
 (0)