Skip to content

Commit f8fab8c

Browse files
committed
feat chaotic: basic work on openapi client
commit_hash:7726e14d3ae11d38c0d698c169f89fa8bf89d338
1 parent cb3fa36 commit f8fab8c

26 files changed

+327
-57
lines changed

.mapping.json

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@
2727
"README.md":"taxi/uservices/userver/README.md",
2828
"SECURITY.md":"taxi/uservices/userver/SECURITY.md",
2929
"THIRD_PARTY.md":"taxi/uservices/userver/THIRD_PARTY.md",
30+
"chaotic-openapi/CMakeLists.txt":"taxi/uservices/userver/chaotic-openapi/CMakeLists.txt",
31+
"chaotic-openapi/bin/main.py":"taxi/uservices/userver/chaotic-openapi/bin/main.py",
32+
"chaotic-openapi/chaotic_openapi/__init__.py":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/__init__.py",
33+
"chaotic-openapi/chaotic_openapi/back/__init__.py":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/back/__init__.py",
34+
"chaotic-openapi/chaotic_openapi/back/cpp_client/__init__.py":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/back/cpp_client/__init__.py",
35+
"chaotic-openapi/chaotic_openapi/back/cpp_client/renderer.py":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/back/cpp_client/renderer.py",
36+
"chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client.hpp.jinja":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client.hpp.jinja",
37+
"chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client_impl.cpp.jinja":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client_impl.cpp.jinja",
38+
"chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client_impl.hpp.jinja":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client_impl.hpp.jinja",
39+
"chaotic-openapi/chaotic_openapi/back/cpp_client/types.py":"taxi/uservices/userver/chaotic-openapi/chaotic_openapi/back/cpp_client/types.py",
40+
"chaotic-openapi/include/userver/chaotic/openapi/parameters.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/parameters.hpp",
41+
"chaotic-openapi/include/userver/chaotic/openapi/parameters_read.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/parameters_read.hpp",
42+
"chaotic-openapi/include/userver/chaotic/openapi/parameters_write.hpp":"taxi/uservices/userver/chaotic-openapi/include/userver/chaotic/openapi/parameters_write.hpp",
43+
"chaotic-openapi/src/chaotic/openapi/parameters.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/parameters.cpp",
44+
"chaotic-openapi/src/chaotic/openapi/parameters_read.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/parameters_read.cpp",
45+
"chaotic-openapi/src/chaotic/openapi/parameters_read_test.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/parameters_read_test.cpp",
46+
"chaotic-openapi/src/chaotic/openapi/parameters_write.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/parameters_write.cpp",
47+
"chaotic-openapi/src/chaotic/openapi/parameters_write_test.cpp":"taxi/uservices/userver/chaotic-openapi/src/chaotic/openapi/parameters_write_test.cpp",
3048
"chaotic/CMakeLists.txt":"taxi/uservices/userver/chaotic/CMakeLists.txt",
3149
"chaotic/bin/chaotic-gen":"taxi/uservices/userver/chaotic/bin/chaotic-gen",
3250
"chaotic/bin/main.py":"taxi/uservices/userver/chaotic/bin/main.py",
@@ -49,11 +67,13 @@
4967
"chaotic/chaotic/compilers/dynamic_config/templates/taxi_config.hpp.jinja":"taxi/uservices/userver/chaotic/chaotic/compilers/dynamic_config/templates/taxi_config.hpp.jinja",
5068
"chaotic/chaotic/compilers/dynamic_config/templates/variable.cpp.jinja":"taxi/uservices/userver/chaotic/chaotic/compilers/dynamic_config/templates/variable.cpp.jinja",
5169
"chaotic/chaotic/compilers/dynamic_config/templates/variable.hpp.jinja":"taxi/uservices/userver/chaotic/chaotic/compilers/dynamic_config/templates/variable.hpp.jinja",
70+
"chaotic/chaotic/cpp_format.py":"taxi/uservices/userver/chaotic/chaotic/cpp_format.py",
5271
"chaotic/chaotic/error.py":"taxi/uservices/userver/chaotic/chaotic/error.py",
5372
"chaotic/chaotic/front/__init__.py":"taxi/uservices/userver/chaotic/chaotic/front/__init__.py",
5473
"chaotic/chaotic/front/parser.py":"taxi/uservices/userver/chaotic/chaotic/front/parser.py",
5574
"chaotic/chaotic/front/ref_resolver.py":"taxi/uservices/userver/chaotic/chaotic/front/ref_resolver.py",
5675
"chaotic/chaotic/front/types.py":"taxi/uservices/userver/chaotic/chaotic/front/types.py",
76+
"chaotic/chaotic/jinja_env.py":"taxi/uservices/userver/chaotic/chaotic/jinja_env.py",
5777
"chaotic/chaotic/main.py":"taxi/uservices/userver/chaotic/chaotic/main.py",
5878
"chaotic/golden_tests/CMakeLists.txt":"taxi/uservices/userver/chaotic/golden_tests/CMakeLists.txt",
5979
"chaotic/golden_tests/output/schemas/allof/allof.cpp":"taxi/uservices/userver/chaotic/golden_tests/output/schemas/allof/allof.cpp",
@@ -119,9 +139,6 @@
119139
"chaotic/include/userver/chaotic/io/userver/utils/strong_typedef.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/io/userver/utils/strong_typedef.hpp",
120140
"chaotic/include/userver/chaotic/object.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/object.hpp",
121141
"chaotic/include/userver/chaotic/oneof_with_discriminator.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/oneof_with_discriminator.hpp",
122-
"chaotic/include/userver/chaotic/openapi/parameters.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/openapi/parameters.hpp",
123-
"chaotic/include/userver/chaotic/openapi/parameters_read.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/openapi/parameters_read.hpp",
124-
"chaotic/include/userver/chaotic/openapi/parameters_write.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/openapi/parameters_write.hpp",
125142
"chaotic/include/userver/chaotic/primitive.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/primitive.hpp",
126143
"chaotic/include/userver/chaotic/ref.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/ref.hpp",
127144
"chaotic/include/userver/chaotic/timepoint_tz.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/timepoint_tz.hpp",
@@ -183,11 +200,6 @@
183200
"chaotic/src/chaotic/io/userver/utils/datetime/date.cpp":"taxi/uservices/userver/chaotic/src/chaotic/io/userver/utils/datetime/date.cpp",
184201
"chaotic/src/chaotic/io/userver/utils/datetime/time_point_tz.cpp":"taxi/uservices/userver/chaotic/src/chaotic/io/userver/utils/datetime/time_point_tz.cpp",
185202
"chaotic/src/chaotic/io/userver/utils/datetime/time_point_tz_iso_basic.cpp":"taxi/uservices/userver/chaotic/src/chaotic/io/userver/utils/datetime/time_point_tz_iso_basic.cpp",
186-
"chaotic/src/chaotic/openapi/parameters.cpp":"taxi/uservices/userver/chaotic/src/chaotic/openapi/parameters.cpp",
187-
"chaotic/src/chaotic/openapi/parameters_read.cpp":"taxi/uservices/userver/chaotic/src/chaotic/openapi/parameters_read.cpp",
188-
"chaotic/src/chaotic/openapi/parameters_read_test.cpp":"taxi/uservices/userver/chaotic/src/chaotic/openapi/parameters_read_test.cpp",
189-
"chaotic/src/chaotic/openapi/parameters_write.cpp":"taxi/uservices/userver/chaotic/src/chaotic/openapi/parameters_write.cpp",
190-
"chaotic/src/chaotic/openapi/parameters_write_test.cpp":"taxi/uservices/userver/chaotic/src/chaotic/openapi/parameters_write_test.cpp",
191203
"chaotic/tests/back/cpp/conftest.py":"taxi/uservices/userver/chaotic/tests/back/cpp/conftest.py",
192204
"chaotic/tests/back/cpp/test_external.py":"taxi/uservices/userver/chaotic/tests/back/cpp/test_external.py",
193205
"chaotic/tests/back/cpp/test_tr_array.py":"taxi/uservices/userver/chaotic/tests/back/cpp/test_tr_array.py",
@@ -388,6 +400,7 @@
388400
"cmake/get_cpm.cmake":"taxi/uservices/userver/cmake/get_cpm.cmake",
389401
"cmake/install/Config.cmake.in":"taxi/uservices/userver/cmake/install/Config.cmake.in",
390402
"cmake/install/userver-chaotic-config.cmake":"taxi/uservices/userver/cmake/install/userver-chaotic-config.cmake",
403+
"cmake/install/userver-chaotic-openapi-config.cmake":"taxi/uservices/userver/cmake/install/userver-chaotic-openapi-config.cmake",
391404
"cmake/install/userver-clickhouse-config.cmake":"taxi/uservices/userver/cmake/install/userver-clickhouse-config.cmake",
392405
"cmake/install/userver-core-config.cmake":"taxi/uservices/userver/cmake/install/userver-core-config.cmake",
393406
"cmake/install/userver-easy-config.cmake":"taxi/uservices/userver/cmake/install/userver-easy-config.cmake",

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ endif()
186186

