Skip to content

Commit e8b1c6d

Browse files
Support tuple and set for constant generation (#290)
Co-authored-by: jayaprabhakar <jayaprabhakar@gmail.com>
1 parent f86245e commit e8b1c6d

5 files changed

Lines changed: 69 additions & 3 deletions

File tree

mbt/lib/shared/proto/mbt_plugin.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ message Value {
2323
MapValue map_value = 4;
2424
ListValue list_value = 5;
2525
SentinelType sentinel_value = 6; // Sentinel for flexible state matching
26+
TupleValue tuple_value = 7; // Tuple (immutable sequence)
27+
SetValue set_value = 8; // Set (unique elements)
2628
}
2729
}
2830

@@ -39,6 +41,10 @@ message ListValue {
3941
repeated Value items = 1;
4042
}
4143

44+
message TupleValue {
45+
repeated Value items = 1;
46+
}
47+
4248
message SetValue {
4349
repeated Value items = 1;
4450
}

mbt/lib/typescript/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export {
2727
Arg,
2828
RoleId,
2929
NotImplementedError,
30-
MbtError
30+
MbtError,
31+
Tuple
3132
} from './types';
3233

3334
// Runner

mbt/lib/typescript/src/overrides.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Tuple } from './types';
2+
13
/**
24
* Builder class for providing variable overrides.
35
* Supports type-safe methods for setting various Starlark-compatible types.
@@ -8,10 +10,10 @@ export class OverridesBuilder {
810
/**
911
* Sets a variable override with automatic type inference.
1012
* @param key - The variable name
11-
* @param value - The value (string, number, boolean, array, or object)
13+
* @param value - The value (string, number, boolean, array, Set, Tuple, or object)
1214
* @returns this for method chaining
1315
*/
14-
set(key: string, value: string | number | boolean | any[] | Record<string, any> | Map<any, any>): this {
16+
set(key: string, value: string | number | boolean | any[] | Set<any> | Tuple | Record<string, any> | Map<any, any>): this {
1517
this.overrides.set(key, value);
1618
return this;
1719
}
@@ -66,6 +68,28 @@ export class OverridesBuilder {
6668
return this.set(key, value);
6769
}
6870

71+
/**
72+
* Sets a tuple variable override.
73+
* In Starlark, tuples are immutable sequences written as (item1, item2, ...).
74+
* @param key - The variable name
75+
* @param value - Tuple value
76+
* @returns this for method chaining
77+
*/
78+
setTuple(key: string, value: Tuple): this {
79+
return this.set(key, value);
80+
}
81+
82+
/**
83+
* Sets a set variable override.
84+
* In Starlark, sets use the set() builtin function: set([item1, item2, ...]).
85+
* @param key - The variable name
86+
* @param value - Set value
87+
* @returns this for method chaining
88+
*/
89+
setSet(key: string, value: Set<any>): this {
90+
return this.set(key, value);
91+
}
92+
6993
/**
7094
* Gets all variable overrides.
7195
* @returns A copy of the overrides map

mbt/lib/typescript/src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ export class MbtError extends Error {
3333
this.name = 'MbtError';
3434
}
3535
}
36+
37+
/**
38+
* Represents a tuple (immutable sequence) for use in overrides.
39+
* In Starlark, tuples are written as (item1, item2, ...).
40+
*/
41+
export class Tuple {
42+
readonly items: any[];
43+
44+
constructor(...items: any[]) {
45+
this.items = items;
46+
}
47+
48+
static from(items: any[]): Tuple {
49+
return new Tuple(...items);
50+
}
51+
}

mbt/lib/typescript/src/value.ts

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

55
import * as pb from '../proto-gen/mbt_plugin_pb';
66
import { Ignored } from './sentinels';
7+
import { Tuple } from './types';
78

89
/**
910
* Converts a protobuf Value to a native TypeScript value.
@@ -94,6 +95,24 @@ export function toProtoValue(value: any): pb.Value {
9495
return result;
9596
}
9697

98+
// Handle Tuple instances (must come before Array check)
99+
if (value instanceof Tuple) {
100+
const tupleValue = new pb.TupleValue();
101+
const items = value.items.map(item => toProtoValue(item));
102+
tupleValue.setItemsList(items);
103+
result.setTupleValue(tupleValue);
104+
return result;
105+
}
106+
107+
// Handle Set instances
108+
if (value instanceof Set) {
109+
const setValue = new pb.SetValue();
110+
const items = Array.from(value).map(item => toProtoValue(item));
111+
setValue.setItemsList(items);
112+
result.setSetValue(setValue);
113+
return result;
114+
}
115+
97116
if (Array.isArray(value)) {
98117
const listValue = new pb.ListValue();
99118
const items = value.map(item => toProtoValue(item));

0 commit comments

Comments
 (0)