Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict_list changes Dict type #327

Open
msmerlak opened this issue Jan 21, 2022 · 2 comments · May be fixed by #342
Open

dict_list changes Dict type #327

msmerlak opened this issue Jan 21, 2022 · 2 comments · May be fixed by #342
Labels
bug Something isn't working running-listing Functionality for running and listing simulation runs

Comments

@msmerlak
Copy link

A dict of type, say, {Symbol, Any} such as Dict{Symbol, Any}(:a => collect(1:10), :b => true) will be turned into a Vector{Dict{Symbol, Integer}}. This can create unwanted behavior.

I propose the following modification to _dict_list:

function _dict_list(c::AbstractDict)

    dict = typeof(c)

    iterable_fields = filter(k -> typeof(c[k]) <: Vector, keys(c))
    non_iterables = setdiff(keys(c), iterable_fields)

    iterable_dict = dict(iterable_fields .=> getindex.(Ref(c), iterable_fields))
    non_iterable_dict = dict(non_iterables .=> getindex.(Ref(c), non_iterables))

    vec(
        map(Iterators.product(values(iterable_dict)...)) do vals
            dd = [k=>convert(eltype(c[k]),v) for (k,v) in zip(keys(iterable_dict),vals)]
            if isempty(non_iterable_dict)
                Dict(dd)
            elseif isempty(iterable_dict)
                non_iterable_dict
            else
                # We can't use merge here because it promotes types.
                # The uniqueness of the dictionary keys is guaranteed.
                dict(dd..., collect(non_iterable_dict)...)
            end
        end
    )
end
@sebastianpech
Copy link
Contributor

Yes, sounds reasonable to keep the original type. Could you open a PR?

@Datseris Datseris added bug Something isn't working running-listing Functionality for running and listing simulation runs labels Jan 21, 2022
@Datseris
Copy link
Member

yEAH I agree this is a bug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working running-listing Functionality for running and listing simulation runs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants