Skip to content
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

Add DynamicObjects::try_parse for typed object conversion #1061

Merged
merged 1 commit into from Oct 31, 2022

Conversation

jmintb
Copy link
Contributor

@jmintb jmintb commented Oct 26, 2022

Signed-off-by: Jessie Chatham Spencer jessie@teainspace.com

Motivation

Issue #1029 requests a method to convert a DynamicObject into a Resource. Such a method would avoid having to repeatedly implement this logic.

Solution

This PR implements the DynamicObject::try_parse method to facilitate easy conversion from a DynamicObject to a Resource. The method uses serde_json to first serialize the DynamicObject and then deserialize the JSON to the Resource.

A small error type ParseDynamicObjectError has been implemented as well to represent a parsing failure. It wraps the original serde_json::Error so the original error information is preserved.

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great. thanks a lot. minor nits on error type only.

kube-core/src/dynamic.rs Show resolved Hide resolved
kube-core/src/dynamic.rs Show resolved Hide resolved
kube-core/src/dynamic.rs Show resolved Hide resolved
kube-core/src/dynamic.rs Outdated Show resolved Hide resolved
@clux clux added the changelog-add changelog added category for prs label Oct 28, 2022
@clux clux added this to the 0.77.0 milestone Oct 28, 2022
@clux clux changed the title Implement a method for parsing DynamicObjects Add DynamicObjects::try_parse for dyn -> typed object conversion Oct 28, 2022
@clux clux changed the title Add DynamicObjects::try_parse for dyn -> typed object conversion Add DynamicObjects::try_parse for typed object conversion Oct 28, 2022
@codecov-commenter
Copy link

codecov-commenter commented Oct 28, 2022

Codecov Report

Merging #1061 (98cf715) into main (e104bea) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1061      +/-   ##
==========================================
+ Coverage   72.32%   72.36%   +0.04%     
==========================================
  Files          65       65              
  Lines        4704     4712       +8     
==========================================
+ Hits         3402     3410       +8     
  Misses       1302     1302              
Impacted Files Coverage Δ
kube-core/src/dynamic.rs 72.00% <100.00%> (+5.33%) ⬆️

Issue: kube-rs#1029

To avoid manually parsing DynamicObjects into
resources, this commit implements DynamicOjbect::try_parse. The
try_parse method will attempt to parse the DynamicObject into a type
implementing the Resource trait, using serde.

Signed-off-by: Jessie Chatham Spencer <jessie@teainspace.com>
Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@clux clux merged commit a7861aa into kube-rs:main Oct 31, 2022
@clux clux linked an issue Nov 11, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-add changelog added category for prs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a DynamicObject::downcast method
3 participants