187187
if (USERVER_FEATURE_CHAOTIC)
188188
add_subdirectory(chaotic)
189+
add_subdirectory(chaotic-openapi)
189190
list(APPEND USERVER_AVAILABLE_COMPONENTS chaotic)
190191
endif()
191192

chaotic-openapi/CMakeLists.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
project(userver-chaotic-openapi CXX)
2+
3+
include(ChaoticGen)
4+
5+
userver_module(chaotic-openapi
6+
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
7+
LINK_LIBRARIES userver-core userver-chaotic
8+
9+
UTEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*_test.cpp"
10+
UTEST_LINK_LIBRARIES userver-core userver-chaotic
11+
)
12+
13+
if(USERVER_BUILD_TESTS)
14+
# userver_venv_setup(
15+
# NAME userver-chaotic-tests
16+
# PYTHON_OUTPUT_VAR USERVER_CHAOTIC_PYTEST_PYTHON_BINARY
17+
# REQUIREMENTS
18+
# "${USERVER_ROOT_DIR}/scripts/chaotic/requirements.txt"
19+
# "${USERVER_ROOT_DIR}/testsuite/requirements.txt"
20+
# UNIQUE
21+
# )
22+
# add_test(
23+
# NAME chaotic-tests-pytest
24+
# COMMAND
25+
# env "PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}"
26+
# "${USERVER_CHAOTIC_PYTEST_PYTHON_BINARY}" -m pytest
27+
# -vvs
28+
# "${CMAKE_CURRENT_SOURCE_DIR}/tests"
29+
# )
30+
31+
# add_subdirectory(integration_tests)
32+
# add_subdirectory(golden_tests)
33+
endif()
34+
35+
_userver_directory_install(COMPONENT chaotic-openapi
36+
DIRECTORY
37+
"${CMAKE_CURRENT_SOURCE_DIR}/chaotic_openapi"
38+
DESTINATION
39+
"${CMAKE_INSTALL_LIBDIR}/userver"
40+
)

