-
Notifications
You must be signed in to change notification settings - Fork 0
/
codema.star
285 lines (259 loc) · 8.78 KB
/
codema.star
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
# Utility functions
def create_field(name, type, description, optional=False, directives=None):
return {
"name": name,
"type": type,
"description": description,
"optional": optional,
"directives": directives if directives else {}
}
def create_model(name, fields, description=""):
return {
"name": name,
"fields": fields,
"description": description
}
def create_function(name, parameters, description=""):
return {
"name": name,
"parameters": parameters,
"description": description
}
def create_function_implementation(function, target_snippets):
return {
"function": function,
"target_snippets": target_snippets
}
def create_microservice(label, models, function_implementations):
return {
"label": label,
"models": models,
"function_implementations": function_implementations,
}
def create_api(package, label, microservices):
return {
"package": package,
"label": label,
"microservices": microservices
}
# Define reusable directives
directives = {
"updatable": {"updatable": True},
"deprecated": {"deprecated": True},
}
# Define models
booking_model = create_model(
"Booking",
[
create_field("id", "String", "The unique identifier for a booking.", optional=False),
create_field("customer_id", "String", "Identifier of the customer who made the booking.", optional=False),
create_field("start_time", "DateTime", "Start time of the booking.", optional=False),
create_field("end_time", "DateTime", "End time of the booking.", optional=True),
create_field("status", "BookingStatus", "Current status of the booking.", optional=False, directives=directives["deprecated"]),
create_field("state", "BookingState", "The state of the booking.", optional=False, directives=directives["updatable"])
],
"Represents a booking entity."
)
# Define functions
create_booking_function = create_function(
"CreateBooking",
["customer_id", "start_time", "end_time"],
"Creates a new booking."
)
update_booking_function = create_function(
"UpdateBooking",
["booking_id", "start_time", "end_time", "state"],
"Updates an existing booking."
)
# Define function implementations
create_booking_implementation = create_function_implementation(
create_booking_function,
{
"repo": "/snippets/create_booking_repo.template",
"grpc": "/snippets/create_booking_grpc.template"
}
)
update_booking_implementation = create_function_implementation(
update_booking_function,
{
"repo": "/snippets/update_booking_repo.template",
"grpc": "/snippets/update_booking_grpc.template"
}
)
# Define microservices
booking_microservice = create_microservice(
"booking",
[booking_model],
[create_booking_implementation, update_booking_implementation],
)
booking_oracle_microservice = create_microservice(
"bookingOracle",
[],
[create_booking_implementation],
)
# Define APIs
booking_api = create_api(
"booking",
"booking",
[booking_microservice, booking_oracle_microservice]
)
service_provider_liability_model = create_model(
"ServiceProviderLiability",
[
create_field("id", "String", "The unique identifier for a liability.", optional=False),
create_field("service_provider_id", "String", "Identifier of the service provider.", optional=False),
create_field("accounting_record_id", "String", "Identifier of the associated accounting record.", optional=False),
create_field("amount", "Int64", "The liability amount.", optional=False),
create_field("currency", "String", "The currency of the liability.", optional=False),
create_field("created_at", "Timestamp", "Creation timestamp.", optional=False),
create_field("updated_at", "Timestamp", "Last update timestamp.", optional=False),
create_field("is_settled", "Bool", "Whether the liability is settled.", optional=False),
],
"Represents a service provider liability."
)
# Define functions
svc_create_function = create_function(
"Create",
["subjectID", "liability"],
"Creates a new service provider liability."
)
update_function = create_function(
"Update",
["subjectID", "id", "liability"],
"Updates an existing service provider liability."
)
delete_function = create_function(
"Delete",
["subjectID", "id"],
"Deletes a service provider liability."
)
query_function = create_function(
"Query",
["serviceProviderIDs", "accountingRecordIDs", "isSettled"],
"Queries service provider liabilities based on given criteria."
)
# Define function implementations
create_implementation = create_function_implementation(
svc_create_function,
{
"handler": "/snippets/handler/create.template",
"logic": "/snippets/logic/create.template",
"repo": "/snippets/repo/create.template",
"relay": "/snippets/relay/create.template",
}
)
update_implementation = create_function_implementation(
update_function,
{
"handler": "/snippets/handler/update.template",
"logic": "/snippets/logic/update.template",
"repo": "/snippets/repo/update.template",
"relay": "/snippets/relay/update.template",
}
)
delete_implementation = create_function_implementation(
delete_function,
{
"handler": "/snippets/handler/delete.template",
"logic": "/snippets/logic/delete.template",
"repo": "/snippets/repo/delete.template",
"relay": "/snippets/relay/delete.template",
}
)
query_implementation = create_function_implementation(
query_function,
{
"handler": "/snippets/handler/query.template",
"logic": "/snippets/logic/query.template",
"repo": "/snippets/repo/query.template",
"relay": "/snippets/relay/query.template",
}
)
service_provider_liability_microservice = create_microservice(
"serviceProviderLiability",
[service_provider_liability_model],
[
create_implementation,
update_implementation,
delete_implementation,
query_implementation,
],
)
# Define APIs
service_provider_liability_api = create_api(
"service-provider",
"serviceProviderLiability",
[service_provider_liability_microservice]
)
# Configuration
config = {
"moduleDir": "./out",
"templateDir": "./codema-templates",
"apis": [booking_api, service_provider_liability_api],
"targets": [
{
"label": "relay-fn",
"templateDir": "/relay-fn",
"each": True,
"defaultVersion": "v0.1.0",
"apis": [
{"label": "booking", "outPath": "/{{.Label}}/{{.Microservice.Label}}_relay_fn_generated.go"}
]
},
{
"label": "grpc",
"templatePath": "/grpc.template",
"apis": [
{"label": "booking", "outPath": "/{{.Label}}/grpc_generated.go"}
]
},
{
"label": "repo",
"templatePath": "/repo.template",
"each": True,
"apis": [
{
"label": "booking",
"outPath": "/{{.Api.LabelKebab}}/{{.Microservice.Label}}_repo.codema.go",
"skipLabels": ["booking", "bookingOracle", "eagerBooking"]
}
]
},
{
"label": "handler",
"templateDir": "/service/internal/rpc/handler",
"each": True,
"defaultVersion": "v0.1.0",
"apis": [
{"label": "serviceProviderLiability", "outPath": "/{{.Api.LabelKebab}}/{{.Microservice.LabelKebab}}/internal/rpc/handler_generated.go"}
]
},
{
"label": "logic",
"templateDir": "/service/internal/logic/logic",
"each": True,
"defaultVersion": "v0.1.0",
"apis": [
{"label": "serviceProviderLiability", "outPath": "/{{.Api.LabelKebab}}/{{.Microservice.LabelKebab}}/internal/logic/logic_generated.go"}
]
},
{
"label": "repo",
"templateDir": "/service/internal/repo/repo",
"each": True,
"defaultVersion": "v0.1.0",
"apis": [
{"label": "serviceProviderLiability", "outPath": "/{{.Api.LabelKebab}}/{{.Microservice.LabelKebab}}/internal/repo/repo_generated.go"}
]
},
{
"label": "relay",
"templateDir": "/service/external/relay/relay",
"each": True,
"defaultVersion": "v0.1.0",
"apis": [
{"label": "serviceProviderLiability", "outPath": "/{{.Api.LabelKebab}}/{{.Microservice.LabelKebab}}/internal/relay/relay_generated.go"}
]
}
]
}