Skip to content

Commit

Permalink
fix(type): same-type check for enum, fixes multiple enums in unions.
Browse files Browse the repository at this point in the history
fixes #317
  • Loading branch information
marcj committed Jul 18, 2022
1 parent 81d998c commit 291c96c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/type-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ const OPs: { [op in ReflectionOp]?: { params: number } } = {
[ReflectionOp.propertySignature]: { params: 1 },
[ReflectionOp.property]: { params: 1 },
[ReflectionOp.jump]: { params: 1 },
[ReflectionOp.enum]: { params: 1 },
[ReflectionOp.enum]: { params: 0 },
[ReflectionOp.enumMember]: { params: 1 },
[ReflectionOp.typeParameter]: { params: 1 },
[ReflectionOp.typeParameterDefault]: { params: 1 },
[ReflectionOp.mappedType]: { params: 2 },
Expand Down
20 changes: 20 additions & 0 deletions packages/type-compiler/tests/transpile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,23 @@ test('readonly array', () => {

console.log(res);
});

test('enum union', () => {
const res = transpileAndRun({
'app': `
enum StatEnginePowerUnit {
Hp,
}
enum StatWeightUnit {
Lbs,
Kg,
}
type StatMeasurementUnit = StatEnginePowerUnit | StatWeightUnit;
typeOf<StatMeasurementUnit>()
`
});

console.log(res);
});
2 changes: 1 addition & 1 deletion packages/type/src/reflection/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export class Processor {
for (; program.program < program.end; program.program++) {
const op = program.ops[program.program];

// process.stdout.write(`[${program.depth}:${program.frame.index}] step ${program.program} ${RuntimeReflectionOp[op]}\n`);
// process.stdout.write(`[${program.depth}:${program.frame.index}] step ${program.program} ${ReflectionOp[op]}\n`);
switch (op) {
case ReflectionOp.string:
this.pushType({ kind: ReflectionKind.string });
Expand Down
11 changes: 11 additions & 0 deletions packages/type/src/reflection/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,17 @@ export function isSameType(a: Type, b: Type, stack: StackEntry[] = []): boolean
return isSameType(a.return, b.return, stack);
}

if (a.kind === ReflectionKind.enum) {
if (b.kind !== ReflectionKind.enum) return false;
if (a.values.length !== b.values.length) return false;

for (let i = 0; i < a.values.length; i++) {
if (a.values[i] !== b.values[i]) return false;
}

return true;
}

if (a.kind === ReflectionKind.union) {
if (b.kind !== ReflectionKind.union) return false;
if (a.types.length !== b.types.length) return false;
Expand Down
23 changes: 22 additions & 1 deletion packages/type/tests/serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { expect, test } from '@jest/globals';
import { reflect, ReflectionClass, typeOf } from '../src/reflection/reflection';
import {
assertType,
AutoIncrement,
BackReference,
BinaryBigInt,
Expand All @@ -30,7 +31,7 @@ import { createSerializeFunction, getSerializeFunction, NamingStrategy, serializ
import { cast, deserialize, serialize } from '../src/serializer-facade';
import { getClassName } from '@deepkit/core';
import { entity, t } from '../src/decorator';
import { Alphanumeric, MaxLength, MinLength, validate, ValidationError } from '../src/validator';
import { Alphanumeric, MaxLength, MinLength, ValidationError } from '../src/validator';

test('deserializer', () => {
class User {
Expand Down Expand Up @@ -974,3 +975,23 @@ test('naming strategy camel case', () => {
expect(res).toEqual({id: 2, posts: [{id: 3, likesCount: 1}, {id: 4, likesCount: 2}]});
}
});

test('enum union', () => {
enum StatEnginePowerUnit {
Hp = 'hp',
}

enum StatWeightUnit {
Lbs = 'lbs',
Kg = 'kg',
}

type StatMeasurementUnit = StatEnginePowerUnit | StatWeightUnit;
const type = typeOf<StatMeasurementUnit>();
assertType(type, ReflectionKind.union);
expect(type.types.length).toBe(2);

expect(deserialize<StatMeasurementUnit>(StatWeightUnit.Kg)).toBe(StatWeightUnit.Kg);
expect(deserialize<StatMeasurementUnit>(StatWeightUnit.Lbs)).toBe(StatWeightUnit.Lbs);
expect(deserialize<StatMeasurementUnit>(StatEnginePowerUnit.Hp)).toBe(StatEnginePowerUnit.Hp);
});

0 comments on commit 291c96c

Please sign in to comment.