Skip to content

Commit 4de7868

Browse files
authored
Merge pull request #132 from control-toolbox/131-dev-error-extensions
Extension error fixed
2 parents ac8d281 + 0e13ff7 commit 4de7868

File tree

5 files changed

+39
-34
lines changed

5 files changed

+39
-34
lines changed

ext/CTModelsJLD.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ julia> export_ocp_solution(JLD2Tag(), sol; filename="mysolution")
2525
# → creates "mysolution.jld2"
2626
```
2727
"""
28-
function CTModels.export_ocp_solution(
29-
::CTModels.JLD2Tag, sol::CTModels.Solution; filename::String="solution"
30-
)
28+
function CTModels.export_ocp_solution(::CTModels.JLD2Tag, sol::CTModels.Solution; filename::String)
3129
save_object(filename * ".jld2", sol)
3230
return nothing
3331
end
@@ -54,9 +52,7 @@ This function loads a previously saved `CTModels.Solution` from disk.
5452
julia> sol = import_ocp_solution(JLD2Tag(), model; filename="mysolution")
5553
```
5654
"""
57-
function CTModels.import_ocp_solution(
58-
::CTModels.JLD2Tag, ocp::CTModels.Model; filename::String="solution"
59-
)
55+
function CTModels.import_ocp_solution(::CTModels.JLD2Tag, ocp::CTModels.Model; filename::String)
6056
return load_object(filename * ".jld2")
6157
end
6258

ext/CTModelsJSON.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ julia> export_ocp_solution(JSON3Tag(), sol; filename="mysolution")
2929
# → creates "mysolution.json"
3030
```
3131
"""
32-
function CTModels.export_ocp_solution(
33-
::CTModels.JSON3Tag, sol::CTModels.Solution; filename::String="solution"
34-
)
32+
function CTModels.export_ocp_solution(::CTModels.JSON3Tag, sol::CTModels.Solution; filename::String)
3533
T = CTModels.time_grid(sol)
3634

