-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat(k8s-version): Add serde support #1034
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
716f905
feat(k8s-version): Add serde support
Techassi 98d8da6
refactor(k8s-version): Move darling code into separate files
Techassi 8fd0c8f
chore(k8s-version): Add changelog entry
Techassi db25ac0
test(k8s-version): Add unit tests for (de)serialization
Techassi 78dd665
chore(k8s-version): Correct typos in changelog
Techassi 65d2845
chore: Apply suggestion
Techassi 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use serde::{Deserialize, Serialize, de::Visitor}; | ||
|
|
||
| use crate::ApiVersion; | ||
|
|
||
| impl<'de> Deserialize<'de> for ApiVersion { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| struct ApiVersionVisitor; | ||
|
|
||
| impl<'de> Visitor<'de> for ApiVersionVisitor { | ||
| type Value = ApiVersion; | ||
|
|
||
| fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
| write!(formatter, "a valid Kubernetes API version") | ||
| } | ||
|
|
||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
| where | ||
| E: serde::de::Error, | ||
| { | ||
| ApiVersion::from_str(v).map_err(serde::de::Error::custom) | ||
| } | ||
| } | ||
|
|
||
| deserializer.deserialize_str(ApiVersionVisitor) | ||
| } | ||
| } | ||
|
|
||
| impl Serialize for ApiVersion { | ||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: serde::Serializer, | ||
| { | ||
| serializer.serialize_str(&self.to_string()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "serde")] | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn deserialize() { | ||
| let _: ApiVersion = | ||
| serde_yaml::from_str("extensions.k8s.io/v1alpha1").expect("api version is valid"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize() { | ||
| let api_version = | ||
| ApiVersion::from_str("extensions.k8s.io/v1alpha1").expect("api version is valid"); | ||
| assert_eq!( | ||
| "extensions.k8s.io/v1alpha1\n", | ||
| serde_yaml::to_string(&api_version).expect("api version must serialize") | ||
| ); | ||
| } | ||
| } |
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,34 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use darling::FromMeta; | ||
|
|
||
| use crate::Level; | ||
|
|
||
| impl FromMeta for Level { | ||
| fn from_string(value: &str) -> darling::Result<Self> { | ||
| Self::from_str(value).map_err(darling::Error::custom) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use quote::quote; | ||
| use rstest::rstest; | ||
|
|
||
| use super::*; | ||
|
|
||
| fn parse_meta(tokens: proc_macro2::TokenStream) -> ::std::result::Result<syn::Meta, String> { | ||
| let attribute: syn::Attribute = syn::parse_quote!(#[#tokens]); | ||
| Ok(attribute.meta) | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case(quote!(ignore = "alpha12"), Level::Alpha(12))] | ||
| #[case(quote!(ignore = "alpha1"), Level::Alpha(1))] | ||
| #[case(quote!(ignore = "beta1"), Level::Beta(1))] | ||
| fn from_meta(#[case] input: proc_macro2::TokenStream, #[case] expected: Level) { | ||
| let meta = parse_meta(input).expect("valid attribute tokens"); | ||
| let version = Level::from_meta(&meta).expect("level must parse from attribute"); | ||
| assert_eq!(version, expected); | ||
| } | ||
| } |
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,60 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use serde::{Deserialize, Serialize, de::Visitor}; | ||
|
|
||
| use crate::Level; | ||
|
|
||
| impl<'de> Deserialize<'de> for Level { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| struct LevelVisitor; | ||
|
|
||
| impl<'de> Visitor<'de> for LevelVisitor { | ||
| type Value = Level; | ||
|
|
||
| fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
| write!(formatter, "a valid Kubernetes API version level") | ||
| } | ||
|
|
||
| fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
| where | ||
| E: serde::de::Error, | ||
| { | ||
| Level::from_str(v).map_err(serde::de::Error::custom) | ||
| } | ||
| } | ||
|
|
||
| deserializer.deserialize_str(LevelVisitor) | ||
| } | ||
| } | ||
|
|
||
| impl Serialize for Level { | ||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: serde::Serializer, | ||
| { | ||
| serializer.serialize_str(&self.to_string()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "serde")] | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn deserialize() { | ||
| let _: Level = serde_yaml::from_str("alpha1").expect("level is valid"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serialize() { | ||
| let api_version = Level::from_str("alpha1").expect("level is valid"); | ||
| assert_eq!( | ||
| "alpha1\n", | ||
| serde_yaml::to_string(&api_version).expect("level must serialize") | ||
| ); | ||
| } | ||
| } |
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,37 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use darling::FromMeta; | ||
|
|
||
| use crate::Version; | ||
|
|
||
| impl FromMeta for Version { | ||
| fn from_string(value: &str) -> darling::Result<Self> { | ||
| Self::from_str(value).map_err(darling::Error::custom) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use quote::quote; | ||
| use rstest::rstest; | ||
|
|
||
| use super::*; | ||
| use crate::Level; | ||
|
|
||
| fn parse_meta(tokens: proc_macro2::TokenStream) -> ::std::result::Result<syn::Meta, String> { | ||
| let attribute: syn::Attribute = syn::parse_quote!(#[#tokens]); | ||
| Ok(attribute.meta) | ||
| } | ||
|
|
||
| #[cfg(feature = "darling")] | ||
| #[rstest] | ||
| #[case(quote!(ignore = "v1alpha12"), Version { major: 1, level: Some(Level::Alpha(12)) })] | ||
| #[case(quote!(ignore = "v1alpha1"), Version { major: 1, level: Some(Level::Alpha(1)) })] | ||
| #[case(quote!(ignore = "v1beta1"), Version { major: 1, level: Some(Level::Beta(1)) })] | ||
| #[case(quote!(ignore = "v1"), Version { major: 1, level: None })] | ||
| fn from_meta(#[case] input: proc_macro2::TokenStream, #[case] expected: Version) { | ||
| let meta = parse_meta(input).expect("valid attribute tokens"); | ||
| let version = Version::from_meta(&meta).expect("version must parse from attribute"); | ||
| assert_eq!(version, expected); | ||
| } | ||
| } |
Oops, something went wrong.
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.