stinger-ipc 0.0.1__py3-none-any.whl → 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {stinger_ipc-0.0.1.dist-info → stinger_ipc-0.0.3.dist-info}/METADATA +4 -4
- stinger_ipc-0.0.3.dist-info/RECORD +52 -0
- stinger_ipc-0.0.3.dist-info/entry_points.txt +4 -0
- stingeripc/args.py +6 -5
- stingeripc/asyncapi.py +249 -167
- stingeripc/components.py +301 -136
- stingeripc/connection.py +2 -1
- stingeripc/exceptions.py +1 -2
- stingeripc/interface.py +8 -4
- stingeripc/lang_symb.py +68 -0
- stingeripc/templates/cpp/CMakeLists.txt.jinja2 +26 -0
- stingeripc/templates/cpp/examples/client_main.cpp.jinja2 +47 -0
- stingeripc/templates/cpp/examples/server_main.cpp.jinja2 +35 -0
- stingeripc/templates/cpp/include/broker.hpp.jinja2 +132 -0
- stingeripc/templates/cpp/include/client.hpp.jinja2 +53 -0
- stingeripc/templates/cpp/include/enums.hpp.jinja2 +17 -0
- stingeripc/templates/cpp/include/ibrokerconnection.hpp.jinja2 +42 -0
- stingeripc/templates/cpp/include/return_types.hpp.jinja2 +14 -0
- stingeripc/templates/cpp/include/server.hpp.jinja2 +44 -0
- stingeripc/templates/cpp/include/structs.hpp.jinja2 +13 -0
- stingeripc/templates/cpp/src/broker.cpp.jinja2 +243 -0
- stingeripc/templates/cpp/src/client.cpp.jinja2 +202 -0
- stingeripc/templates/cpp/src/server.cpp.jinja2 +170 -0
- stingeripc/templates/markdown/index.md.jinja2 +142 -0
- stingeripc/templates/python/__init__.py.jinja2 +1 -0
- stingeripc/templates/python/client.py.jinja2 +309 -0
- stingeripc/templates/python/connection.py.jinja2 +164 -0
- stingeripc/templates/python/interface_types.py.jinja2 +48 -0
- stingeripc/templates/python/method_codes.py.jinja2 +30 -0
- stingeripc/templates/python/pyproject.toml.jinja2 +9 -0
- stingeripc/templates/python/server.py.jinja2 +214 -0
- stingeripc/templates/rust/Cargo.toml.jinja2 +4 -0
- stingeripc/templates/rust/client/Cargo.toml.jinja2 +25 -0
- stingeripc/templates/rust/client/examples/client.rs.jinja2 +53 -0
- stingeripc/templates/rust/client/src/lib.rs.jinja2 +247 -0
- stingeripc/templates/rust/connection/Cargo.toml.jinja2 +21 -0
- stingeripc/templates/rust/connection/examples/pub_and_recv.rs.jinja2 +44 -0
- stingeripc/templates/rust/connection/src/handler.rs.jinja2 +0 -0
- stingeripc/templates/rust/connection/src/lib.rs.jinja2 +262 -0
- stingeripc/templates/rust/connection/src/payloads.rs.jinja2 +131 -0
- stingeripc/templates/rust/server/Cargo.toml.jinja2 +19 -0
- stingeripc/templates/rust/server/examples/server.rs.jinja2 +83 -0
- stingeripc/templates/rust/server/src/lib.rs.jinja2 +272 -0
- stingeripc/tools/__init__.py +0 -0
- stingeripc/tools/markdown_generator.py +25 -0
- stingeripc/tools/python_generator.py +41 -0
- stingeripc/tools/rust_generator.py +50 -0
- stingeripc/topic.py +11 -8
- stinger_ipc-0.0.1.dist-info/RECORD +0 -13
- {stinger_ipc-0.0.1.dist-info → stinger_ipc-0.0.3.dist-info}/WHEEL +0 -0
- {stinger_ipc-0.0.1.dist-info → stinger_ipc-0.0.3.dist-info}/licenses/LICENSE +0 -0
- {stinger_ipc-0.0.1.dist-info → stinger_ipc-0.0.3.dist-info}/top_level.txt +0 -0
stingeripc/asyncapi.py
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
Provides the functionality needed to create an AsyncAPI service specification from a Stinger file.
|
3
3
|
"""
|
4
4
|
|
5
|
-
|
6
5
|
import sys
|
7
6
|
from jacobsjinjatoo import templator as jj2
|
8
7
|
from jacobsjinjatoo import stringmanip
|
@@ -13,6 +12,7 @@ from collections import OrderedDict
|
|
13
12
|
from .components import StingerSpec, Arg, ArgPrimitive, ArgEnum, ArgStruct
|
14
13
|
from .args import ArgType, ArgPrimitiveType
|
15
14
|
|
15
|
+
|
16
16
|
class Direction(Enum):
|
17
17
|
SERVER_PUBLISHES = 1
|
18
18
|
SERVER_SUBSCRIBES = 2
|
@@ -30,44 +30,41 @@ class ObjectSchema:
|
|
30
30
|
self._properties: dict[str, Any] = dict()
|
31
31
|
self._required = set()
|
32
32
|
self._dependent_schemas = {}
|
33
|
-
|
34
|
-
def add_value_property(
|
35
|
-
|
36
|
-
|
37
|
-
}
|
33
|
+
|
34
|
+
def add_value_property(
|
35
|
+
self, name: str, arg_primitive_type: ArgPrimitiveType, required=True
|
36
|
+
):
|
37
|
+
schema = {"type": ArgPrimitiveType.to_json_type(arg_primitive_type)}
|
38
38
|
self._properties[name] = schema
|
39
39
|
if required:
|
40
40
|
self._required.add(name)
|
41
41
|
|
42
42
|
def add_value_dependency(self, name: str, required_on_name: str, required_on_value):
|
43
43
|
self._dependent_schemas[name] = {
|
44
|
-
"properties": {
|
45
|
-
required_on_name: {
|
46
|
-
"const": required_on_value
|
47
|
-
}
|
48
|
-
}
|
44
|
+
"properties": {required_on_name: {"const": required_on_value}}
|
49
45
|
}
|
50
46
|
|
51
|
-
def add_const_value_property(
|
47
|
+
def add_const_value_property(
|
48
|
+
self, name: str, arg_type: ArgPrimitiveType, const_value, required=True
|
49
|
+
):
|
52
50
|
self.add_value_property(name, arg_type, required)
|
53
|
-
self._properties[name][
|
51
|
+
self._properties[name]["const"] = const_value
|
54
52
|
|
55
|
-
def add_enum_value_property(
|
53
|
+
def add_enum_value_property(
|
54
|
+
self, name: str, arg_type: ArgPrimitiveType, possible_values, required=True
|
55
|
+
):
|
56
56
|
self.add_value_property(name, arg_type, required)
|
57
|
-
self._properties[name][
|
57
|
+
self._properties[name]["enum"] = possible_values
|
58
58
|
|
59
59
|
def add_reference_property(self, name: str, dollar_ref: str, required=True):
|
60
|
-
schema = {
|
61
|
-
"$ref": dollar_ref
|
62
|
-
}
|
60
|
+
schema = {"$ref": dollar_ref}
|
63
61
|
self._properties[name] = schema
|
64
62
|
if required:
|
65
63
|
self._required.add(name)
|
66
64
|
|
67
|
-
|
68
|
-
def to_schema(self) -> dict[str, str|dict[str, Any]|list[str]]:
|
65
|
+
def to_schema(self) -> dict[str, str | dict[str, Any] | list[str]]:
|
69
66
|
props: dict[str, Any] = dict()
|
70
|
-
schema: dict[str, str|dict[str, Any]|list[str]] = {
|
67
|
+
schema: dict[str, str | dict[str, Any] | list[str]] = {
|
71
68
|
"type": "object",
|
72
69
|
"properties": props,
|
73
70
|
"required": sorted(list(self._required)),
|
@@ -76,10 +73,11 @@ class ObjectSchema:
|
|
76
73
|
props[prop_name] = prop_schema
|
77
74
|
return schema
|
78
75
|
|
76
|
+
|
79
77
|
class Message(object):
|
80
78
|
"""The information needed to create an AsyncAPI Message structure."""
|
81
79
|
|
82
|
-
def __init__(self, message_name: str, schema: str|None = None):
|
80
|
+
def __init__(self, message_name: str, schema: str | None = None):
|
83
81
|
self.name = message_name
|
84
82
|
self.schema = schema or {"type": "null"}
|
85
83
|
self._traits: list[dict[str, Any]] = list()
|
@@ -95,7 +93,7 @@ class Message(object):
|
|
95
93
|
def add_trait(self, trait):
|
96
94
|
self._traits.append(trait)
|
97
95
|
|
98
|
-
def add_header(self, name: str, schema: dict[str, Any], required: bool=False):
|
96
|
+
def add_header(self, name: str, schema: dict[str, Any], required: bool = False):
|
99
97
|
self._headers[name] = (required, schema)
|
100
98
|
|
101
99
|
def get_message(self) -> dict[str, Any]:
|
@@ -106,10 +104,12 @@ class Message(object):
|
|
106
104
|
if len(self._traits) > 0:
|
107
105
|
msg["traits"] = self._traits
|
108
106
|
if len(self._headers) > 0:
|
109
|
-
msg["headers"] = OrderedDict(
|
110
|
-
|
111
|
-
|
112
|
-
|
107
|
+
msg["headers"] = OrderedDict(
|
108
|
+
{
|
109
|
+
"properties": OrderedDict(),
|
110
|
+
"required": list(),
|
111
|
+
}
|
112
|
+
)
|
113
113
|
for header_name, (required, schema) in self._headers.items():
|
114
114
|
msg["headers"]["properties"][header_name] = schema
|
115
115
|
if required:
|
@@ -125,14 +125,14 @@ class Channel(object):
|
|
125
125
|
topic: str,
|
126
126
|
name: str,
|
127
127
|
direction: Direction,
|
128
|
-
message_name: str|None = None,
|
128
|
+
message_name: str | None = None,
|
129
129
|
):
|
130
130
|
self.topic = topic
|
131
131
|
self.name = name
|
132
132
|
self.direction = direction
|
133
133
|
self.message_name = message_name or name
|
134
134
|
self.mqtt = {"qos": 1, "retain": False}
|
135
|
-
self.description: str|None = None
|
135
|
+
self.description: str | None = None
|
136
136
|
self.parameters: dict[str, str] = dict()
|
137
137
|
self._operation_traits: list[dict[str, Any]] = list()
|
138
138
|
|
@@ -152,22 +152,30 @@ class Channel(object):
|
|
152
152
|
self._operation_traits.append(trait)
|
153
153
|
return self
|
154
154
|
|
155
|
-
def get_operation(
|
155
|
+
def get_operation(
|
156
|
+
self, client_type: SpecType, use_common=False
|
157
|
+
) -> dict[str, dict[str, Any]]:
|
156
158
|
channel_item: dict[str, dict[str, Any]] = dict()
|
157
|
-
op_item: OrderedDict[str, Any] = OrderedDict(
|
158
|
-
|
159
|
-
|
160
|
-
"
|
159
|
+
op_item: OrderedDict[str, Any] = OrderedDict(
|
160
|
+
{
|
161
|
+
"operationId": self.name,
|
162
|
+
"message": {
|
163
|
+
"$ref": f"{use_common or ''}#/components/messages/{self.message_name}"
|
164
|
+
},
|
161
165
|
}
|
162
|
-
|
166
|
+
)
|
163
167
|
if use_common is not False:
|
164
168
|
op_item["traits"] = [
|
165
169
|
{"$ref": f"{use_common}#/components/operationTraits/{self.name}"}
|
166
170
|
]
|
167
171
|
elif len(self._operation_traits) > 0:
|
168
|
-
op_item.update(
|
169
|
-
|
170
|
-
|
172
|
+
op_item.update(
|
173
|
+
OrderedDict(
|
174
|
+
{
|
175
|
+
"traits": self._operation_traits,
|
176
|
+
}
|
177
|
+
)
|
178
|
+
)
|
171
179
|
if (
|
172
180
|
client_type == SpecType.SERVER
|
173
181
|
and self.direction == Direction.SERVER_PUBLISHES
|
@@ -181,11 +189,7 @@ class Channel(object):
|
|
181
189
|
if len(self.parameters) > 0:
|
182
190
|
params_obj = dict()
|
183
191
|
for param_name, param_type in self.parameters.items():
|
184
|
-
params_obj[param_name] = {
|
185
|
-
"schema": {
|
186
|
-
"type": param_type
|
187
|
-
}
|
188
|
-
}
|
192
|
+
params_obj[param_name] = {"schema": {"type": param_type}}
|
189
193
|
channel_item.update({"parameters": params_obj})
|
190
194
|
return channel_item
|
191
195
|
|
@@ -194,9 +198,9 @@ class Server(object):
|
|
194
198
|
def __init__(self, name: str):
|
195
199
|
self.name = name
|
196
200
|
self._protocol = "mqtt"
|
197
|
-
self._host: str|None = None
|
198
|
-
self._port: int|None = None
|
199
|
-
self._lwt_topic: str|None = None
|
201
|
+
self._host: str | None = None
|
202
|
+
self._port: int | None = None
|
203
|
+
self._lwt_topic: str | None = None
|
200
204
|
|
201
205
|
def set_host(self, host: str, port: int):
|
202
206
|
self._host = host
|
@@ -209,10 +213,7 @@ class Server(object):
|
|
209
213
|
|
210
214
|
@property
|
211
215
|
def url(self) -> str:
|
212
|
-
return "{}:{}".format(
|
213
|
-
self._host or "{hostname}",
|
214
|
-
self._port or "{port}"
|
215
|
-
)
|
216
|
+
return "{}:{}".format(self._host or "{hostname}", self._port or "{port}")
|
216
217
|
|
217
218
|
def get_server(self) -> dict[str, Any]:
|
218
219
|
spec: dict[str, Any] = {
|
@@ -221,28 +222,33 @@ class Server(object):
|
|
221
222
|
"url": self.url,
|
222
223
|
}
|
223
224
|
if self._lwt_topic is not None:
|
224
|
-
spec[
|
225
|
-
|
226
|
-
"
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
225
|
+
spec["bindings"] = OrderedDict(
|
226
|
+
{
|
227
|
+
"mqtt": OrderedDict(
|
228
|
+
{
|
229
|
+
"lastWill": OrderedDict(
|
230
|
+
{
|
231
|
+
"retain": False,
|
232
|
+
"message": None,
|
233
|
+
"qos": 1,
|
234
|
+
"topic": self._lwt_topic,
|
235
|
+
}
|
236
|
+
)
|
237
|
+
}
|
238
|
+
)
|
239
|
+
}
|
240
|
+
)
|
234
241
|
if self._host is None or self._port is None:
|
235
|
-
spec[
|
242
|
+
spec["variables"] = {}
|
236
243
|
if self._host is None:
|
237
|
-
spec[
|
244
|
+
spec["variables"]["hostname"] = {
|
238
245
|
"description": "The hosthame or IP address of the MQTT broker."
|
239
246
|
}
|
240
247
|
if self._port is None:
|
241
|
-
spec[
|
242
|
-
"description": "The port for the MQTT server"
|
243
|
-
}
|
248
|
+
spec["variables"]["port"] = {"description": "The port for the MQTT server"}
|
244
249
|
return spec
|
245
250
|
|
251
|
+
|
246
252
|
class AsyncApiCreator(object):
|
247
253
|
"""A class to create a AsyncAPI specification from several AsyncAPI structures.
|
248
254
|
|
@@ -251,89 +257,137 @@ class AsyncApiCreator(object):
|
|
251
257
|
|
252
258
|
def __init__(self):
|
253
259
|
self.info = dict()
|
254
|
-
self.asyncapi: OrderedDict[str, Any] = OrderedDict(
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
"
|
261
|
-
|
262
|
-
"
|
263
|
-
|
264
|
-
"
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
"
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
260
|
+
self.asyncapi: OrderedDict[str, Any] = OrderedDict(
|
261
|
+
{
|
262
|
+
"asyncapi": "2.4.0",
|
263
|
+
"id": "",
|
264
|
+
"info": OrderedDict(),
|
265
|
+
"channels": OrderedDict(),
|
266
|
+
"components": OrderedDict(
|
267
|
+
{
|
268
|
+
"operationTraits": OrderedDict(
|
269
|
+
{
|
270
|
+
"methodCall": OrderedDict(
|
271
|
+
{
|
272
|
+
"bindings": OrderedDict(
|
273
|
+
{
|
274
|
+
"mqtt": OrderedDict(
|
275
|
+
{
|
276
|
+
"bindingVersion": "0.2.0",
|
277
|
+
"qos": 2,
|
278
|
+
"retain": False,
|
279
|
+
}
|
280
|
+
),
|
281
|
+
}
|
282
|
+
),
|
283
|
+
}
|
284
|
+
),
|
285
|
+
"methodCallback": OrderedDict(
|
286
|
+
{
|
287
|
+
"bindings": OrderedDict(
|
288
|
+
{
|
289
|
+
"mqtt": OrderedDict(
|
290
|
+
{
|
291
|
+
"bindingVersion": "0.2.0",
|
292
|
+
"qos": 1,
|
293
|
+
"retain": False,
|
294
|
+
}
|
295
|
+
),
|
296
|
+
}
|
297
|
+
),
|
298
|
+
}
|
299
|
+
),
|
300
|
+
"signal": OrderedDict(
|
301
|
+
{
|
302
|
+
"bindings": OrderedDict(
|
303
|
+
{
|
304
|
+
"mqtt": OrderedDict(
|
305
|
+
{
|
306
|
+
"bindingVersion": "0.2.0",
|
307
|
+
"qos": 2,
|
308
|
+
"retain": False,
|
309
|
+
}
|
310
|
+
),
|
311
|
+
}
|
312
|
+
),
|
313
|
+
}
|
314
|
+
),
|
315
|
+
}
|
316
|
+
),
|
317
|
+
"messageTraits": OrderedDict(
|
318
|
+
{
|
319
|
+
"methodJsonArguments": OrderedDict(
|
320
|
+
{
|
321
|
+
"bindings": OrderedDict(
|
322
|
+
{
|
323
|
+
"mqtt": OrderedDict(
|
324
|
+
{
|
325
|
+
"bindingVersion": "0.2.0",
|
326
|
+
"contentType": "application/json",
|
327
|
+
"correlationData": OrderedDict(
|
328
|
+
{
|
329
|
+
"type": "string",
|
330
|
+
"format": "uuid",
|
331
|
+
}
|
332
|
+
),
|
333
|
+
}
|
334
|
+
),
|
335
|
+
}
|
336
|
+
),
|
337
|
+
}
|
338
|
+
),
|
339
|
+
"methodJsonResponse": OrderedDict(
|
340
|
+
{
|
341
|
+
"bindings": OrderedDict(
|
342
|
+
{
|
343
|
+
"mqtt": OrderedDict(
|
344
|
+
{
|
345
|
+
"bindingVersion": "0.2.0",
|
346
|
+
"contentType": "application/json",
|
347
|
+
"correlationData": OrderedDict(
|
348
|
+
{
|
349
|
+
"type": "string",
|
350
|
+
"format": "uuid",
|
351
|
+
}
|
352
|
+
),
|
353
|
+
"responseTopic": {
|
354
|
+
"type": "string",
|
355
|
+
},
|
356
|
+
}
|
357
|
+
),
|
358
|
+
}
|
359
|
+
),
|
360
|
+
}
|
361
|
+
),
|
362
|
+
"signalJson": OrderedDict(
|
363
|
+
{
|
364
|
+
"bindings": OrderedDict(
|
365
|
+
{
|
366
|
+
"mqtt": OrderedDict(
|
367
|
+
{
|
368
|
+
"bindingVersion": "0.2.0",
|
369
|
+
"contentType": "application/json",
|
370
|
+
}
|
371
|
+
),
|
372
|
+
}
|
373
|
+
),
|
374
|
+
}
|
375
|
+
),
|
376
|
+
}
|
377
|
+
),
|
378
|
+
"messages": OrderedDict(),
|
379
|
+
"schemas": OrderedDict(),
|
380
|
+
}
|
381
|
+
),
|
382
|
+
}
|
383
|
+
)
|
330
384
|
self.channels = []
|
331
385
|
self.messages = []
|
332
386
|
self.servers = []
|
333
387
|
self.name = "interface"
|
334
388
|
|
335
389
|
def add_schema(self, schema_name: str, schema_spec: dict[str, Any]):
|
336
|
-
schema_dict: dict[str, Any] = self.asyncapi[
|
390
|
+
schema_dict: dict[str, Any] = self.asyncapi["components"]["schemas"]
|
337
391
|
schema_dict[schema_name] = schema_spec
|
338
392
|
|
339
393
|
def add_channel(self, channel: Channel):
|
@@ -355,9 +409,9 @@ class AsyncApiCreator(object):
|
|
355
409
|
def get_asyncapi(self, client_type: SpecType, use_common=None):
|
356
410
|
spec = self.asyncapi.copy()
|
357
411
|
if len(self.servers) > 0:
|
358
|
-
spec[
|
412
|
+
spec["servers"] = {}
|
359
413
|
for svr in self.servers:
|
360
|
-
spec[
|
414
|
+
spec["servers"][svr.name] = svr.get_server()
|
361
415
|
for ch in self.channels:
|
362
416
|
spec["channels"][ch.topic] = ch.get_operation(
|
363
417
|
client_type, use_common or False
|
@@ -392,23 +446,23 @@ class StingerToAsyncApi:
|
|
392
446
|
description.append(f"{i} - {enum_value}")
|
393
447
|
accepted_values.append(i)
|
394
448
|
json_schema = {
|
395
|
-
"type":
|
449
|
+
"type": "integer",
|
396
450
|
"description": "\n ".join(description),
|
397
|
-
"enum": accepted_values
|
451
|
+
"enum": accepted_values,
|
398
452
|
}
|
399
453
|
self._asyncapi.add_schema(schema_name, json_schema)
|
400
454
|
return self
|
401
455
|
|
402
456
|
def _add_interface_info(self):
|
403
457
|
topic, info = self._stinger.interface_info
|
404
|
-
self._asyncapi.add_to_info("version", info[
|
405
|
-
self._asyncapi.add_to_info("title", info[
|
458
|
+
self._asyncapi.add_to_info("version", info["version"])
|
459
|
+
self._asyncapi.add_to_info("title", info["title"])
|
406
460
|
ch = Channel(topic, "interfaceInfo", Direction.SERVER_PUBLISHES)
|
407
461
|
ch.set_mqtt(qos=1, retain=True)
|
408
462
|
self._asyncapi.add_channel(ch)
|
409
463
|
msg = Message("interfaceInfo")
|
410
464
|
schema = ObjectSchema()
|
411
|
-
for k,v in info.items():
|
465
|
+
for k, v in info.items():
|
412
466
|
schema.add_const_value_property(k, ArgPrimitiveType.STRING, v)
|
413
467
|
msg.set_schema(schema.to_schema())
|
414
468
|
self._asyncapi.add_message(msg)
|
@@ -425,17 +479,15 @@ class StingerToAsyncApi:
|
|
425
479
|
def _add_enums(self):
|
426
480
|
for enum_name, enum_spec in self._stinger.enums.items():
|
427
481
|
schema_name = f"enum_{enum_name}"
|
428
|
-
description = [
|
429
|
-
f"The {enum_name} enum has the following values:"
|
430
|
-
]
|
482
|
+
description = [f"The {enum_name} enum has the following values:"]
|
431
483
|
accepted_values = []
|
432
484
|
for i, enum_value in enumerate(enum_spec.values):
|
433
485
|
description.append(f"{i} - {enum_value}")
|
434
486
|
accepted_values.append(i)
|
435
487
|
json_schema = {
|
436
|
-
"type":
|
488
|
+
"type": "integer",
|
437
489
|
"description": "\n ".join(description),
|
438
|
-
"enum": accepted_values
|
490
|
+
"enum": accepted_values,
|
439
491
|
}
|
440
492
|
self._asyncapi.add_schema(schema_name, json_schema)
|
441
493
|
|
@@ -451,40 +503,68 @@ class StingerToAsyncApi:
|
|
451
503
|
if isinstance(arg_spec, ArgPrimitive):
|
452
504
|
schema.add_value_property(arg_spec.name, arg_spec.type)
|
453
505
|
elif isinstance(arg_spec, ArgEnum):
|
454
|
-
schema.add_reference_property(
|
506
|
+
schema.add_reference_property(
|
507
|
+
arg_spec.name, f"#/components/schemas/enum_{arg_spec.enum.name}"
|
508
|
+
)
|
455
509
|
msg.set_schema(schema.to_schema())
|
456
510
|
self._asyncapi.add_message(msg)
|
457
511
|
|
458
512
|
def _add_methods(self):
|
459
513
|
for method_name, method_spec in self._stinger.methods.items():
|
460
|
-
call_ch = Channel(
|
461
|
-
|
514
|
+
call_ch = Channel(
|
515
|
+
method_spec.topic, method_name, Direction.SERVER_SUBSCRIBES
|
516
|
+
)
|
517
|
+
call_ch.add_operation_trait(
|
518
|
+
{"$ref": "#/components/operationTraits/methodCall"}
|
519
|
+
)
|
462
520
|
self._asyncapi.add_channel(call_ch)
|
463
521
|
call_msg = Message(method_name)
|
464
|
-
call_msg.add_trait(
|
522
|
+
call_msg.add_trait(
|
523
|
+
{"$ref": "#/components/messageTraits/methodJsonArguments"}
|
524
|
+
)
|
465
525
|
call_msg_schema = ObjectSchema()
|
466
526
|
for arg_spec in method_spec.arg_list:
|
467
527
|
if isinstance(arg_spec, ArgPrimitive):
|
468
528
|
call_msg_schema.add_value_property(arg_spec.name, arg_spec.type)
|
469
529
|
elif isinstance(arg_spec, ArgEnum):
|
470
|
-
call_msg_schema.add_reference_property(
|
530
|
+
call_msg_schema.add_reference_property(
|
531
|
+
arg_spec.name, f"#/components/schemas/enum_{arg_spec.name}"
|
532
|
+
)
|
471
533
|
call_msg.set_schema(call_msg_schema.to_schema())
|
472
534
|
self._asyncapi.add_message(call_msg)
|
473
535
|
|
474
|
-
resp_ch = Channel(
|
475
|
-
|
536
|
+
resp_ch = Channel(
|
537
|
+
method_spec.response_topic("{client_id}"),
|
538
|
+
f"{method_name}Response",
|
539
|
+
Direction.SERVER_PUBLISHES,
|
540
|
+
)
|
541
|
+
resp_ch.add_operation_trait(
|
542
|
+
{"$ref": "#/components/operationTraits/methodCall"}
|
543
|
+
)
|
476
544
|
self._asyncapi.add_channel(resp_ch)
|
477
545
|
resp_msg = Message(f"{method_name}Response")
|
478
|
-
resp_msg.add_trait(
|
479
|
-
|
546
|
+
resp_msg.add_trait(
|
547
|
+
{"$ref": "#/components/messageTraits/methodJsonArguments"}
|
548
|
+
)
|
549
|
+
resp_msg.add_header(
|
550
|
+
"result",
|
551
|
+
{"$ref": "#/components/schemas/stinger_method_return_codes"},
|
552
|
+
required=True,
|
553
|
+
)
|
480
554
|
resp_msg.add_header("debug", {"type": "string"}, required=False)
|
481
555
|
resp_msg_schema = ObjectSchema()
|
482
556
|
|
483
557
|
def add_arg(arg: Arg):
|
484
558
|
if isinstance(arg, ArgPrimitive):
|
485
|
-
resp_msg_schema.add_value_property(
|
559
|
+
resp_msg_schema.add_value_property(
|
560
|
+
arg.name, arg.type, required=True
|
561
|
+
)
|
486
562
|
elif isinstance(arg, ArgEnum):
|
487
|
-
resp_msg_schema.add_reference_property(
|
563
|
+
resp_msg_schema.add_reference_property(
|
564
|
+
arg.name,
|
565
|
+
f"#/components/schemas/enum_{arg.enum.name}",
|
566
|
+
required=True,
|
567
|
+
)
|
488
568
|
|
489
569
|
if isinstance(method_spec.return_value, ArgStruct):
|
490
570
|
for arg_spec in method_spec.return_value.members:
|
@@ -492,10 +572,12 @@ class StingerToAsyncApi:
|
|
492
572
|
resp_msg_schema.add_value_dependency(arg_spec.name, "result", 0)
|
493
573
|
elif method_spec.return_value is not None:
|
494
574
|
add_arg(method_spec.return_value)
|
495
|
-
resp_msg_schema.add_value_dependency(
|
575
|
+
resp_msg_schema.add_value_dependency(
|
576
|
+
method_spec.return_value_name, "result", 0
|
577
|
+
)
|
496
578
|
|
497
579
|
resp_msg.set_schema(resp_msg_schema.to_schema())
|
498
580
|
self._asyncapi.add_message(resp_msg)
|
499
581
|
|
500
582
|
def get_asyncapi(self):
|
501
|
-
return self._asyncapi.get_asyncapi(SpecType.CLIENT)
|
583
|
+
return self._asyncapi.get_asyncapi(SpecType.CLIENT)
|