3735
blob = Dict(
@@ -94,9 +92,7 @@ Handles both vector and matrix encodings of signals. If dual fields are missing
9492
julia> sol = import_ocp_solution(JSON3Tag(), model; filename="mysolution")
9593
```
9694
"""
97-
function CTModels.import_ocp_solution(
98-
::CTModels.JSON3Tag, ocp::CTModels.Model; filename::String="solution"
99-
)
95+
function CTModels.import_ocp_solution(::CTModels.JSON3Tag, ocp::CTModels.Model; filename::String)
10096
json_string = read(filename * ".json", String)
10197
blob = JSON3.read(json_string)
10298

src/CTModels.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ const ConstraintsDictType = OrderedDict{
104104
#
105105
include("default.jl")
106106

107+
#
108+
include("utils.jl")
109+
include("types.jl")
110+
107111
# export / import
108112
"""
109113
$(TYPEDEF)
@@ -132,28 +136,28 @@ $(TYPEDSIGNATURES)
132136
133137
Export a solution in JLD format.
134138
"""
135-
export_ocp_solution(::JLD2Tag, args...; kwargs...) = throw(CTBase.ExtensionError(:JLD2))
139+
export_ocp_solution(::JLD2Tag, ::AbstractSolution; filename::String) = throw(CTBase.ExtensionError(:JLD2))
136140

137141
"""
138142
$(TYPEDSIGNATURES)
139143
140144
Import a solution from a JLD file.
141145
"""
142-
import_ocp_solution(::JLD2Tag, args...; kwargs...) = throw(CTBase.ExtensionError(:JLD2))
146+
import_ocp_solution(::JLD2Tag, ::AbstractModel; filename::String) = throw(CTBase.ExtensionError(:JLD2))
143147

144148
"""
145149
$(TYPEDSIGNATURES)
146150
147151
Export a solution in JSON format.
148152
"""
149-
export_ocp_solution(::JSON3Tag, args...; kwargs...) = throw(CTBase.ExtensionError(:JSON3))
153+
export_ocp_solution(::JSON3Tag, ::AbstractSolution; filename::String) = throw(CTBase.ExtensionError(:JSON3))
150154

151155
"""
152156
$(TYPEDSIGNATURES)
153157
154158
Import a solution from a JLD file.
155159
"""
156-
import_ocp_solution(::JSON3Tag, args...; kwargs...) = throw(CTBase.ExtensionError(:JSON3))
160+
import_ocp_solution(::JSON3Tag, ::AbstractModel; filename::String) = throw(CTBase.ExtensionError(:JSON3))
157161

158162
"""
159163
$(TYPEDSIGNATURES)
@@ -167,11 +171,11 @@ julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JSON)
167171
julia> CTModels.export_ocp_solution(sol; filename="solution", format=:JLD)
168172
```
169173
"""
170-
function export_ocp_solution(args...; format::Symbol=__format(), kwargs...)
174+
function export_ocp_solution(sol::AbstractSolution; format::Symbol=__format(), filename::String=__filename_export_import())
171175
if format == :JLD
172-
return export_ocp_solution(JLD2Tag(), args...; kwargs...)
176+
return export_ocp_solution(JLD2Tag(), sol; filename=filename)
173177
elseif format == :JSON
174-
return export_ocp_solution(JSON3Tag(), args...; kwargs...)
178+
return export_ocp_solution(JSON3Tag(), sol; filename=filename)
175179
else
176180
throw(
177181
CTBase.IncorrectArgument(
@@ -193,11 +197,11 @@ julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JSON
193197
julia> sol = CTModels.import_ocp_solution(ocp; filename="solution", format=:JLD)
194198
```
195199
"""
196-
function import_ocp_solution(args...; format::Symbol=__format(), kwargs...)
200+
function import_ocp_solution(ocp::AbstractModel; format::Symbol=__format(), filename::String=__filename_export_import())
197201
if format == :JLD
198-
return import_ocp_solution(JLD2Tag(), args...; kwargs...)
202+
return import_ocp_solution(JLD2Tag(), ocp; filename=filename)
199203
elseif format == :JSON
200-
return import_ocp_solution(JSON3Tag(), args...; kwargs...)
204+
return import_ocp_solution(JSON3Tag(), ocp; filename=filename)
201205
else
202206
throw(
203207
CTBase.IncorrectArgument(
@@ -207,10 +211,6 @@ function import_ocp_solution(args...; format::Symbol=__format(), kwargs...)
207211
end
208212
end
209213

210-
#
211-
include("utils.jl")
212-
include("types.jl")
213-
214214
# to be extended
215215
"""
216216
$(TYPEDSIGNATURES)

src/default.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,9 @@ Used to set the default value of the storage of elements in a matrix.
102102
The default value is `1`.
103103
"""
104104
__matrix_dimension_storage() = 1
105+
106+
"""
107+
$(TYPEDSIGNATURES)
108+
109+
"""
110+
__filename_export_import() = "solution"

test/test_ext_exceptions.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
function test_ext_exceptions()
22

3-
@test_throws CTBase.IncorrectArgument CTModels.export_ocp_solution(format=:dummy)
4-
@test_throws CTBase.ExtensionError CTModels.export_ocp_solution(format=:JSON)
5-
@test_throws CTBase.ExtensionError CTModels.export_ocp_solution(format=:JLD)
3+
ocp, sol, pre_ocp = solution_example()
64

7-
@test_throws CTBase.IncorrectArgument CTModels.import_ocp_solution(format=:dummy)
8-
@test_throws CTBase.ExtensionError CTModels.import_ocp_solution(format=:JSON)
9-
@test_throws CTBase.ExtensionError CTModels.import_ocp_solution(format=:JLD)
5+
# export
6+
@test_throws CTBase.IncorrectArgument CTModels.export_ocp_solution(sol; format=:dummy)
7+
@test_throws CTBase.ExtensionError CTModels.export_ocp_solution(sol; format=:JSON)
8+
@test_throws CTBase.ExtensionError CTModels.export_ocp_solution(sol; format=:JLD)
9+
@test_throws MethodError CTModels.export_ocp_solution()
1010

11-
ocp, sol, pre_ocp = solution_example()
11+
# import
12+
@test_throws CTBase.IncorrectArgument CTModels.import_ocp_solution(ocp; format=:dummy)
13+
@test_throws CTBase.ExtensionError CTModels.import_ocp_solution(ocp; format=:JSON)
14+
@test_throws CTBase.ExtensionError CTModels.import_ocp_solution(ocp; format=:JLD)
15+
@test_throws MethodError CTModels.import_ocp_solution()
16+
17+
# plot
1218
@test_throws CTBase.ExtensionError CTModels.plot(sol)
13-
19+
@test_throws MethodError CTModels.plot(sol, 1)
20+
1421
end

0 commit comments

Comments
 (0)