Skip to content

Commit 59db575

Browse files
committed
Support for conversion of vector of integers to a vector of Enums
1 parent 9380dfd commit 59db575

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PostgresORM"
22
uuid = "748b5efa-ed57-4836-b183-a38105a77fdd"
33
authors = ["Vincent Laugier <vincent.laugier@gmail.com>"]
4-
version = "0.2.0"
4+
version = "0.3.0"
55

66
[deps]
77
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

src/Controller/coreORM.utils.part1.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ function util_dict2entity(props_dict::Dict{Symbol,T},
368368
# If attribute is already of the right type do nothing
369369
if isa(props_dict[fsymbol],ftype)
370370
continue
371+
371372
# Override some properties
372373
elseif ftype <: UUID
373374
if ismissing(props_dict[fsymbol])
@@ -407,6 +408,7 @@ function util_dict2entity(props_dict::Dict{Symbol,T},
407408
match(r"^([0-9]{4}-[0-9]{2}-[0-9]{2})",
408409
props_dict[fsymbol])
409410
props_dict[fsymbol] = Date(date_match.match)
411+
410412
elseif ftype <: Time
411413
if (ismissing(props_dict[fsymbol]))
412414
continue
@@ -416,15 +418,23 @@ function util_dict2entity(props_dict::Dict{Symbol,T},
416418
match(r"([0-9]{2}:[0-9]{2}:[0-9]{2})",
417419
props_dict[fsymbol])
418420
props_dict[fsymbol] = Time(time_match.match)
421+
419422
elseif ftype <: Vector{String}
420423
if (ismissing(props_dict[fsymbol]))
421424
continue
422425
end
423426
# @info props_dict[fsymbol]
424427
props_dict[fsymbol] =
425428
PostgresORMUtil.postgresql_string_array_2_string_vector(props_dict[fsymbol])
429+
426430
elseif ftype <: Vector{T} where T <: Base.Enums.Enum
427-
props_dict[fsymbol] = string2vector_of_enums(ftype,props_dict[fsymbol])
431+
432+
if isa(props_dict[fsymbol],Union{Vector{T},Vector{Union{T,Missing}}} where T <: Integer)
433+
props_dict[fsymbol] = vector_of_integers2vector_of_enums(ftype,props_dict[fsymbol])
434+
else
435+
props_dict[fsymbol] = string2vector_of_enums(ftype,props_dict[fsymbol])
436+
end
437+
428438
elseif ftype <: Enum
429439

430440
if ismissing(props_dict[fsymbol])

src/PostgresORM.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module PostgresORM
4949
get_nonmissing_typeof_uniontype, dataframe2vector_of_namedtuples,
5050
dataframerow2namedtuple, getdictvalues, getpropertiesvalues,
5151
setpropertiesvalues!, remove_spaces_and_split, diff_dict,
52-
string2enum, string2vector_of_enums,
52+
string2enum, string2vector_of_enums, vector_of_integers2vector_of_enums,
5353
int2enum, enum2int, dictstringkeys2symbol, dictnothingvalues2missing,
5454
getproperties_asdict, string2zoneddatetime
5555

src/PostgresORMUtil/utils.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ function string2vector_of_enums(vectorOfEnumsTypes::Type{Vector{T}},
158158

159159
end
160160

161+
function vector_of_integers2vector_of_enums(
162+
vectorOfEnumsTypes::Type{Vector{T}} where T <: Base.Enums.Enum,
163+
vectorOfInts::Union{Vector{W},Vector{Union{W,Missing}}} where W <: Integer)
164+
165+
enumType = eltype(vectorOfEnumsTypes)
166+
167+
return int2enum.(enumType,vectorOfInts)
168+
169+
end
170+
161171
function string2zoneddatetime(str)
162172
# eg. "2019-09-03T11:00:00.000Z"
163173
date_match_GMT =

0 commit comments

Comments
 (0)