Skip to content

Commit 540e521

Browse files
Copilotstreamich
andcommitted
feat: complete codegen method refactoring with passing tests
Co-authored-by: streamich <[email protected]>
1 parent 0f89bf7 commit 540e521

File tree

5 files changed

+78
-102
lines changed

5 files changed

+78
-102
lines changed

src/codegen/binary/cborEncoders.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import type {CborEncoderCodegenContext} from './CborEncoderCodegenContext';
2-
import type {JsExpression} from '@jsonjoy.com/util/lib/codegen/util/JsExpression';
2+
import {JsExpression} from '@jsonjoy.com/util/lib/codegen/util/JsExpression';
33
import type {Type} from '../../type';
44
import type {BinaryJsonEncoder} from '@jsonjoy.com/json-pack/lib/types';
55
import type {BinaryEncoderCodegenContext} from './BinaryEncoderCodegenContext';
66
import {normalizeAccessor} from '@jsonjoy.com/util/lib/codegen/util/normalizeAccessor';
7+
import {EncodingFormat} from '@jsonjoy.com/json-pack/lib/constants';
78

89
type CborEncoderFunction = (ctx: CborEncoderCodegenContext, value: JsExpression, type: Type) => void;
910

10-
const codegenBinaryEncoder = (ctx: BinaryEncoderCodegenContext<BinaryJsonEncoder>, value: JsExpression, type: Type): void => {
11+
const codegenBinaryEncoder = (
12+
ctx: BinaryEncoderCodegenContext<BinaryJsonEncoder>,
13+
value: JsExpression,
14+
type: Type,
15+
): void => {
1116
const kind = type.getTypeName();
1217
const v = value.use();
13-
18+
1419
switch (kind) {
1520
case 'str': {
1621
const strType = type as any; // StrType
@@ -75,7 +80,7 @@ export const any = (ctx: CborEncoderCodegenContext, value: JsExpression, type: T
7580
};
7681

7782
export const bool = (ctx: CborEncoderCodegenContext, value: JsExpression): void => {
78-
codegenBinaryEncoder(ctx, value, { getTypeName: () => 'bool' } as Type);
83+
codegenBinaryEncoder(ctx, value, {getTypeName: () => 'bool'} as Type);
7984
};
8085

8186
export const num = (ctx: CborEncoderCodegenContext, value: JsExpression, type: Type): void => {
@@ -140,7 +145,7 @@ export const obj = (
140145
const codegen = ctx.codegen;
141146
const r = codegen.var(value.use());
142147
const encodeUnknownFields = !!objType.schema.encodeUnknownFields;
143-
148+
144149
if (encodeUnknownFields) {
145150
ctx.js(/* js */ `encoder.writeAny(${r});`);
146151
return;
@@ -149,7 +154,7 @@ export const obj = (
149154
const fields = objType.fields;
150155
const requiredFields = fields.filter((f: any) => !f.optional && f.constructor?.name !== 'ObjectOptionalFieldType');
151156
const optionalFields = fields.filter((f: any) => f.optional || f.constructor?.name === 'ObjectOptionalFieldType');
152-
157+
153158
if (optionalFields.length === 0) {
154159
// All fields are required
155160
ctx.js(/* js */ `encoder.writeObjHdr(${fields.length});`);
@@ -163,24 +168,24 @@ export const obj = (
163168
// Mixed fields - need to count optional ones dynamically
164169
const rSize = codegen.getRegister();
165170
ctx.js(/* js */ `var ${rSize} = ${requiredFields.length};`);
166-
171+
167172
// Count optional fields that exist
168173
for (const field of optionalFields) {
169174
const key = field.key;
170175
const accessor = normalizeAccessor(key);
171176
ctx.js(/* js */ `if (${r}${accessor} !== undefined) ${rSize}++;`);
172177
}
173-
178+
174179
ctx.js(/* js */ `encoder.writeObjHdr(${rSize});`);
175-
180+
176181
// Encode required fields
177182
for (const field of requiredFields) {
178183
const key = field.key;
179184
const accessor = normalizeAccessor(key);
180185
ctx.js(/* js */ `encoder.writeStr(${JSON.stringify(key)});`);
181186
encodeFn(ctx, new JsExpression(() => `${r}${accessor}`), field.value);
182187
}
183-
188+
184189
// Encode optional fields
185190
for (const field of optionalFields) {
186191
const key = field.key;
@@ -206,7 +211,7 @@ export const map = (
206211
const rKey = codegen.var();
207212
const rLen = codegen.var(`${rKeys}.length`);
208213
const ri = codegen.var('0');
209-
214+
210215
ctx.js(/* js */ `var ${rKeys} = Object.keys(${r}), ${rLen} = ${rKeys}.length, ${rKey}, ${ri} = 0;`);
211216
ctx.js(/* js */ `encoder.writeObjHdr(${rLen});`);
212217
ctx.js(/* js */ `for (; ${ri} < ${rLen}; ${ri}++) {`);
@@ -216,15 +221,12 @@ export const map = (
216221
ctx.js(`}`);
217222
};
218223

219-
export const ref = (
220-
ctx: CborEncoderCodegenContext,
221-
value: JsExpression,
222-
type: Type,
223-
): void => {
224+
export const ref = (ctx: CborEncoderCodegenContext, value: JsExpression, type: Type): void => {
224225
const refType = type as any; // RefType
225226
const system = ctx.options.system || refType.system;
226227
if (!system) throw new Error('NO_SYSTEM');
227-
const encoder = system.resolve(refType.schema.ref).type.encoder(ctx.encoder.format);
228+
const format = EncodingFormat.Cbor;
229+
const encoder = system.resolve(refType.schema.ref).type.encoder(format);
228230
const d = ctx.codegen.linkDependency(encoder);
229231
ctx.js(`${d}(${value.use()}, encoder);`);
230232
};
@@ -255,11 +257,7 @@ export const or = (
255257
* Main router function that dispatches CBOR encoding to the appropriate
256258
* encoder function based on the type's kind.
257259
*/
258-
export const generate = (
259-
ctx: CborEncoderCodegenContext,
260-
value: JsExpression,
261-
type: Type,
262-
): void => {
260+
export const generate = (ctx: CborEncoderCodegenContext, value: JsExpression, type: Type): void => {
263261
const kind = type.getTypeName();
264262

265263
switch (kind) {
@@ -302,4 +300,4 @@ export const generate = (
302300
default:
303301
throw new Error(`${kind} type CBOR encoding not implemented`);
304302
}
305-
};
303+
};

src/codegen/binary/jsonEncoders.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import type {JsonEncoderCodegenContext} from './JsonEncoderCodegenContext';
2-
import type {JsExpression} from '@jsonjoy.com/util/lib/codegen/util/JsExpression';
2+
import {JsExpression} from '@jsonjoy.com/util/lib/codegen/util/JsExpression';
33
import type {Type} from '../../type';
44
import type {BinaryJsonEncoder} from '@jsonjoy.com/json-pack/lib/types';
55
import type {BinaryEncoderCodegenContext} from './BinaryEncoderCodegenContext';
66
import {normalizeAccessor} from '@jsonjoy.com/util/lib/codegen/util/normalizeAccessor';
7+
import {EncodingFormat} from '@jsonjoy.com/json-pack/lib/constants';
78

89
type JsonEncoderFunction = (ctx: JsonEncoderCodegenContext, value: JsExpression, type: Type) => void;
910

10-
const codegenBinaryEncoder = (ctx: BinaryEncoderCodegenContext<BinaryJsonEncoder>, value: JsExpression, type: Type): void => {
11+
const codegenBinaryEncoder = (
12+
ctx: BinaryEncoderCodegenContext<BinaryJsonEncoder>,
13+
value: JsExpression,
14+
type: Type,
15+
): void => {
1116
const kind = type.getTypeName();
1217
const v = value.use();
13-
18+
1419
switch (kind) {
1520
case 'str': {
1621
const strType = type as any; // StrType
@@ -75,7 +80,7 @@ export const any = (ctx: JsonEncoderCodegenContext, value: JsExpression, type: T
7580
};
7681

7782
export const bool = (ctx: JsonEncoderCodegenContext, value: JsExpression): void => {
78-
codegenBinaryEncoder(ctx, value, { getTypeName: () => 'bool' } as Type);
83+
codegenBinaryEncoder(ctx, value, {getTypeName: () => 'bool'} as Type);
7984
};
8085

8186
export const num = (ctx: JsonEncoderCodegenContext, value: JsExpression, type: Type): void => {
@@ -140,7 +145,7 @@ export const obj = (
140145
const codegen = ctx.codegen;
141146
const r = codegen.var(value.use());
142147
const encodeUnknownFields = !!objType.schema.encodeUnknownFields;
143-
148+
144149
if (encodeUnknownFields) {
145150
ctx.js(/* js */ `encoder.writeAny(${r});`);
146151
return;
@@ -149,7 +154,7 @@ export const obj = (
149154
const fields = objType.fields;
150155
const requiredFields = fields.filter((f: any) => !f.optional && f.constructor?.name !== 'ObjectOptionalFieldType');
151156
const optionalFields = fields.filter((f: any) => f.optional || f.constructor?.name === 'ObjectOptionalFieldType');
152-
157+
153158
if (optionalFields.length === 0) {
154159
// All fields are required
155160
ctx.js(/* js */ `encoder.writeObjHdr(${fields.length});`);
@@ -163,24 +168,24 @@ export const obj = (
163168
// Mixed fields - need to count optional ones dynamically
164169
const rSize = codegen.getRegister();
165170
ctx.js(/* js */ `var ${rSize} = ${requiredFields.length};`);
166-
171+
167172
// Count optional fields that exist
168173
for (const field of optionalFields) {
169174
const key = field.key;
170175
const accessor = normalizeAccessor(key);
171176
ctx.js(/* js */ `if (${r}${accessor} !== undefined) ${rSize}++;`);
172177
}
173-
178+
174179
ctx.js(/* js */ `encoder.writeObjHdr(${rSize});`);
175-
180+
176181
// Encode required fields
177182
for (const field of requiredFields) {
178183
const key = field.key;
179184
const accessor = normalizeAccessor(key);
180185
ctx.js(/* js */ `encoder.writeStr(${JSON.stringify(key)});`);
181186
encodeFn(ctx, new JsExpression(() => `${r}${accessor}`), field.value);
182187
}
183-
188+
184189
// Encode optional fields
185190
for (const field of optionalFields) {
186191
const key = field.key;
@@ -206,7 +211,7 @@ export const map = (
206211
const rKey = codegen.var();
207212
const rLen = codegen.var(`${rKeys}.length`);
208213
const ri = codegen.var('0');
209-
214+
210215
ctx.js(/* js */ `var ${rKeys} = Object.keys(${r}), ${rLen} = ${rKeys}.length, ${rKey}, ${ri} = 0;`);
211216
ctx.js(/* js */ `encoder.writeObjHdr(${rLen});`);
212217
ctx.js(/* js */ `for (; ${ri} < ${rLen}; ${ri}++) {`);
@@ -216,15 +221,12 @@ export const map = (
216221
ctx.js(`}`);
217222
};
218223

219-
export const ref = (
220-
ctx: JsonEncoderCodegenContext,
221-
value: JsExpression,
222-
type: Type,
223-
): void => {
224+
export const ref = (ctx: JsonEncoderCodegenContext, value: JsExpression, type: Type): void => {
224225
const refType = type as any; // RefType
225226
const system = ctx.options.system || refType.system;
226227
if (!system) throw new Error('NO_SYSTEM');
227-
const encoder = system.resolve(refType.schema.ref).type.encoder(ctx.encoder.format);
228+
const format = EncodingFormat.Json;
229+
const encoder = system.resolve(refType.schema.ref).type.encoder(format);
228230
const d = ctx.codegen.linkDependency(encoder);
229231
ctx.js(`${d}(${value.use()}, encoder);`);
230232
};
@@ -255,11 +257,7 @@ export const or = (
255257
* Main router function that dispatches JSON encoding to the appropriate
256258
* encoder function based on the type's kind.
257259
*/
258-
export const generate = (
259-
ctx: JsonEncoderCodegenContext,
260-
value: JsExpression,
261-
type: Type,
262-
): void => {
260+
export const generate = (ctx: JsonEncoderCodegenContext, value: JsExpression, type: Type): void => {
263261
const kind = type.getTypeName();
264262

265263
switch (kind) {
@@ -302,4 +300,4 @@ export const generate = (
302300
default:
303301
throw new Error(`${kind} type JSON encoding not implemented`);
304302
}
305-
};
303+
};

src/codegen/binary/messagePackEncoders.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import type {MessagePackEncoderCodegenContext} from './MessagePackEncoderCodegenContext';
2-
import type {JsExpression} from '@jsonjoy.com/util/lib/codegen/util/JsExpression';
2+
import {JsExpression} from '@jsonjoy.com/util/lib/codegen/util/JsExpression';
33
import type {Type} from '../../type';
44
import type {BinaryJsonEncoder} from '@jsonjoy.com/json-pack/lib/types';
55
import type {BinaryEncoderCodegenContext} from './BinaryEncoderCodegenContext';
66
import {normalizeAccessor} from '@jsonjoy.com/util/lib/codegen/util/normalizeAccessor';
7+
import {EncodingFormat} from '@jsonjoy.com/json-pack/lib/constants';
78

89
type MessagePackEncoderFunction = (ctx: MessagePackEncoderCodegenContext, value: JsExpression, type: Type) => void;
910

10-
const codegenBinaryEncoder = (ctx: BinaryEncoderCodegenContext<BinaryJsonEncoder>, value: JsExpression, type: Type): void => {
11+
const codegenBinaryEncoder = (
12+
ctx: BinaryEncoderCodegenContext<BinaryJsonEncoder>,
13+
value: JsExpression,
14+
type: Type,
15+
): void => {
1116
const kind = type.getTypeName();
1217
const v = value.use();
13-
18+
1419
switch (kind) {
1520
case 'str': {
1621
const strType = type as any; // StrType
@@ -75,7 +80,7 @@ export const any = (ctx: MessagePackEncoderCodegenContext, value: JsExpression,
7580
};
7681

7782
export const bool = (ctx: MessagePackEncoderCodegenContext, value: JsExpression): void => {
78-
codegenBinaryEncoder(ctx, value, { getTypeName: () => 'bool' } as Type);
83+
codegenBinaryEncoder(ctx, value, {getTypeName: () => 'bool'} as Type);
7984
};
8085

8186
export const num = (ctx: MessagePackEncoderCodegenContext, value: JsExpression, type: Type): void => {
@@ -140,7 +145,7 @@ export const obj = (
140145
const codegen = ctx.codegen;
141146
const r = codegen.var(value.use());
142147
const encodeUnknownFields = !!objType.schema.encodeUnknownFields;
143-
148+
144149
if (encodeUnknownFields) {
145150
ctx.js(/* js */ `encoder.writeAny(${r});`);
146151
return;
@@ -149,7 +154,7 @@ export const obj = (
149154
const fields = objType.fields;
150155
const requiredFields = fields.filter((f: any) => !f.optional && f.constructor?.name !== 'ObjectOptionalFieldType');
151156
const optionalFields = fields.filter((f: any) => f.optional || f.constructor?.name === 'ObjectOptionalFieldType');
152-
157+
153158
if (optionalFields.length === 0) {
154159
// All fields are required
155160
ctx.js(/* js */ `encoder.writeObjHdr(${fields.length});`);
@@ -163,24 +168,24 @@ export const obj = (
163168
// Mixed fields - need to count optional ones dynamically
164169
const rSize = codegen.getRegister();
165170
ctx.js(/* js */ `var ${rSize} = ${requiredFields.length};`);
166-
171+
167172
// Count optional fields that exist
168173
for (const field of optionalFields) {
169174
const key = field.key;
170175
const accessor = normalizeAccessor(key);
171176
ctx.js(/* js */ `if (${r}${accessor} !== undefined) ${rSize}++;`);
172177
}
173-
178+
174179
ctx.js(/* js */ `encoder.writeObjHdr(${rSize});`);
175-
180+
176181
// Encode required fields
177182
for (const field of requiredFields) {
178183
const key = field.key;
179184
const accessor = normalizeAccessor(key);
180185
ctx.js(/* js */ `encoder.writeStr(${JSON.stringify(key)});`);
181186
encodeFn(ctx, new JsExpression(() => `${r}${accessor}`), field.value);
182187
}
183-
188+
184189
// Encode optional fields
185190
for (const field of optionalFields) {
186191
const key = field.key;
@@ -206,7 +211,7 @@ export const map = (
206211
const rKey = codegen.var();
207212
const rLen = codegen.var(`${rKeys}.length`);
208213
const ri = codegen.var('0');
209-
214+
210215
ctx.js(/* js */ `var ${rKeys} = Object.keys(${r}), ${rLen} = ${rKeys}.length, ${rKey}, ${ri} = 0;`);
211216
ctx.js(/* js */ `encoder.writeObjHdr(${rLen});`);
212217
ctx.js(/* js */ `for (; ${ri} < ${rLen}; ${ri}++) {`);
@@ -216,15 +221,12 @@ export const map = (
216221
ctx.js(`}`);
217222
};
218223

219-
export const ref = (
220-
ctx: MessagePackEncoderCodegenContext,
221-
value: JsExpression,
222-
type: Type,
223-
): void => {
224+
export const ref = (ctx: MessagePackEncoderCodegenContext, value: JsExpression, type: Type): void => {
224225
const refType = type as any; // RefType
225226
const system = ctx.options.system || refType.system;
226227
if (!system) throw new Error('NO_SYSTEM');
227-
const encoder = system.resolve(refType.schema.ref).type.encoder(ctx.encoder.format);
228+
const format = EncodingFormat.MsgPack;
229+
const encoder = system.resolve(refType.schema.ref).type.encoder(format);
228230
const d = ctx.codegen.linkDependency(encoder);
229231
ctx.js(`${d}(${value.use()}, encoder);`);
230232
};
@@ -255,11 +257,7 @@ export const or = (
255257
* Main router function that dispatches MessagePack encoding to the appropriate
256258
* encoder function based on the type's kind.
257259
*/
258-
export const generate = (
259-
ctx: MessagePackEncoderCodegenContext,
260-
value: JsExpression,
261-
type: Type,
262-
): void => {
260+
export const generate = (ctx: MessagePackEncoderCodegenContext, value: JsExpression, type: Type): void => {
263261
const kind = type.getTypeName();
264262

265263
switch (kind) {
@@ -302,4 +300,4 @@ export const generate = (
302300
default:
303301
throw new Error(`${kind} type MessagePack encoding not implemented`);
304302
}
305-
};
303+
};

0 commit comments

Comments
 (0)