1- """Emit arrow-js Schema literals for the VGI TypeScript worker.
1+ """Emit facade-style Schema literals for the VGI TypeScript worker.
22
33Sister module to `vgi.codegen.cpp_schemas`. Same inputs (Protocol walk +
44explicit info-type list), different rendering — emits human-readable
5- TypeScript that `import`s from `@query-farm/apache-arrow` and exports one
6- `Schema` constant per unique dataclass or per-method.
5+ TypeScript that imports from the vgi-typescript Arrow facade
6+ (`../arrow/index.js`) and exports one `VgiSchema` constant per unique
7+ dataclass or per-method.
8+
9+ The facade re-exports either arrow-js or flechette per build target, so
10+ the generated file is backend-agnostic — the same source compiles for the
11+ Bun subprocess worker AND the Cloudflare Workers HTTP entrypoint.
712
813### Multirepo workflow
914
2025`tests/test_generated_ts_schemas.py` in vgi-python enforces that the
2126checked-in `.ts` matches what the generator would emit right now.
2227
23- ### Arrow-JS quirks
28+ ### Facade quirks
2429
25- - `new Dictionary (valueType, indexType, ordered, ... )` — value first, opposite
30+ - `dictionary (valueType, indexType, ordered?, id? )` — value first, opposite
2631 of pyarrow's `pa.dictionary(index_type, value_type)`. Emitter flips.
27- - `new Map_(new Field("entries", new Struct([key, value]), false), keysSorted)`
28- — structural form; arrow-js doesn't expose a convenience overload .
29- - `new Field (name, type, nullable, metadata?)` — 3rd positional is nullable.
32+ - `map(keyField, valueField, keysSorted)` — flat signature; the facade
33+ builds the Struct{key,value} entries Field internally .
34+ - `field (name, type, nullable, metadata?)` — 3rd positional is nullable.
3035"""
3136
3237from __future__ import annotations
4954
5055
5156# --------------------------------------------------------------------------- #
52- # Type emitter: pyarrow DataType -> TS expression (new Utf8( ) / new Field (...))
57+ # Type emitter: pyarrow DataType -> facade factory call (utf8( ) / field (...))
5358# --------------------------------------------------------------------------- #
5459
55- _SCALAR_MAP : dict [Any , str ] = {
56- pa .null (): "new Null()" ,
57- pa .bool_ (): "new Bool()" ,
58- pa .int8 (): "new Int8()" ,
59- pa .int16 (): "new Int16()" ,
60- pa .int32 (): "new Int32()" ,
61- pa .int64 (): "new Int64()" ,
62- pa .uint8 (): "new Uint8()" ,
63- pa .uint16 (): "new Uint16()" ,
64- pa .uint32 (): "new Uint32()" ,
65- pa .uint64 (): "new Uint64()" ,
66- pa .float32 (): "new Float32()" ,
67- pa .float64 (): "new Float64()" ,
68- pa .string (): "new Utf8()" ,
69- pa .binary (): "new Binary()" ,
60+ # pyarrow proto -> (factory expression, facade symbol to import)
61+ _SCALAR_MAP : dict [Any , tuple [str , str ]] = {
62+ pa .null (): ("nullType()" , "nullType" ),
63+ pa .bool_ (): ("bool()" , "bool" ),
64+ pa .int8 (): ("int8()" , "int8" ),
65+ pa .int16 (): ("int16()" , "int16" ),
66+ pa .int32 (): ("int32()" , "int32" ),
67+ pa .int64 (): ("int64()" , "int64" ),
68+ pa .uint8 (): ("uint8()" , "uint8" ),
69+ pa .uint16 (): ("uint16()" , "uint16" ),
70+ pa .uint32 (): ("uint32()" , "uint32" ),
71+ pa .uint64 (): ("uint64()" , "uint64" ),
72+ pa .float32 (): ("float32()" , "float32" ),
73+ pa .float64 (): ("float64()" , "float64" ),
74+ pa .string (): ("utf8()" , "utf8" ),
75+ pa .binary (): ("binary()" , "binary" ),
7076}
7177
72- # Track which arrow-js symbols the emitter used, so the output's import
78+ # Track which facade symbols the emitter used, so the output's import
7379# statement only pulls in what's actually referenced.
7480_IMPORTS_IN_USE : set [str ] = set ()
7581
@@ -78,37 +84,19 @@ def _use(name: str) -> None:
7884 _IMPORTS_IN_USE .add (name )
7985
8086
81- _SCALAR_IMPORT : dict [str , str ] = {
82- "new Null()" : "Null" ,
83- "new Bool()" : "Bool" ,
84- "new Int8()" : "Int8" ,
85- "new Int16()" : "Int16" ,
86- "new Int32()" : "Int32" ,
87- "new Int64()" : "Int64" ,
88- "new Uint8()" : "Uint8" ,
89- "new Uint16()" : "Uint16" ,
90- "new Uint32()" : "Uint32" ,
91- "new Uint64()" : "Uint64" ,
92- "new Float32()" : "Float32" ,
93- "new Float64()" : "Float64" ,
94- "new Utf8()" : "Utf8" ,
95- "new Binary()" : "Binary" ,
96- }
97-
98-
9987def _emit_type (dtype : pa .DataType , * , origin : str ) -> str :
100- for proto , expr in _SCALAR_MAP .items ():
88+ for proto , ( expr , sym ) in _SCALAR_MAP .items ():
10189 if dtype .equals (proto ):
102- _use (_SCALAR_IMPORT [ expr ] )
90+ _use (sym )
10391 return expr
10492
10593 if pa .types .is_list (dtype ):
106- _use ("List " )
107- _use ("Field " )
94+ _use ("list " )
95+ _use ("field " )
10896 value_field = dtype .value_field
10997 inner_type = _emit_type (value_field .type , origin = f"{ origin } [list item]" )
11098 nullable = "true" if value_field .nullable else "false"
111- return f'new List(new Field ("{ value_field .name } ", { inner_type } , { nullable } ))'
99+ return f'list(field ("{ value_field .name } ", { inner_type } , { nullable } ))'
112100
113101 if pa .types .is_map (dtype ):
114102 key_field = dtype .key_field
@@ -126,40 +114,34 @@ def _emit_type(dtype: pa.DataType, *, origin: str) -> str:
126114 f"item='{ item_field .name } ' nullable={ item_field .nullable } ). "
127115 "Add explicit MapType construction to ts_schemas._emit_type() if needed." ,
128116 )
129- _use ("Map_" )
130- _use ("Field" )
131- _use ("Struct" )
117+ _use ("map" )
118+ _use ("field" )
132119 key_type = _emit_type (dtype .key_type , origin = f"{ origin } [map key]" )
133120 item_type = _emit_type (dtype .item_type , origin = f"{ origin } [map value]" )
134121 # pyarrow's pa.map_ defaults keys_sorted=False.
135- keys_sorted = "false"
136- # Cast children to `any` to bypass arrow-js's Field<T> generic
137- # inference, matching the pattern already used in dispatch.ts:354-357.
138122 return (
139- 'new Map_(new Field("entries", new Struct([\n '
140- f' new Field("key", { key_type } as any, false),\n '
141- f' new Field("value", { item_type } as any, true),\n '
142- f" ] as any), false), { keys_sorted } )"
123+ f'map(field("key", { key_type } , false), '
124+ f'field("value", { item_type } , true), false)'
143125 )
144126
145127 if pa .types .is_dictionary (dtype ):
146- _use ("Dictionary " )
128+ _use ("dictionary " )
147129 value_type = _emit_type (dtype .value_type , origin = f"{ origin } [dict value]" )
148130 index_type = _emit_type (dtype .index_type , origin = f"{ origin } [dict index]" )
149- # arrow-js: `new Dictionary(valueType, indexType, id?, isOrdered?)`.
150- # Value comes first — opposite of pyarrow's (index, value). Omit `id` so
151- # arrow-js auto-assigns; pass `null` positional when `ordered` is true.
131+ # facade: `dictionary(valueType, indexType, ordered?, id?)`.
132+ # Value comes first — opposite of pyarrow's (index, value).
152133 if dtype .ordered :
153- return f"new Dictionary ({ value_type } , { index_type } , null , true)"
154- return f"new Dictionary ({ value_type } , { index_type } )"
134+ return f"dictionary ({ value_type } , { index_type } , true)"
135+ return f"dictionary ({ value_type } , { index_type } )"
155136
156137 if pa .types .is_struct (dtype ):
157- _use ("Struct " )
158- _use ("Field " )
138+ _use ("struct " )
139+ _use ("field " )
159140 child_exprs = [
160- _emit_field (dtype .field (i ), origin = f"{ origin } [struct child { i } ]" ) for i in range (dtype .num_fields )
141+ _emit_field (dtype .field (i ), origin = f"{ origin } [struct child { i } ]" )
142+ for i in range (dtype .num_fields )
161143 ]
162- return "new Struct ([" + ", " .join (child_exprs ) + "])"
144+ return "struct ([" + ", " .join (child_exprs ) + "])"
163145
164146 raise GeneratorError (
165147 f"vgi.codegen.ts_schemas: unsupported Arrow type { type (dtype ).__name__ !r} at { origin } "
@@ -173,17 +155,17 @@ def _uses_default_map_field_name(dtype: pa.MapType[Any, Any, Any]) -> bool:
173155 return canonical .equals (dtype )
174156
175157
176- def _emit_field (field : pa .Field [Any ], * , origin : str ) -> str :
177- _use ("Field " )
178- type_expr = _emit_type (field .type , origin = f"{ origin } [{ field .name } ]" )
179- nullable = "true" if field .nullable else "false"
180- return f'new Field ("{ field .name } ", { type_expr } , { nullable } )'
158+ def _emit_field (field_obj : pa .Field [Any ], * , origin : str ) -> str :
159+ _use ("field " )
160+ type_expr = _emit_type (field_obj .type , origin = f"{ origin } [{ field_obj .name } ]" )
161+ nullable = "true" if field_obj .nullable else "false"
162+ return f'field ("{ field_obj .name } ", { type_expr } , { nullable } )'
181163
182164
183165def _emit_const (es : EmittedSchema ) -> str :
184- _use ("Schema " )
166+ _use ("schema " )
185167 body = f"// Origin: { es .origin } \n "
186- body += f"export const { es .name } Schema = new Schema ("
168+ body += f"export const { es .name } Schema = schema ("
187169 if len (es .schema ) == 0 :
188170 body += "[]);\n "
189171 else :
@@ -194,25 +176,28 @@ def _emit_const(es: EmittedSchema) -> str:
194176 return body
195177
196178
197- GENERATOR_VERSION = "1"
179+ # Bumped to v2 to mark the arrow-js -> facade transition. The generated
180+ # file's `Generator: vgi-gen-ts-schemas v2` header lets the conformance test
181+ # detect stale checkouts that need a regen.
182+ GENERATOR_VERSION = "2"
198183
199184
200185def emit (out : TextIO ) -> None :
201186 """Emit the generated TypeScript schemas module to *out*."""
202187 schemas = collect_schemas ()
203188
204- # Render all schemas FIRST to capture which arrow-js symbols were used.
189+ # Render all schemas FIRST to capture which facade symbols were used.
205190 _IMPORTS_IN_USE .clear ()
206191 body_blocks = [_emit_const (es ) for es in schemas ]
207- # Schema is always used in the import because every factory returns one.
208- _use ("Schema " )
192+ # `schema` is always used in the import because every factory returns one.
193+ _use ("schema " )
209194 imports = sorted (_IMPORTS_IN_USE )
210195
211196 body = io .StringIO ()
212197 body .write ("import {\n " )
213198 for sym in imports :
214199 body .write (f" { sym } ,\n " )
215- body .write ('} from "@query-farm/apache- arrow";\n \n ' )
200+ body .write ('} from "../ arrow/index.js ";\n \n ' )
216201
217202 for block in body_blocks :
218203 body .write (block )
0 commit comments