Skip to content

Commit 15923e0

Browse files
committed
feat: Update openapi transformers
1 parent 3148c6c commit 15923e0

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

cloudquery/sdk/schema/column.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ def __init__(
2323
self.not_null = not_null
2424
self.incremental_key = incremental_key
2525
self.unique = unique
26+
27+
def __str__(self) -> str:
28+
return f"Column(name={self.name}, type={self.type}, description={self.description}, primary_key={self.primary_key}, not_null={self.not_null}, incremental_key={self.incremental_key}, unique={self.unique})"
29+
30+
def __repr__(self) -> str:
31+
return f"Column(name={self.name}, type={self.type}, description={self.description}, primary_key={self.primary_key}, not_null={self.not_null}, incremental_key={self.incremental_key}, unique={self.unique})"
32+
33+
def __eq__(self, __value: object) -> bool:
34+
if type(__value) == Column:
35+
return (self.name == __value.name
36+
and self.type == __value.type
37+
and self.description == __value.description
38+
and self.primary_key == __value.primary_key
39+
and self.not_null == __value.not_null
40+
and self.incremental_key == __value.incremental_key
41+
and self.unique == __value.unique)
42+
return False
2643

2744
def to_arrow_field(self):
2845
metadata = {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .transformers import transform_list_of_dict
2+
from .openapi import oapi_definition_to_columns
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Dict, List
22
import pyarrow as pa
3+
from cloudquery.sdk.types import JSONType
34
from cloudquery.sdk.schema import Column
45

56

@@ -9,16 +10,25 @@ def oapi_type_to_arrow_type(field) -> pa.DataType:
910
return pa.string()
1011
elif oapi_type == "number":
1112
return pa.int64()
13+
elif oapi_type == "integer":
14+
return pa.int64()
1215
elif oapi_type == "boolean":
1316
return pa.bool_()
17+
elif oapi_type == "array":
18+
return JSONType()
19+
elif oapi_type == "object":
20+
return JSONType()
21+
elif oapi_type is None and "$ref" in field:
22+
return JSONType()
1423
else:
1524
return pa.string()
1625

1726

18-
def oapi_properties_to_columns(properties: Dict) -> List[Column]:
27+
def oapi_definition_to_columns(definition: Dict) -> List[Column]:
1928
columns = []
20-
for key, value in properties.items():
29+
for key, value in definition["properties"].items():
30+
column_type = oapi_type_to_arrow_type(value)
2131
columns.append(
22-
Column(name=key, type=value, description=value.get("description"))
32+
Column(name=key, type=column_type, description=value.get("description"))
2333
)
2434
return columns

tests/transformers/__init__.py

Whitespace-only changes.

tests/transformers/openapi.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import pyarrow as pa
2+
from cloudquery.sdk.transformers import oapi_definition_to_columns
3+
from cloudquery.sdk.schema import Column
4+
from cloudquery.sdk.types import JSONType
5+
6+
OAPI_SPEC = {
7+
"swagger": "2.0",
8+
"info": {
9+
"version": "2.0",
10+
"title": "Test API",
11+
"description": "Unit tests APIs",
12+
},
13+
"host": "cloudquery.io",
14+
"schemes": ["https"],
15+
"consumes": ["application/json"],
16+
"produces": ["application/json"],
17+
"paths": {},
18+
"definitions": {
19+
"TestDefinition": {
20+
"type": "object",
21+
"properties": {
22+
"string": {
23+
"type": "string",
24+
},
25+
"number": {
26+
"type": "number",
27+
},
28+
"integer": {
29+
"type": "integer",
30+
},
31+
"boolean": {
32+
"type": "boolean",
33+
},
34+
"object": {
35+
"$ref": "#/definitions/SomeDefinition",
36+
},
37+
"array": {
38+
"type": "array",
39+
"items": {"$ref": "#/definitions/SomeDefinition"},
40+
},
41+
},
42+
},
43+
},
44+
}
45+
46+
47+
def test_oapi_properties_to_columns():
48+
expected_columns = [
49+
Column("string", pa.string() , description=None),
50+
Column("number", pa.int64() , description=None),
51+
Column("integer", pa.int64() , description=None),
52+
Column("boolean", pa.bool_() , description=None),
53+
Column("object", JSONType() , description=None),
54+
Column("array", JSONType() , description=None),
55+
]
56+
columns = oapi_definition_to_columns(OAPI_SPEC["definitions"]["TestDefinition"])
57+
assert expected_columns == columns

0 commit comments

Comments
 (0)