Skip to content

Commit ba15373

Browse files
authored
typescript: canonical naming for reducer and procedure. (#4371)
# Description of Changes Canonical naming for reducer and procedure. It shouldn't have the `name` field there but if it's there it should be used as canonical name. # API and ABI breaking changes Yes # Expected complexity level and risk 1
1 parent 19cc87e commit ba15373

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

crates/bindings-typescript/src/server/procedures.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ function registerProcedure<
101101
Ret extends TypeBuilder<any, any>,
102102
>(
103103
ctx: SchemaInner,
104-
name: string,
104+
exportName: string,
105105
params: Params,
106106
ret: Ret,
107-
fn: ProcedureFn<S, Params, Ret>
107+
fn: ProcedureFn<S, Params, Ret>,
108+
opts?: ProcedureOpts
108109
) {
109-
ctx.defineFunction(name);
110+
ctx.defineFunction(exportName);
110111
const paramsType: ProductType = {
111112
elements: Object.entries(params).map(([n, c]) => ({
112113
name: n,
@@ -118,12 +119,21 @@ function registerProcedure<
118119
const returnType = ctx.registerTypesRecursively(ret).algebraicType;
119120

120121
ctx.moduleDef.procedures.push({
121-
sourceName: name,
122+
sourceName: exportName,
122123
params: paramsType,
123124
returnType,
124125
visibility: FunctionVisibility.ClientCallable,
125126
});
126127

128+
if (opts?.name != null) {
129+
ctx.moduleDef.explicitNames.entries.push({
130+
tag: 'Function',
131+
value: {
132+
sourceName: exportName,
133+
canonicalName: opts.name,
134+
},
135+
});
136+
}
127137
const { typespace } = ctx;
128138

129139
ctx.procedures.push({

crates/bindings-typescript/src/server/reducers.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ export function makeReducerExport<
3131
fn: Reducer<any, any>,
3232
lifecycle?: Lifecycle
3333
): ReducerExport<S, Params> {
34-
const name = opts?.name;
35-
3634
const reducerExport: ReducerExport<S, Params> = (...args) => fn(...args);
3735
reducerExport[exportContext] = ctx;
3836
reducerExport[registerExport] = (ctx, exportName) => {
39-
registerReducer(ctx, name ?? exportName, params, fn, lifecycle);
37+
registerReducer(ctx, exportName, params, fn, opts, lifecycle);
4038
ctx.functionExports.set(
4139
reducerExport as ReducerExport<any, any>,
42-
name ?? exportName
40+
exportName
4341
);
4442
};
4543

@@ -56,27 +54,28 @@ export function makeReducerExport<
5654
*/
5755
export function registerReducer(
5856
ctx: SchemaInner,
59-
name: string,
57+
exportName: string,
6058
params: RowObj | RowBuilder<RowObj>,
6159
fn: Reducer<any, any>,
60+
opts?: ReducerOpts,
6261
lifecycle?: Lifecycle
6362
): void {
64-
ctx.defineFunction(name);
63+
ctx.defineFunction(exportName);
6564

6665
if (!(params instanceof RowBuilder)) {
6766
params = new RowBuilder(params);
6867
}
6968

7069
if (params.typeName === undefined) {
71-
params.typeName = toPascalCase(name);
70+
params.typeName = toPascalCase(exportName);
7271
}
7372

7473
const ref = ctx.registerTypesRecursively(params);
7574
const paramsType = ctx.resolveType(ref).value;
7675
const isLifecycle = lifecycle != null;
7776

7877
ctx.moduleDef.reducers.push({
79-
sourceName: name,
78+
sourceName: exportName,
8079
params: paramsType,
8180
//ModuleDef validation code is responsible to mark private reducers
8281
visibility: FunctionVisibility.ClientCallable,
@@ -85,17 +84,27 @@ export function registerReducer(
8584
errReturnType: AlgebraicType.String,
8685
});
8786

87+
if (opts?.name != null) {
88+
ctx.moduleDef.explicitNames.entries.push({
89+
tag: 'Function',
90+
value: {
91+
sourceName: exportName,
92+
canonicalName: opts.name,
93+
},
94+
});
95+
}
96+
8897
if (isLifecycle) {
8998
ctx.moduleDef.lifeCycleReducers.push({
9099
lifecycleSpec: lifecycle,
91-
functionName: name,
100+
functionName: exportName,
92101
});
93102
}
94103

95104
// If the function isn't named (e.g. `function foobar() {}`), give it the same
96105
// name as the reducer so that it's clear what it is in in backtraces.
97106
if (!fn.name) {
98-
Object.defineProperty(fn, 'name', { value: name, writable: false });
107+
Object.defineProperty(fn, 'name', { value: exportName, writable: false });
99108
}
100109

101110
ctx.reducers.push(fn);

crates/codegen/src/rust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ pub struct {cols_ix} {{"
735735
.iter()
736736
.find(|col| col.col_id == cols.as_singleton().expect("singleton column"))
737737
.unwrap();
738-
let field_name = column.accessor_name.deref();
738+
let field_name = column.accessor_name.deref().to_case(Case::Snake);
739739
let field_type = type_name(module, &column.ty_for_generate);
740740

741741
writeln!(
@@ -763,7 +763,7 @@ impl __sdk::__query_builder::HasIxCols for {struct_name} {{
763763
.iter()
764764
.find(|col| col.col_id == cols.as_singleton().expect("singleton column"))
765765
.expect("singleton column");
766-
let field_name = column.accessor_name.deref();
766+
let field_name = column.accessor_name.deref().to_case(Case::Snake);
767767
let col_name = column.name.deref();
768768

769769
writeln!(

0 commit comments

Comments
 (0)