Skip to content

Commit bdcf90c

Browse files
authored
Merge pull request #4 from OpenSesame/fix/macro_structs_generator
Errors: implemented macro for generating structures
2 parents c96efd5 + a00bed3 commit bdcf90c

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
name = "Elasticsearch"
22
uuid = "e586a49d-aa29-4ce5-8f91-fa4f824367bd"
33
authors = ["Egor Shmorgun <egor.shmorgun@opensesame.com>"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[deps]
77
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
88
Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
99
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1010
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
11-
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1211
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1312
Mocking = "78c3b35d-d492-501b-9361-3d52fe80e533"
1413
Retry = "20febd7b-183b-5ae2-ac4a-720e7ce64774"

src/elastic_transport/transport/errors.jl

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,26 @@ const HTTP_STATUSES = Dict(
5252
510 => :NotExtended
5353
)
5454

55-
foreach(values(HTTP_STATUSES)) do exception_name
56-
struct_expr = Meta.parse(
57-
"""
58-
struct $exception_name <: ServerException
59-
status
60-
message::String
55+
macro generate_exception_structures(http_statuses_dict)
56+
dict = eval(http_statuses_dict)
57+
# dict = HTTP_STATUSES
58+
return Expr(:block, map(values(dict)) do exception_name
59+
quote
60+
struct $exception_name <: ServerException
61+
status
62+
message::String
63+
end
6164
end
62-
"""
63-
)
64-
65-
eval(struct_expr)
65+
end...)
6666
end
6767

68-
using InteractiveUtils: subtypes
68+
@generate_exception_structures(HTTP_STATUSES)
6969

70-
const CODE_TO_EXCEPTION = Dict(
71-
Symbol(Base.typename(t).name) => t
72-
for t in subtypes(ServerException)
73-
) |> t_codes -> Dict(
74-
k => t_codes[v]
75-
for (k, v) in HTTP_STATUSES
76-
)
70+
macro transform_exceptions_to_types(http_statuses_dict)
71+
dict = eval(http_statuses_dict)
72+
Dict(k => eval(v) for (k, v) in dict)
73+
end
74+
75+
const CODE_TO_EXCEPTION = @transform_exceptions_to_types(HTTP_STATUSES)
7776

7877
const HOST_UNREACHABLE_EXCEPTIONS = [HTTP.TimeoutError, HTTP.ConnectError]

0 commit comments

Comments
 (0)