Skip to content

Commit a5938ee

Browse files
committed
Fix feature for parametric structs that have type UnionAll and added tests
1 parent 35b9888 commit a5938ee

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/utils_gen/utils.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ get_types(["Base", "Core"]) # returns type names from both packages
309309
function get_types(modul::Module)
310310
types = []
311311
for name names(modul)
312-
if isdefined(modul, name) && getfield(modul, name) isa DataType
312+
if isdefined(modul, name) && (
313+
getfield(modul, name) isa DataType || (
314+
getfield(modul, name) isa UnionAll &&
315+
Base.unwrap_unionall(getfield(modul, name)) isa DataType
316+
)
317+
)
313318
push!(types, name)
314319
end
315320
end
@@ -368,7 +373,12 @@ This set of functions helps extract the inheritance hierarchy of types defined i
368373
function get_supertypes(modul::Module)
369374
types=Dict()
370375
for name names(modul)
371-
if isdefined(modul, name) && getfield(modul, name) isa DataType
376+
if isdefined(modul, name) && (
377+
getfield(modul, name) isa DataType || (
378+
getfield(modul, name) isa UnionAll &&
379+
Base.unwrap_unionall(getfield(modul, name)) isa DataType
380+
)
381+
)
372382
types[name] = supertypes(getfield(modul, name))
373383
end
374384
end
@@ -410,7 +420,6 @@ Checks whether a given type is a concrete struct with at least one field.
410420
411421
# Description
412422
This function combines three checks:
413-
- `isconcretetype(type)`: Ensures the type is concrete (i.e., can be instantiated).
414423
- `isstructtype(type)`: Ensures the type is a struct.
415424
- `nfields(type) > 0`: Ensures the struct has at least one field.
416425
@@ -427,7 +436,8 @@ has_fields(AbstractType) # returns false
427436
```
428437
"""
429438
function has_fields(type)
430-
return (isconcretetype(type) && isstructtype(type) && nfields(type) > 0)
439+
t = type isa UnionAll ? Base.unwrap_unionall(type) : type
440+
return (t isa DataType && isstructtype(t) && nfields(t) > 0)
431441
end
432442

433443
"""

test/test_interactivity.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,26 @@ pin_plot_button = get_button(gui, :pin_plot)
9898
descriptive_names_raw =
9999
YAML.load_file(path_to_descriptive_names; dicttype = Dict{Symbol,Any})
100100
str1 = "Relative fixed operating expense per installed capacity"
101-
gui2 = GUI(
101+
str2 = "Initial stored energy in the dam"
102+
gui3 = GUI(
102103
case;
103104
path_to_descriptive_names = path_to_descriptive_names,
104105
)
105106
@test descriptive_names_raw[:structures][:Node][:opex_fixed] == str1
106107
@test :StorCapOpexFixed keys(descriptive_names_raw[:structures])
107108
@test :RefNetworkNode keys(descriptive_names_raw[:structures])
108109

109-
descriptive_names = EMGUI.get_var(gui2, :descriptive_names)
110+
@test descriptive_names_raw[:structures][:HydroStorage][:level_init] == str2
111+
@test :HydroStor keys(descriptive_names_raw[:structures])
112+
@test :PumpedHydroStor keys(descriptive_names_raw[:structures])
113+
114+
descriptive_names = EMGUI.get_var(gui3, :descriptive_names)
110115
@test descriptive_names[:structures][:StorCapOpexFixed][:opex_fixed] == str1
111116
@test descriptive_names[:structures][:RefNetworkNode][:opex_fixed] == str1
112-
EMGUI.close(gui2)
117+
118+
@test descriptive_names[:structures][:HydroStor][:level_init] == str2
119+
@test descriptive_names[:structures][:PumpedHydroStor][:level_init] == str2
120+
EMGUI.close(gui3)
113121
end
114122
end
115123

0 commit comments

Comments
 (0)