-
Notifications
You must be signed in to change notification settings - Fork 40
update openapi de-serialize #607
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # OpenAPI serde for the supplemental attributes InfrastructureSystems itself owns. | ||
| # | ||
| # `GeographicInfo` and `DataSource` are IS types, so their field mapping belongs here rather | ||
| # than in a domain package: PowerSystems previously carried the `GeographicInfo` pair only | ||
| # because it happened to own the document walk, which meant a second domain package wanting | ||
| # the same attribute would have had to duplicate it. | ||
| # | ||
| # These take no `OpenAPIRefs`. That registry lives in the domain package because it carries | ||
| # the document's `unit_system` and `base_power` — power-domain state IS has no notion of — | ||
| # so the export direction takes the id its caller already resolved. Neither type has a | ||
| # unit-bearing field, so there is nothing else the domain layer would need to supply. | ||
|
|
||
| """ | ||
| Convert an OpenAPI-model instance into the matching Sienna type. | ||
|
|
||
| Declared here, extended by domain packages: `PowerSystems` adds a method per component and | ||
| per attribute it owns, so `from_openapi` stays one function across the stack rather than one | ||
| per package. | ||
| """ | ||
| function from_openapi end | ||
|
|
||
| """ | ||
| Convert a Sienna component or attribute into its OpenAPI-model representation. | ||
|
|
||
| The counterpart of [`from_openapi`](@ref); the same extension rule applies. | ||
| """ | ||
| function to_openapi end | ||
|
|
||
| # ── GeographicInfo ────────────────────────────────────────────────────────────── | ||
|
|
||
| from_openapi(po::PowerCoreOpenAPIModels.GeographicInfo) = | ||
| GeographicInfo(; geo_json = po.geo_json) | ||
|
|
||
| to_openapi(geo::GeographicInfo, id::Int) = | ||
| PowerCoreOpenAPIModels.GeographicInfo(; id = id, geo_json = get_geo_json(geo)) | ||
|
|
||
|
jd-lara marked this conversation as resolved.
|
||
| # ── DataSource ────────────────────────────────────────────────────────────────── | ||
| # | ||
| # The document states both timestamps as `ZonedDateTime` while `DataSource` stores plain | ||
| # `DateTime`, so import drops the offset after normalizing to UTC and export re-attaches | ||
| # UTC. Normalizing rather than discarding the zone matters: two documents recording the same | ||
| # instant in different zones must import to the same `DateTime`, which `DateTime(zdt)` alone | ||
| # would not give. | ||
| # | ||
| # `extra` widens `Dict{String, String}` to the `Dict{String, Any}` the field declares; on the | ||
| # way out, values are stringified, since the schema types that map as strings. | ||
|
|
||
| _datasource_utc(zdt) = Dates.DateTime(TimeZones.astimezone(zdt, TimeZones.tz"UTC")) | ||
| _datasource_utc(::Nothing) = nothing | ||
|
|
||
| _datasource_zoned(dt::Dates.DateTime) = TimeZones.ZonedDateTime(dt, TimeZones.tz"UTC") | ||
|
|
||
| # `published_at`/`recorded_by` are absence-by-predicate, not absence-by-`nothing`: their | ||
| # accessors error rather than return a sentinel, so the export path asks first. An absent | ||
| # field is written as `null`, which the schema marks optional. | ||
| function _datasource_published_at(ds::DataSource) | ||
| has_published_at(ds) || return nothing | ||
| return _datasource_zoned(get_published_at(ds)) | ||
| end | ||
|
|
||
| function _datasource_recorded_by(ds::DataSource) | ||
| has_recorded_by(ds) || return nothing | ||
| return get_recorded_by(ds) | ||
| end | ||
|
|
||
| _datasource_fields(::Nothing) = String[] | ||
| _datasource_fields(v) = collect(String, v) | ||
|
|
||
| _datasource_extra(::Nothing) = Dict{String, Any}() | ||
| _datasource_extra(d) = Dict{String, Any}(k => v for (k, v) in d) | ||
|
|
||
| function from_openapi(po::PowerCoreOpenAPIModels.DataSource) | ||
| return DataSource(; | ||
| organization = po.organization, | ||
| retrieved_at = _datasource_utc(po.retrieved_at), | ||
| dataset = po.dataset, | ||
| url = po.url, | ||
| version = po.version, | ||
| published_at = _datasource_utc(po.published_at), | ||
| confidence = po.confidence, | ||
| recorded_by = po.recorded_by, | ||
| fields = _datasource_fields(po.fields), | ||
| extra = _datasource_extra(po.extra), | ||
| ) | ||
| end | ||
|
|
||
| function to_openapi(ds::DataSource, id::Int) | ||
| return PowerCoreOpenAPIModels.DataSource(; | ||
| id = id, | ||
| organization = get_organization(ds), | ||
| retrieved_at = _datasource_zoned(get_retrieved_at(ds)), | ||
| dataset = get_dataset(ds), | ||
| url = get_url(ds), | ||
| version = get_version(ds), | ||
| published_at = _datasource_published_at(ds), | ||
| confidence = get_confidence(ds), | ||
| recorded_by = _datasource_recorded_by(ds), | ||
| fields = get_fields(ds), | ||
| extra = Dict{String, String}(k => string(v) for (k, v) in get_extra(ds)), | ||
| ) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.