Skip to content

Commit

Permalink
fix conversions for equivalent data types #420
Browse files Browse the repository at this point in the history
  • Loading branch information
oguimbal committed Oct 11, 2024
1 parent 8df81cd commit 6e80c58
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/datatypes/datatype-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export abstract class TypeBase<TRaw = any> implements _IType<TRaw>, _RelationBas
get name(): string {
return this.primary;
}
get primaryName(): string {
return this.primary;
}


/** Compute a custom unicty hash for a non null value */
Expand Down
17 changes: 13 additions & 4 deletions src/datatypes/t-equivalent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Types } from './datatypes';

export class EquivalentType extends TypeBase<string> {

private equiv: IType;
private equiv: _IType;

constructor(private def: IEquivalentType) {
super(null);
Expand All @@ -17,7 +17,7 @@ export class EquivalentType extends TypeBase<string> {
}
this.equiv = eq;
} else {
this.equiv = def.equivalentTo;
this.equiv = def.equivalentTo as _IType;
}

if (!this.equiv) {
Expand All @@ -26,7 +26,15 @@ export class EquivalentType extends TypeBase<string> {
}

get primary(): DataType {
return this.def.name as any;
return this.equiv.primary;
}

get primaryName(): string {
return this.def.name;
}

get name(): string {
return this.def.name;
}

doCanCast(to: _IType) {
Expand All @@ -42,7 +50,8 @@ export class EquivalentType extends TypeBase<string> {
}

doCanBuildFrom(from: _IType): boolean | nil {
return from.primary === this.equiv.primary;
// return from.canCast(this.equiv);
return this.equiv.canCast(from);
}

doBuildFrom(value: Evaluator<string>, from: _IType<string>): Evaluator<string> | nil {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ export interface _IType<TRaw = any> extends IType, _RelationBase {
readonly type: 'type';
/** Data type */
readonly primary: DataType;
readonly primaryName: string;
/** Reg type name */
readonly name: string; // | null;
readonly reg: Reg;
Expand Down
6 changes: 3 additions & 3 deletions src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,10 @@ export class DbSchema implements _ISchema, ISchema {
}

_registerType(type: _IType): this {
if (this.simpleTypes[type.primary] || this.sizeableTypes[type.primary] || this.getOwnObject(type.primary)) {
throw new QueryError(`type "${type.primary}" already exists`);
if (this.simpleTypes[type.primaryName] || this.sizeableTypes[type.primaryName] || this.getOwnObject(type.primaryName)) {
throw new QueryError(`type "${type.primaryName}" already exists`);
}
this.simpleTypes[type.primary] = type;
this.simpleTypes[type.primaryName] = type;
this._reg_register(type);
return this;
}
Expand Down
21 changes: 20 additions & 1 deletion src/tests/conversions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, beforeEach, expect } from 'bun:test';

import { newDb } from '../db';

import { IMemoryDb } from '../interfaces-private';
import { DataType, IMemoryDb } from '../interfaces-private';
import { expectQueryError, expectSingle } from './test-utils';

describe('Conversions', () => {
Expand Down Expand Up @@ -246,4 +246,23 @@ describe('Conversions', () => {
`), /subquery must return only one column/)

})

it('insert float4', () => {
// see issue https://github.com/oguimbal/pg-mem/issues/420
db.public.registerEquivalentType({
name: 'float4',
// which type is it equivalent to (will be able to cast it from it)
equivalentTo: DataType.float,
isValid(val: any) {
// Validate that the value is a valid number
return typeof val === 'number' && !isNaN(val);
},
});
none(`CREATE TABLE task_assignments (
id serial PRIMARY KEY,
progress float4
);
INSERT INTO task_assignments (progress) VALUES (39), (79);`);
})
});
3 changes: 1 addition & 2 deletions src/tests/custom-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ describe('Custom types', () => {
});

none(`SELECT 'something'::custom`);

expectQueryError(() => none(`SELECT 'throw'::custom`), /Nope/);
expectQueryError(() => none(`SELECT 'whatever'::custom`), /invalid input syntax for type custom/);
expectQueryError(() => none(`SELECT 42::custom`), /cannot cast type integer to custom/);
expectQueryError(() => none(`SELECT 42::custom`), /invalid input syntax for type custom/);
});

it('can register custom type with length', () => {
Expand Down

0 comments on commit 6e80c58

Please sign in to comment.