Configurations.jl icon indicating copy to clipboard operation
Configurations.jl copied to clipboard

ArgumentError in from_dict for Vectors and Strings

Open xlxs4 opened this issue 1 year ago • 1 comments

When trying to parse a string in the TOML and convert it to a vector, it yields:

ERROR: ArgumentError: map(f, s::AbstractString) requires f to return AbstractChar; try map(f, collect(s)) or a comprehension instead

Hitting https://github.com/Roger-luo/Configurations.jl/blob/ac5e96c88492ac8e73e9fe3ecd433320212a526e/src/from_dict.jl#L137

when trying to extend from_dict to work with a T<:AbstractVector and an S<:AbstractString.

MWE:

function axis2vec(s)
    axis2vec = Dict(
        "+X" => Vector(1.0, 0, 0),
        "-X" => Vector(-1.0, 0, 0),
        "+Y" => Vector(0, 1.0, 0),
        "-Y" => Vector(0, -1.0, 0),
        "+Z" => Vector(0, 0, 1.0),
        "-Z" => Vector(0, 0, -1.0),
    )
    return axis2vec[axis]
end

    
@option struct Example
    xs::Vector{Float64}
end

Configurations.from_dict(::Type{Example}, ::Type{Vector{Float64}}, x::AbstractString) = axis2vec(x)

xlxs4 avatar Dec 25 '23 22:12 xlxs4

For anyone interested, I'm currently circumventing this by doing something similar to:

struct Example
    xs::Vector{Float64}
end

@option struct _Example
    xs::String
end

_from_dict(t, d) = from_dict(t, d)
function _from_dict(t::Type{_Example}, d)
    _ex = from_dict(t, d)
    return Example(axis2vec(_ex.xs)
end

xlxs4 avatar Feb 12 '24 17:02 xlxs4