Skip to content

Commit 2feca44

Browse files
authored
refactor: Make column a plain object (#30)
1 parent bfbb583 commit 2feca44

File tree

3 files changed

+86
-65
lines changed

3 files changed

+86
-65
lines changed

src/schema/column.ts

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,70 @@
1-
import { DataType, Field } from '@apache-arrow/esnext-esm';
1+
import { isDeepStrictEqual } from 'node:util';
2+
3+
import { DataType, Field, Bool } from '@apache-arrow/esnext-esm';
24

35
import * as arrow from './arrow.js';
46

5-
export class Column {
7+
export type Column = {
68
name: string;
79
type: DataType;
810
description: string;
911
primaryKey: boolean;
1012
notNull: boolean;
1113
incrementalKey: boolean;
1214
unique: boolean;
15+
};
1316

14-
constructor(
15-
name: string,
16-
type: DataType,
17-
description: string = '',
18-
primaryKey: boolean = false,
19-
notNull: boolean = false,
20-
incrementalKey: boolean = false,
21-
unique: boolean = false,
22-
) {
23-
this.name = name;
24-
this.type = type;
25-
this.description = description;
26-
this.primaryKey = primaryKey;
27-
this.notNull = notNull;
28-
this.incrementalKey = incrementalKey;
29-
this.unique = unique;
30-
}
31-
32-
toString(): string {
33-
return `Column(name=${this.name}, type=${this.type}, description=${this.description}, primary_key=${this.primaryKey}, not_null=${this.notNull}, incremental_key=${this.incrementalKey}, unique=${this.unique})`;
34-
}
17+
export const createColumn = ({
18+
name = '',
19+
type = new Bool(),
20+
description = '',
21+
incrementalKey = false,
22+
notNull = false,
23+
primaryKey = false,
24+
unique = false,
25+
}: Partial<Column> = {}): Column => ({
26+
name,
27+
type,
28+
description,
29+
primaryKey,
30+
notNull,
31+
incrementalKey,
32+
unique,
33+
});
3534

36-
// JavaScript (and TypeScript) uses a single method for both string representation and debugging output
37-
toJSON(): string {
38-
return this.toString();
39-
}
35+
export const formatColumn = (column: Column): string => {
36+
const { name, type, description, primaryKey, notNull, incrementalKey, unique } = column;
37+
return `Column(name=${name}, type=${type}, description=${description}, primary_key=${primaryKey}, not_null=${notNull}, incremental_key=${incrementalKey}, unique=${unique})`;
38+
};
4039

41-
equals(value: object): boolean {
42-
if (value instanceof Column) {
43-
return (
44-
this.name === value.name &&
45-
this.type === value.type &&
46-
this.description === value.description &&
47-
this.primaryKey === value.primaryKey &&
48-
this.notNull === value.notNull &&
49-
this.incrementalKey === value.incrementalKey &&
50-
this.unique === value.unique
51-
);
52-
}
53-
return false;
54-
}
40+
export const equals = (column: Column, other: unknown): boolean => {
41+
return isDeepStrictEqual(column, other);
42+
};
5543

56-
toArrowField(): Field {
57-
const metadataMap = new Map<string, string>();
58-
metadataMap.set(arrow.METADATA_PRIMARY_KEY, this.primaryKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
59-
metadataMap.set(arrow.METADATA_UNIQUE, this.unique ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
60-
metadataMap.set(arrow.METADATA_INCREMENTAL, this.incrementalKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
44+
export const toArrowField = (column: Column): Field => {
45+
const { name, type, notNull, primaryKey, unique, incrementalKey } = column;
46+
const metadataMap = new Map<string, string>();
47+
metadataMap.set(arrow.METADATA_PRIMARY_KEY, primaryKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
48+
metadataMap.set(arrow.METADATA_UNIQUE, unique ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
49+
metadataMap.set(arrow.METADATA_INCREMENTAL, incrementalKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
6150

62-
return new Field(this.name, this.type, /*nullable=*/ !this.notNull, metadataMap);
63-
}
51+
return new Field(name, type, /*nullable=*/ !notNull, metadataMap);
52+
};
6453

65-
static fromArrowField(field: Field): Column {
66-
const metadata = field.metadata;
67-
const primaryKey = metadata.get(arrow.METADATA_PRIMARY_KEY) === arrow.METADATA_TRUE;
68-
const unique = metadata.get(arrow.METADATA_UNIQUE) === arrow.METADATA_TRUE;
69-
const incrementalKey = metadata.get(arrow.METADATA_INCREMENTAL) === arrow.METADATA_TRUE;
54+
export const fromArrowField = (field: Field): Column => {
55+
const { name, type, nullable } = field;
56+
const metadata = field.metadata;
57+
const primaryKey = metadata.get(arrow.METADATA_PRIMARY_KEY) === arrow.METADATA_TRUE;
58+
const unique = metadata.get(arrow.METADATA_UNIQUE) === arrow.METADATA_TRUE;
59+
const incrementalKey = metadata.get(arrow.METADATA_INCREMENTAL) === arrow.METADATA_TRUE;
7060

71-
return new Column(field.name, field.type, '', primaryKey, !field.nullable, unique, incrementalKey);
72-
}
73-
}
61+
return {
62+
name,
63+
type,
64+
description: '',
65+
primaryKey,
66+
notNull: !nullable,
67+
unique,
68+
incrementalKey,
69+
};
70+
};

src/transformers/openapi.test.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Utf8, Int64, Bool } from '@apache-arrow/esnext-esm';
22
import test from 'ava';
33

4-
import { Column } from '../schema/column.js';
4+
import { Column, createColumn } from '../schema/column.js';
55
import { JSONType } from '../types/json.js';
66

77
import { oapiDefinitionToColumns } from './openapi.js';
@@ -49,12 +49,36 @@ const OAPI_SPEC = {
4949

5050
test('should parse spec as expected', (t) => {
5151
const expectedColumns: Column[] = [
52-
new Column('string', new Utf8(), ''),
53-
new Column('number', new Int64(), ''),
54-
new Column('integer', new Int64(), ''),
55-
new Column('boolean', new Bool(), ''),
56-
new Column('object', new JSONType(), ''),
57-
new Column('array', new JSONType(), ''),
52+
createColumn({
53+
name: 'string',
54+
type: new Utf8(),
55+
description: '',
56+
}),
57+
createColumn({
58+
name: 'number',
59+
type: new Int64(),
60+
description: '',
61+
}),
62+
createColumn({
63+
name: 'integer',
64+
type: new Int64(),
65+
description: '',
66+
}),
67+
createColumn({
68+
name: 'boolean',
69+
type: new Bool(),
70+
description: '',
71+
}),
72+
createColumn({
73+
name: 'object',
74+
type: new JSONType(),
75+
description: '',
76+
}),
77+
createColumn({
78+
name: 'array',
79+
type: new JSONType(),
80+
description: '',
81+
}),
5882
];
5983

6084
const columns = oapiDefinitionToColumns(OAPI_SPEC['definitions']['TestDefinition']);

src/transformers/openapi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DataType, Utf8, Int64, Bool } from '@apache-arrow/esnext-esm';
22

3-
import { Column } from '../schema/column.js';
3+
import { Column, createColumn } from '../schema/column.js';
44
import { JSONType } from '../types/json.js';
55

66
interface OAPIProperty {
@@ -55,7 +55,7 @@ export function oapiDefinitionToColumns(definition: OAPIDefinition, overrideColu
5555
for (const key in definition.properties) {
5656
const value = definition.properties[key];
5757
const columnType = oapiTypeToArrowType(value);
58-
const column = new Column(key, columnType, value.description);
58+
const column = createColumn({ name: key, type: columnType, description: value.description });
5959
const overrideColumn = getColumnByName(overrideColumns, key);
6060
if (overrideColumn) {
6161
column.type = overrideColumn.type;

0 commit comments

Comments
 (0)