Skip to content

Commit 7cd5232

Browse files
committed
read multiple times to capture multiple()
1 parent 694854f commit 7cd5232

5 files changed

Lines changed: 169 additions & 149 deletions

File tree

‎lib/bind.ts‎

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export function bindPhase(options: {
100100
let params = Object.values(phase.params) as Param<string, unknown>[];
101101
let pending = new Map(params.map((param) => [param.name, param]));
102102
let results = new Map<string, Result<unknown>>();
103+
let captures = new Map<string, Capture>();
103104

104105
function settle(
105106
param: Param<string, unknown>,
@@ -136,11 +137,9 @@ export function bindPhase(options: {
136137
for (let param of pending.values()) {
137138
let view = rest.tokens.view({
138139
range: segment.range,
139-
// Repeated options must see every occurrence in the phase, not only
140-
// the first option/value pair before the binding horizon.
141-
through: param.multiple ? undefined : horizon?.index,
140+
through: horizon?.index,
142141
});
143-
let read = param.cli.read(view, param.multiple);
142+
let read = param.cli.read(view);
144143

145144
if (read.result.ok && !read.result.value.exists) {
146145
continue;
@@ -156,9 +155,47 @@ export function bindPhase(options: {
156155
break;
157156
}
158157

158+
if (
159+
offer.param.multiple && offer.read.result.ok &&
160+
offer.read.result.value.exists
161+
) {
162+
rest = { ...rest, tokens: offer.read.claim.rest };
163+
let capture = captures.get(offer.param.name) ?? {
164+
values: [],
165+
issues: [],
166+
};
167+
captures.set(offer.param.name, {
168+
values: [
169+
...capture.values,
170+
offer.read.result.value.value,
171+
],
172+
issues: [
173+
...capture.issues,
174+
...(offer.read.result.issues ?? []),
175+
],
176+
});
177+
continue;
178+
}
179+
159180
accept(offer.param, fromRead(offer.param, offer.read, rest));
160181
}
161182

183+
for (let param of pending.values()) {
184+
let capture = captures.get(param.name);
185+
if (!capture) {
186+
continue;
187+
}
188+
189+
results.set(
190+
param.name,
191+
merge(
192+
decodeMany(param, capture.values, [param.name]),
193+
capture.issues,
194+
),
195+
);
196+
pending.delete(param.name);
197+
}
198+
162199
// Address sources have stable visibility once the route is known. They are
163200
// tried only after CLI has reached its fixed point so a provisional CLI miss
164201
// cannot let a lower-priority source settle the parameter too early.
@@ -201,6 +238,11 @@ export function bindPhase(options: {
201238
return { rest, model, issues, valid };
202239
}
203240

241+
type Capture = {
242+
values: unknown[];
243+
issues: Issue[];
244+
};
245+
204246
function fromRead<T>(
205247
param: Param<string, T>,
206248
read: CLIRead,
@@ -226,13 +268,11 @@ function fromRead<T>(
226268
}
227269

228270
let value = read.result.value.value;
229-
let result = Array.isArray(value) ? decodeMany(param, value, path) : decode(
230-
param,
231-
value,
232-
typeof value === "string" ? param.decode(value) : [value],
233-
path,
271+
let candidates = typeof value === "string" ? param.decode(value) : [value];
272+
let result = merge(
273+
decode(param, value, candidates, path),
274+
read.result.issues,
234275
);
235-
result = merge(result, read.result.issues);
236276

237277
return {
238278
exists: true,
@@ -251,44 +291,55 @@ function decodeMany<T>(
251291
values: unknown[],
252292
path: string[],
253293
): Result<T> {
254-
let candidates: unknown[][] = [[]];
294+
return decode(param, values, candidates(param, values), path);
295+
}
255296

256-
for (let value of values) {
257-
let decoded = typeof value === "string" ? param.decode(value) : [value];
258-
candidates = candidates.flatMap((prefix) =>
259-
decoded.map((candidate) => [...prefix, candidate])
260-
);
297+
function* candidates<T>(
298+
param: Param<string, T>,
299+
values: readonly unknown[],
300+
index = 0,
301+
prefix: readonly unknown[] = [],
302+
): Iterable<unknown[]> {
303+
if (index === values.length) {
304+
yield [...prefix];
305+
return;
261306
}
262307

263-
return decode(param, values, candidates, path);
308+
let value = values[index];
309+
let decoded = typeof value === "string" ? param.decode(value) : [value];
310+
for (let candidate of decoded) {
311+
yield* candidates(param, values, index + 1, [...prefix, candidate]);
312+
}
264313
}
265314

266315
function decode<T>(
267316
param: Param<string, T>,
268317
value: unknown,
269-
candidates: unknown[],
318+
candidates: Iterable<unknown>,
270319
path: string[],
271320
): Result<T> {
272-
if (candidates.length === 0) {
273-
return {
274-
ok: false,
275-
issues: [{
276-
message: `unable to decode ${JSON.stringify(value)}`,
277-
path,
278-
}],
279-
};
280-
}
281-
282321
let issues: readonly Issue[] | undefined;
322+
let found = false;
283323

284324
for (let candidate of candidates) {
325+
found = true;
285326
let result = validate(param, candidate, path);
286327
if (result.ok) {
287328
return result;
288329
}
289330
issues = issues ?? result.issues;
290331
}
291332

333+
if (!found) {
334+
return {
335+
ok: false,
336+
issues: [{
337+
message: `unable to decode ${JSON.stringify(value)}`,
338+
path,
339+
}],
340+
};
341+
}
342+
292343
return {
293344
ok: false,
294345
issues: issues ?? [],

‎lib/param.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Param<K extends string, T> extends Definition<K> {
1818
multiple?: boolean;
1919
}
2020

21-
export interface MultipleParam<K extends string, T> extends Param<K, T[]> {
21+
export interface MultipleParam<K extends string, T> extends Param<K, T> {
2222
multiple: true;
2323
}
2424

@@ -66,18 +66,15 @@ export function schema<S extends Schema>(
6666
export interface SchemaTransform<S extends Schema> extends Transform {
6767
readonly input: Param<string, unknown>;
6868
readonly output: this["input"] extends MultipleParam<infer N, unknown>
69-
? OutputOf<S> extends (infer E)[] ? MultipleParam<N, E> : never
70-
: this["input"] extends Param<infer N, unknown>
71-
? Param<N, OutputOf<S>>
69+
? MultipleParam<N, OutputOf<S>>
70+
: this["input"] extends Param<infer N, unknown> ? Param<N, OutputOf<S>>
7271
: never;
7372
}
7473

7574
interface MultipleTransform extends Transform {
7675
readonly input: Param<string, unknown>;
7776
readonly output: this["input"] extends Param<infer N, infer T>
78-
? unknown extends T ? MultipleParam<N, unknown>
79-
: T extends (infer E)[] ? MultipleParam<N, E>
80-
: never
77+
? MultipleParam<N, T>
8178
: never;
8279
}
8380

0 commit comments

Comments
 (0)