Skip to content

Commit 9daab47

Browse files
authored
Tweaks to make the package compatible with JSON3 struct mapping (#24)
1 parent 9ff21f7 commit 9daab47

3 files changed

Lines changed: 428 additions & 424 deletions

File tree

src/Generate/generate-types.jl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Generate the code for OMOPCommonDataModel from the DDL.
77
output_file = "autogenerated.jl",
88
string_casing::F = pascalcase,
99
export_structs::Bool = true,
10-
make_all_fields_optional::Bool = false) where F
10+
make_all_fields_optional::Bool = false,
11+
struct_keyword = "struct") where F
1112
input_file_abspath = abspath(input_file)
1213
output_file_abspath = abspath(output_file)
1314
@info("Reading from input file \"$(input_file_abspath)\"")
@@ -16,7 +17,8 @@ Generate the code for OMOPCommonDataModel from the DDL.
1617
contents = _generate(input_file_contents;
1718
export_structs = export_structs,
1819
make_all_fields_optional = make_all_fields_optional,
19-
string_casing = string_casing)
20+
string_casing = string_casing,
21+
struct_keyword = struct_keyword)
2022
mkpath(dirname(output_file_abspath))
2123
rm(output_file_abspath; force = true, recursive = true)
2224
open(output_file_abspath, "w") do io
@@ -30,7 +32,8 @@ end
3032
@inline function _generate(input_file_contents::AbstractString;
3133
string_casing::F,
3234
export_structs::Bool,
33-
make_all_fields_optional)::String where F
35+
make_all_fields_optional,
36+
struct_keyword)::String where F
3437
pattern = r"CREATE TABLE ([\w_]*?)[ ]?[\n]?\(([\s\S]*?\n)\s?\s?\s?\s?\s?\s?\)\n;"
3538
all_match_positions = findall(pattern, input_file_contents)
3639
all_match_strings = String[input_file_contents[position] for position in all_match_positions]
@@ -39,7 +42,8 @@ end
3942
str;
4043
export_structs = export_structs,
4144
make_all_fields_optional = make_all_fields_optional,
42-
string_casing = string_casing) for str in all_match_strings
45+
string_casing = string_casing,
46+
struct_keyword = struct_keyword) for str in all_match_strings
4347
]
4448
num_generated_types = length(all_type_lines)
4549
output_lines = vcat(
@@ -65,7 +69,8 @@ end
6569
str;
6670
string_casing::F,
6771
export_structs::Bool,
68-
make_all_fields_optional::Bool)::String where F
72+
make_all_fields_optional::Bool,
73+
struct_keyword)::String where F
6974
m = match(pattern, str)
7075
cdmname = m[1]
7176
_cdmname = strip(cdmname)
@@ -97,7 +102,7 @@ end
97102
String["\$(DocStringExtensions.TYPEDEF)"],
98103
String["\$(DocStringExtensions.TYPEDFIELDS)"],
99104
String["\"\"\""],
100-
String["Base.@kwdef struct $(structname) <: CDMType"],
105+
String["Base.@kwdef $(struct_keyword) $(structname) <: CDMType"],
101106
fields,
102107
String["end"],
103108
)
@@ -146,15 +151,14 @@ end
146151
null_or_notnull;
147152
make_all_fields_optional::Bool)::String
148153
_null_or_notnull = strip(null_or_notnull)
149-
required_case = "$(partial_fieldtype)"
150-
optional_case = "Union{$(partial_fieldtype), Missing} = missing"
151-
if make_all_fields_optional
152-
return optional_case
153-
end
154154
if _null_or_notnull == "NULL"
155-
return optional_case
155+
return "Union{$(partial_fieldtype), Missing} = missing # optional"
156156
elseif _null_or_notnull == "NOT NULL"
157-
return required_case
157+
if make_all_fields_optional
158+
return "Union{$(partial_fieldtype), Missing} = missing # required"
159+
else
160+
return "$(partial_fieldtype) # required"
161+
end
158162
end
159163
throw(ArgumentError("Invalid value for `null_or_notnull`: $(null_or_notnull)"))
160164
end

0 commit comments

Comments
 (0)