Skip to content

Commit f6bbc96

Browse files
authored
fix the discriminate case init position (#559)
1 parent 382adbb commit f6bbc96

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

core/base/__tests__/layout.ts

+67-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import { describe, expect, it } from "@jest/globals";
22

33
import {
44
Layout,
5-
serializeLayout,
6-
deserializeLayout,
5+
LayoutToType,
6+
RoArray,
77
addFixedValues,
8-
layoutDiscriminator,
98
bitsetItem,
10-
} from './../src/index.js';
9+
column,
10+
deserializeLayout,
11+
layoutDiscriminator,
12+
serializeLayout,
13+
} from "./../src/index.js";
1114

15+
// prettier-ignore
1216
const testLayout = [
1317
{ name: "uintFixedPrimitive", binary: "uint", size: 1, custom: 3 },
1418
{
@@ -105,6 +109,7 @@ const testLayout = [
105109
// type DynamicItems = DynamicItemsOfLayout<typeof testLayout>;
106110
// type DynamicValues = LayoutToType<DynamicItems>;
107111

112+
// prettier-ignore
108113
describe("Layout tests", function () {
109114

110115
const completeValues = {
@@ -337,3 +342,61 @@ describe("Layout tests", function () {
337342

338343
});
339344
});
345+
346+
describe("Switch Layout Size Tests", () => {
347+
it("Can discriminate a set of layouts", () => {
348+
const layouta = [
349+
{
350+
name: "payload",
351+
binary: "bytes",
352+
lengthSize: 2,
353+
layout: [
354+
{
355+
name: "payload",
356+
binary: "switch",
357+
idSize: 1,
358+
layouts: [
359+
[[0, "Direct"], []],
360+
[[1, "Payload"], [{ name: "data", binary: "bytes", lengthSize: 4 }]],
361+
],
362+
},
363+
],
364+
},
365+
] as const satisfies Layout;
366+
367+
const layoutb = [
368+
{
369+
name: "payload",
370+
binary: "bytes",
371+
lengthSize: 3,
372+
layout: [
373+
{
374+
name: "payload",
375+
binary: "switch",
376+
idSize: 1,
377+
layouts: [
378+
[[0, "Nothing"], []],
379+
[[1, "Data"], [{ name: "data", binary: "bytes", lengthSize: 4 }]],
380+
],
381+
},
382+
],
383+
},
384+
] as const satisfies Layout;
385+
386+
const messageLayouts = [
387+
["Layout", layouta],
388+
["LayoutB", layoutb],
389+
] as const satisfies RoArray<[string, Layout]>;
390+
const messageDiscriminator = layoutDiscriminator(column(messageLayouts, 1));
391+
392+
const b: LayoutToType<typeof layoutb> = {
393+
payload: {
394+
payload: { id: "Data", data: new Uint8Array([0, 0, 0, 0]) },
395+
},
396+
};
397+
398+
const data = serializeLayout(layoutb, b);
399+
const idx = messageDiscriminator(data);
400+
expect(idx).toEqual(1);
401+
});
402+
});

core/base/src/utils/layout/discriminate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ function layoutItemMeta(
172172
//keep track of the current index in each case's fixed bytes array
173173
const itIndexes = caseFixedBytes.map(_ => 0);
174174

175+
let caseIndex = 0;
175176
for (let bytePos = 0; bytePos < minLen;) {
176177
let byteVal = null;
177-
let caseIndex = 0;
178178
while (caseIndex < caseFixedBytes.length) {
179179
let curItIndex = itIndexes[caseIndex];
180180
const curFixedBytes = caseFixedBytes[caseIndex];

0 commit comments

Comments
 (0)