Skip to content

Commit

Permalink
Support multiple configs as input in configure() and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyun committed Jul 3, 2020
1 parent 2bcbe96 commit af34133
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const Config = OrderedDict{Symbol,Any}
configure(c::AbstractDict) = configure(c...)
configure(c::Pair) = _configure(c.first, c.second)
configure(c::Tuple) = configure(c...)
configure(c::Vector) = error("single configuration expected: $c")
#HACK: allow single patch (i.e. `0 => :a => 1` instead of `1:2`) in configexpand
configure(c::Array{T,0}) where {T} = configure(c...)
configure(c::Vector) = configure.(c)
configure(c...) = merge(merge, configure.(c)...)
configure(::Nothing) = configure()
configure(c) = error("unrecognized configuration: $c")
Expand Down Expand Up @@ -103,15 +101,18 @@ configexpand(patch; base=()) = begin
else
s, C = only(P)
k, V = only(C)
[s => k => v for v in V]
#HACK: allow single patch (i.e. `0 => :a => 1` instead of `1:2`)
reshape([s => k => v for v in V], :)
end
configrebase(configs; base=base)
configexpand(configs; base=base)
end
configexpand(configs::Vector; base=()) = configrebase(configs; base=base)
configrebase(configs::Vector; base=()) = isempty(configs) ? [configure(base)] : [configure((base, c)) for c in configs]
configrebase(config; base=()) = configrebase([config]; base=base)

configreduce(a::Vector, b) = configrebase(b; base=a)
configreduce(a, b::Vector) = configrebase(b; base=a)
configreduce(a::Vector, b::Vector) = configreduce.(a, b)
configreduce(a, b) = configure(a, b)
configreduce(a) = configure(a)
configreduce(a::Vector) = configure.(a)
Expand Down
6 changes: 4 additions & 2 deletions test/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import DataStructures: OrderedDict
@test_throws ErrorException Cropbox.configure(0)
@test_throws ErrorException Cropbox.configure(:a)
@test_throws ErrorException Cropbox.configure("a")
@test_throws ErrorException Cropbox.configure([])
end
end

Expand All @@ -34,7 +33,10 @@ import DataStructures: OrderedDict

@testset "vector" begin
c = [:S1 => :a => 1, :S2 => :a => 2]
@test_throws ErrorException Cropbox.configure(c)
C = Cropbox.configure(c)
C1 = Cropbox.configure(c[1])
C2 = Cropbox.configure(c[2])
@test C == [C1, C2]
end

@testset "type" begin
Expand Down

0 comments on commit af34133

Please sign in to comment.