Skip to content

Commit c0dd257

Browse files
authored
Merge pull request #34 from Open-EO/fix_optional_process_params
Fix optional and required args
2 parents 2369525 + a2a0f7e commit c0dd257

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
matrix:
3232
version:
3333
- "1.9"
34-
- "nightly"
34+
- "1.10"
3535
os:
3636
- ubuntu-latest
3737
arch:

docs/src/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ password = ENV["OPENEO_PASSWORD"]
2323
c = connect("earthengine.openeo.org", "v1.0", username, password)
2424
step1 = c.load_collection(
2525
"COPERNICUS/S2", BoundingBox(west=16.06, south=48.06, east=16.65, north=48.35),
26-
["2020-01-01", "2020-01-31"], ["B10"]
26+
["2020-01-01", "2020-01-31"]; bands = ["B10"]
2727
)
28-
step2 = c.reduce_dimension(step1, ProcessGraph("median"), "t", nothing)
29-
step3 = c.save_result(step2, "GTIFF-ZIP", Dict())
28+
step2 = c.reduce_dimension(step1, ProcessGraph("median"), "t")
29+
step3 = c.save_result(step2, "GTIFF")
3030
path = c.compute_result(step3)
3131
```
3232

src/Processes.jl

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,35 @@ struct Process
2727
experimental::Union{Nothing,Bool}
2828
end
2929
StructTypes.StructType(::Type{Process}) = StructTypes.Struct()
30-
function (process::Process)(args...)
31-
params = get_parameters(process.parameters)
32-
length(args) == length(params) || throw(ArgumentError("Number of arguments does not match for process $(process.id)"))
33-
argument_dict = Dict{Symbol,Any}()
34-
for i in 1:length(args)
35-
argname, argtype = params[i]
36-
args[i] isa argtype || throw(ArgumentError("Type of argument number $i does not match, expected $argtype but got $(typeof(args[i]))"))
37-
argument_dict[argname] = args[i]
30+
31+
function (process::Process)(args...; kwargs...)
32+
required_args = get_parameters(process.parameters, :required)
33+
34+
length(required_args) == length(args) || error("Must provide $(length(required_args)) positional arguments")
35+
36+
args_d = Dict{Symbol,Any}(zip(map(x -> x.first, required_args), args))
37+
merge!(args_d, Dict(kwargs))
38+
39+
if isnothing(args_d)
40+
args_d = Dict{Symbol,Any}()
3841
end
39-
ProcessCall("$(process.id)", argument_dict)
42+
ProcessCall("$(process.id)", args_d)
4043
end
44+
4145
function Docs.getdoc(process::Process)
42-
arguments = get_parameters(process.parameters)
43-
args_str = join(["$(k)::$(v)" for (k, v) in arguments], ", ")
44-
docs = """ $(process.id)($(args_str))
46+
args_str = join(["$(k)::$(v)" for (k, v) in get_parameters(process.parameters, :required)], ", ")
47+
kwargs_str = join(["$(k)::$(v)" for (k, v) in get_parameters(process.parameters, :optional)], ", ")
48+
docs = """ $(process.id)($(args_str); $(kwargs_str))
4549
$(process.description)
4650
"""
4751
Markdown.parse(docs)
4852
end
4953
Base.Docs.doc(p::Process, ::Type=Union{}) = Base.Docs.getdoc(p)
5054

5155
function Base.show(io::IO, ::MIME"text/plain", p::Process)
52-
print(io, "$(p.id)($(join([x.name for x in p.parameters], ", "))): $(p.summary)")
56+
args_str = join(["$(k)::$(v)" for (k, v) in get_parameters(p.parameters, :required)], ", ")
57+
kwargs_str = join(["$(k)::$(v)" for (k, v) in get_parameters(p.parameters, :optional)], ", ")
58+
print(io, "$(p.id)($(args_str); $(kwargs_str)): $(p.summary)")
5359
end
5460

5561
# root e.g. https://earthengine.openeo.org/v1.0/processes
@@ -76,7 +82,6 @@ mutable struct ProcessCall <: AbstractProcessCall
7682
const arguments::Dict{Symbol,Any}
7783
result::Bool
7884
end
79-
ProcessCall(id, process_id, arguments) = ProcessCall(id, process_id, arguments, false)
8085
StructTypes.StructType(::Type{ProcessCall}) = StructTypes.Mutable()
8186
StructTypes.excludes(::Type{ProcessCall}) = (:id,)
8287

@@ -112,7 +117,7 @@ function Base.show(io::IO, ::MIME"text/plain", p::ProcessCall)
112117
pretty_print(io, Dict(:result => p.result))
113118
end
114119

115-
function get_parameters(parameters)
120+
function get_parameters(parameters, keep=:all)
116121
# openEO type string to Julia type
117122
julia_types_map = Dict(
118123
"string" => String,
@@ -145,7 +150,9 @@ function get_parameters(parameters)
145150
julia_types = [get(julia_types_map, t, String) for t in types]
146151
julia_type = Union{julia_types...}
147152

148-
push!(res, name => julia_type)
153+
if keep == :all || (keep == :optional && p.optional == true) || (keep == :required && isnothing(p.optional))
154+
push!(res, name => julia_type)
155+
end
149156
end
150157
return res
151158
end

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ password = ENV["OPENEO_PASSWORD"]
2222
# test sequential workflow
2323
step1 = c2.load_collection(
2424
"COPERNICUS/S2", BoundingBox(west=16.06, south=48.06, east=16.65, north=48.35),
25-
["2020-01-20", "2020-01-30"], ["B10"]
25+
["2020-01-20", "2020-01-30"]; bands=["B10"]
2626
)
2727
@test step1.id == "load_collection_tQ79zrFEGi8="
2828
@test step1.process_id == "load_collection"
2929
@test Set(keys(step1.arguments)) == Set([:bands, :id, :spatial_extent, :temporal_extent])
3030
@test step1.arguments[:bands] == ["B10"]
3131

32-
step2 = c2.reduce_dimension(step1, ProcessGraph("median"), "t", nothing)
33-
step3 = c2.save_result(step2, "JPEG", Dict())
32+
step2 = c2.reduce_dimension(step1, ProcessGraph("median"), "t")
33+
step3 = c2.save_result(step2, "JPEG")
3434
result = c2.compute_result(step3)
3535
@test result == "out.jpeg"
3636

0 commit comments

Comments
 (0)