chaotic-openapi/bin/main.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from chaotic_openapi.back.cpp_client import renderer
2+
from chaotic_openapi.back.cpp_client import types
3+
4+
from chaotic.back.cpp import type_name
5+
from chaotic.back.cpp import types as cpp_types
6+
7+
8+
def main():
9+
ctx = renderer.Context(generate_path='', clang_format_bin='')
10+
spec = types.ClientSpec(
11+
client_name='test',
12+
cpp_namespace='clients::test',
13+
operations=[
14+
types.Operation(
15+
method='POST',
16+
path='testme',
17+
description='A testing method to call',
18+
parameters=[
19+
types.Parameter(
20+
raw_name='number',
21+
cpp_name='number',
22+
parser='TrivialParameter',
23+
),
24+
],
25+
request_bodies=[
26+
types.RequestBody(
27+
content_type='text/plaintext',
28+
schema=cpp_types.CppPrimitiveType(
29+
raw_cpp_type=type_name.TypeName('int'),
30+
user_cpp_type=None,
31+
json_schema=None,
32+
nullable=False,
33+
validators=cpp_types.CppPrimitiveValidator(
34+
prefix='/definitions/type',
35+
),
36+
),
37+
),
38+
],
39+
),
40+
],
41+
)
42+
outputs = renderer.render(spec, ctx)
43+
for output in outputs:
44+
print(f'\n{output.rel_path}\n====\n{output.content}')
45+
46+
47+
if __name__ == '__main__':
48+
main()

