-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopenapi.ts
More file actions
70 lines (64 loc) · 1.78 KB
/
openapi.ts
File metadata and controls
70 lines (64 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { DataType } from '@apache-arrow/esnext-esm';
import { Utf8, Int64, Bool } from '@apache-arrow/esnext-esm';
import type { Column } from '../schema/column.js';
import { createColumn } from '../schema/column.js';
import { JSONType } from '../types/json.js';
interface OAPIProperty {
type?: string;
description?: string;
$ref?: string;
items?: {
$ref: string;
};
}
interface OAPIDefinition {
properties: {
[key: string]: OAPIProperty;
};
}
function oapiTypeToArrowType(field: OAPIProperty): DataType {
const oapiType = field.type;
switch (oapiType) {
case 'string': {
return new Utf8();
}
case 'number':
case 'integer': {
return new Int64();
}
case 'boolean': {
return new Bool();
}
case 'array':
case 'object': {
return new JSONType();
}
default: {
return !oapiType && '$ref' in field ? new JSONType() : new Utf8();
}
}
}
export function getColumnByName(columns: Column[], name: string): Column | undefined {
for (const column of columns) {
if (column.name === name) {
return column;
}
}
return undefined;
}
export function oapiDefinitionToColumns(definition: OAPIDefinition, overrideColumns: Column[] = []): Column[] {
const columns: Column[] = [];
for (const key in definition.properties) {
const value = definition.properties[key];
const columnType = oapiTypeToArrowType(value);
const column = createColumn({ name: key, type: columnType, description: value.description });
const overrideColumn = getColumnByName(overrideColumns, key);
if (overrideColumn) {
column.type = overrideColumn.type;
column.primaryKey = overrideColumn.primaryKey;
column.unique = overrideColumn.unique;
}
columns.push(column);
}
return columns;
}