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 by Bors] - Ensure that explicit YAML documents are generated for CRDs #450
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
64345dd
Ensure that explicit YAML documents are generated
siegfriedweber 36720e7
Remove check for YAML document type
siegfriedweber b0e31ae
Merge branch 'main' into yaml_module
siegfriedweber 44edca4
Serialize enums as YAML maps containing one entry in which the key id…
siegfriedweber File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,95 @@ | ||
//! Utility functions for processing data in the YAML file format | ||
use serde::ser; | ||
|
||
use crate::error::{Error, OperatorResult}; | ||
|
||
/// A YAML document type | ||
/// | ||
/// For a detailled description, see the | ||
/// [YAML specification](https://yaml.org/spec/1.2.2/#rule-l-any-document). | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub enum DocumentType { | ||
DirectiveDocument, | ||
ExplicitDocument, | ||
BareDocument, | ||
} | ||
|
||
/// Serializes the given data structure as an explicit YAML document. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Serialization can fail if `T`'s implementation of `Serialize` decides to return an error. | ||
/// | ||
/// An [`Error::UnsupportedYamlDocumentError`] is returned if the used `serde_yaml` version | ||
/// generates a directive document. | ||
pub fn to_explicit_document_string<T>(value: &T) -> OperatorResult<String> | ||
where | ||
T: ?Sized + ser::Serialize, | ||
{ | ||
// The returned document type depends on the serde_yaml version. | ||
let document = serde_yaml::to_string(value)?; | ||
nightkr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
match determine_document_type(&document) { | ||
DocumentType::DirectiveDocument => Err(Error::UnsupportedYamlDocumentError { | ||
message: "serde_yaml::to_string generated a directive document which cannot be \ | ||
converted to an explicit document." | ||
.into(), | ||
}), | ||
DocumentType::ExplicitDocument => Ok(document), | ||
DocumentType::BareDocument => Ok(format!("---\n{}", document)), | ||
} | ||
} | ||
|
||
/// Determines the type of the given YAML document. | ||
/// | ||
/// It is assumend that the given string contains a valid YAML document. | ||
pub fn determine_document_type(document: &str) -> DocumentType { | ||
if document.starts_with('%') { | ||
DocumentType::DirectiveDocument | ||
} else if document.starts_with("---") { | ||
DocumentType::ExplicitDocument | ||
} else { | ||
DocumentType::BareDocument | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::BTreeMap; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn value_can_be_serialized_to_an_explicit_document_string() { | ||
let value: BTreeMap<_, _> = [("key", "value")].into(); | ||
|
||
let actual_yaml = to_explicit_document_string(&value).expect("serializable value"); | ||
|
||
let expected_yaml = "\ | ||
---\n\ | ||
key: value\n"; | ||
|
||
assert_eq!(expected_yaml, actual_yaml); | ||
} | ||
|
||
#[test] | ||
fn document_type_can_be_determined() { | ||
let directive_document = "\ | ||
%YAML 1.2\n\ | ||
---\n\ | ||
key: value"; | ||
let explicit_document = "\ | ||
---\n\ | ||
key: value"; | ||
let bare_document = "\ | ||
key: value"; | ||
|
||
let directive_document_type = determine_document_type(directive_document); | ||
let explicit_document_type = determine_document_type(explicit_document); | ||
let bare_document_type = determine_document_type(bare_document); | ||
|
||
assert_eq!(DocumentType::DirectiveDocument, directive_document_type); | ||
assert_eq!(DocumentType::ExplicitDocument, explicit_document_type); | ||
assert_eq!(DocumentType::BareDocument, bare_document_type); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, does this come from serde_yaml? We might need to go back to the old tagging format somehow…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dtolnay/serde-yaml#298
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proposed solution in
serde_yaml:0.9.4
is to annotate every enum field with#[serde(with = "serde_yaml::with::singleton_map")]
, see dtolnay/serde-yaml#300.But this solution does not work together with
JsonSchema
, see GREsau/schemars#89.One workaround is to use the serde parameters
serialize_with
anddeserialize_with
:This becomes quite verbose.
In which cases do the YAML tags actually pose a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
serde_yaml:0.9.9
introduced singleton_map_recursive which allows to serialize enums as YAML maps containing one entry in which the key identifies the variant name.This is now used in
stackable_operator::yaml::serialize_to_explicit_document
. Further annotations are not necessary in the operators.