chaotic-openapi/chaotic_openapi/__init__.py

Whitespace-only changes.

chaotic-openapi/chaotic_openapi/back/__init__.py

Whitespace-only changes.

chaotic-openapi/chaotic_openapi/back/cpp_client/__init__.py

Whitespace-only changes.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import dataclasses
2+
import os
3+
import pathlib
4+
5+
import jinja2
6+
7+
from chaotic import cpp_format
8+
from chaotic import jinja_env
9+
from chaotic_openapi.back.cpp_client import types
10+
11+
PARENT_DIR = os.path.dirname(__file__)
12+
13+
TEMPLATE_NAMES = [
14+
'client.hpp',
15+
# no 'client.cpp',
16+
'client_impl.hpp',
17+
'client_impl.cpp',
18+
# 'requests.hpp',
19+
# 'requests.cpp',
20+
# 'responses.hpp',
21+
# 'responses.cpp',
22+
# 'exceptions.hpp',
23+
]
24+
25+
26+
@dataclasses.dataclass
27+
class Context:
28+
generate_path: pathlib.Path
29+
clang_format_bin: str
30+
31+
32+
@dataclasses.dataclass
33+
class CppOutput:
34+
rel_path: str
35+
content: str
36+
37+
def save(self, prefix: str) -> None:
38+
with open(prefix + self.rel_path, 'w') as ofile:
39+
ofile.write(self.content)
40+
41+
42+
def make_env() -> jinja2.Environment:
43+
env = jinja_env.make_env(
44+
'chaotic-openapi/chaotic_openapi/back/cpp_client',
45+
os.path.join(PARENT_DIR),
46+
)
47+
48+
return env
49+
50+
51+
JINJA_ENV = make_env()
52+
53+
54+
def render(spec: types.ClientSpec, context: Context) -> None:
55+
env = {'namespace': spec.cpp_namespace, 'operations': spec.operations}
56+
57+
output = []
58+
for name in TEMPLATE_NAMES:
59+
tpl = JINJA_ENV.get_template(f'templates/{name}.jinja')
60+
pp = tpl.render(**env)
61+
pp = cpp_format.format_pp(pp, binary=context.clang_format_bin)
62+
63+
if name.endswith('.hpp'):
64+
rel_path = f'include/client/{spec.client_name}/{name}'
65+
else:
66+
rel_path = f'src/client/{spec.client_name}/{name}'
67+
68+
output.append(CppOutput(rel_path=rel_path, content=pp))
69+
return output
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
namespace {{ namespace }} {
4+
5+
class Client {
6+
public:
7+
{% for op in operations %}
8+
{# TODO: description #}
9+
virtual {{ op.cpp_namespace() }}::Response {{ op.cpp_method_name() }}(
10+
{# TODO: parameters #}
11+
) = 0;
12+
{% endfor %}
13+
};
14+
15+
}

chaotic-openapi/chaotic_openapi/back/cpp_client/templates/client_impl.cpp.jinja

Whitespace-only changes.

0 commit comments

Comments
 (0)