Skip to content

Commit

Permalink
Merge pull request #113 from Malax/schneems/layer-caching-opt-in-part-1
Browse files Browse the repository at this point in the history
Support deserializing empty <layer>.toml
  • Loading branch information
schneems authored Oct 21, 2021
2 parents f72b75d + fb30265 commit 3d4c50e
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions src/data/layer_content_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ pub struct LayerContentTypeTable {
pub cache: bool,
}

impl LayerContentTypeTable {
fn default() -> Self {
LayerContentTypeTable {
launch: false,
build: false,
cache: false,
}
}
}

/// Describes Layer Content Metadata
///
/// See [Cloud Native Buildpack specification](https://github.com/buildpacks/spec/blob/main/buildpack.md#layer-content-metadata-toml)
Expand All @@ -45,6 +55,7 @@ pub struct LayerContentTypeTable {
/// ```
#[derive(Debug, Deserialize, Serialize)]
pub struct LayerContentMetadata<M> {
#[serde(default = "LayerContentTypeTable::default")]
pub types: LayerContentTypeTable,

/// Metadata that describes the layer contents.
Expand All @@ -54,11 +65,7 @@ pub struct LayerContentMetadata<M> {
impl Default for LayerContentMetadata<GenericMetadata> {
fn default() -> Self {
LayerContentMetadata {
types: LayerContentTypeTable {
launch: false,
build: false,
cache: false,
},
types: LayerContentTypeTable::default(),
metadata: GenericMetadata::default(),
}
}
Expand Down Expand Up @@ -97,6 +104,31 @@ impl<M> LayerContentMetadata<M> {
mod tests {
use super::*;

#[test]
fn layer_types_have_defaults() {
let layer: Result<LayerContentMetadata<Option<toml::value::Table>>, toml::de::Error> =
toml::from_str(
r#"
[types]
"#,
);

let layer = layer.unwrap();
assert_eq!(layer.metadata, None);
assert!(!layer.types.launch);
assert!(!layer.types.build);
assert!(!layer.types.cache);

let layer: Result<LayerContentMetadata<Option<toml::value::Table>>, toml::de::Error> =
toml::from_str(r#""#);

let layer = layer.unwrap();
assert_eq!(layer.metadata, None);
assert!(!layer.types.launch);
assert!(!layer.types.build);
assert!(!layer.types.cache);
}

#[test]
fn metadata_is_optional() {
let layer: Result<LayerContentMetadata<Option<toml::value::Table>>, toml::de::Error> =
Expand Down

0 comments on commit 3d4c50e

Please sign in to comment.