Skip to content

Commit

Permalink
fix: skip serializing default fields (#401)
Browse files Browse the repository at this point in the history
* fix: skip serializing default fields

* fix: small test issue
  • Loading branch information
baszalmstra authored Dec 7, 2023
1 parent 1832dd2 commit e77f97a
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 86 deletions.
5 changes: 3 additions & 2 deletions src/recipe/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ use super::custom_yaml::Node;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recipe {
package: Package,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
source: Vec<Source>,
build: Build,
requirements: Requirements,
#[serde(default, skip_serializing_if = "Test::is_default")]
test: Test,
#[serde(default, skip_serializing_if = "About::is_default")]
about: About,
extra: (),
}

impl Recipe {
Expand Down Expand Up @@ -175,7 +177,6 @@ impl Recipe {
requirements,
test,
about,
extra: (),
};

Ok(recipe)
Expand Down
15 changes: 15 additions & 0 deletions src/recipe/parser/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,34 @@ use crate::{
/// About information.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct About {
#[serde(skip_serializing_if = "Option::is_none")]
homepage: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
repository: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
documentation: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
license: Option<License>,
#[serde(skip_serializing_if = "Option::is_none")]
license_family: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
license_files: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
license_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
summary: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
prelink_message: Option<String>,
}

impl About {
/// Returns true if the about has its default configuration.
pub fn is_default(&self) -> bool {
self == &Self::default()
}

/// Get the homepage.
pub const fn homepage(&self) -> Option<&Url> {
self.homepage.as_ref()
Expand Down
9 changes: 9 additions & 0 deletions src/recipe/parser/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ use crate::{
},
};

/// A helper method to skip serializing the `skip` field if it's false.
fn should_not_serialize_skip(skip: &bool) -> bool {
!skip
}

/// The build options contain information about how to build the package and some additional
/// metadata about the package.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand All @@ -23,13 +28,17 @@ pub struct Build {
pub(super) number: u64,
/// The build string is usually set automatically as the hash of the variant configuration.
/// It's possible to override this by setting it manually, but not recommended.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) string: Option<String>,
/// List of conditions under which to skip the build of the package.
#[serde(default, skip_serializing_if = "should_not_serialize_skip")]
pub(super) skip: bool,
/// The build script can be either a list of commands or a path to a script. By
/// default, the build script is set to `build.sh` or `build.bat` on Unix and Windows respectively.
#[serde(default, skip_serializing_if = "Script::is_default")]
pub(super) script: Script,
/// A noarch package runs on any platform. It can be either a python package or a generic package.
#[serde(default, skip_serializing_if = "NoArchType::is_none")]
pub(super) noarch: NoArchType,
/// Python specific build configuration
#[serde(default, skip_serializing_if = "Python::is_default")]
Expand Down
1 change: 1 addition & 0 deletions src/recipe/parser/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl TryConvertNode<Package> for RenderedMappingNode {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OutputPackage {
name: PackageName,
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<String>,
}

Expand Down
9 changes: 9 additions & 0 deletions src/recipe/parser/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ impl Script {
pub fn secrets(&self) -> &[String] {
self.secrets.as_slice()
}

/// Returns true if the script references the default build script and has no additional
/// configuration.
pub fn is_default(&self) -> bool {
self.content.is_default()
&& self.interpreter.is_none()
&& self.env.is_empty()
&& self.secrets.is_empty()
}
}

impl From<ScriptContent> for Script {
Expand Down
21 changes: 21 additions & 0 deletions src/recipe/parser/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,24 @@ pub struct GitSource {
#[serde(default)]
rev: String,
/// Optionally a depth to clone the repository, defaults to `None`
#[serde(skip_serializing_if = "Option::is_none")]
depth: Option<i32>,
/// Optionally patches to apply to the source code
#[serde(default, skip_serializing_if = "Vec::is_empty")]
patches: Vec<PathBuf>,
/// Optionally a folder name under the `work` directory to place the source code
#[serde(skip_serializing_if = "Option::is_none")]
folder: Option<PathBuf>,
/// Optionally request the lfs pull in git source
#[serde(skip_serializing_if = "should_not_serialize_lfs")]
lfs: bool,
}

/// A helper method to skip serializing the lfs flag if it is false.
fn should_not_serialize_lfs(lfs: &bool) -> bool {
!lfs
}

impl GitSource {
#[cfg(test)]
pub fn create(
Expand Down Expand Up @@ -283,10 +292,13 @@ pub struct UrlSource {
md5: Option<Md5Hash>,

/// Optionally a file name to rename the downloaded file (does not apply to archives)
#[serde(skip_serializing_if = "Option::is_none")]
file_name: Option<String>,
/// Patches to apply to the source code
#[serde(default, skip_serializing_if = "Vec::is_empty")]
patches: Vec<PathBuf>,
/// Optionally a folder name under the `work` directory to place the source code
#[serde(skip_serializing_if = "Option::is_none")]
folder: Option<PathBuf>,
}

Expand Down Expand Up @@ -401,15 +413,24 @@ pub struct PathSource {
/// Path to the local source code
path: PathBuf,
/// Patches to apply to the source code
#[serde(default, skip_serializing_if = "Vec::is_empty")]
patches: Vec<PathBuf>,
/// Optionally a folder name under the `work` directory to place the source code
#[serde(skip_serializing_if = "Option::is_none")]
folder: Option<PathBuf>,
/// Optionally a file name to rename the file to
#[serde(skip_serializing_if = "Option::is_none")]
file_name: Option<PathBuf>,
/// Whether to use the `.gitignore` file in the source directory. Defaults to `true`.
#[serde(skip_serializing_if = "should_not_serialize_use_gitignore")]
use_gitignore: bool,
}

/// Helper method to skip serializing the use_gitignore flag if it is true.
fn should_not_serialize_use_gitignore(use_gitignore: &bool) -> bool {
*use_gitignore
}

impl PathSource {
/// Get the path.
pub const fn path(&self) -> &PathBuf {
Expand Down
18 changes: 18 additions & 0 deletions src/recipe/parser/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,50 @@ use crate::{
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Test {
/// Try importing a python module as a sanity check
#[serde(default, skip_serializing_if = "Vec::is_empty")]
imports: Vec<String>,
/// Run a list of given commands
#[serde(default, skip_serializing_if = "Vec::is_empty")]
commands: Vec<String>,
/// Extra requirements to be installed at test time
#[serde(default, skip_serializing_if = "Vec::is_empty")]
requires: Vec<String>,
/// Extra files to be copied to the test environment from the source dir (can be globs)
#[serde(default, skip_serializing_if = "Vec::is_empty")]
source_files: Vec<String>,
/// Extra files to be copied to the test environment from the build dir (can be globs)
#[serde(default, skip_serializing_if = "Vec::is_empty")]
files: Vec<String>,
/// <!-- TODO: use a better name: --> All new test section
#[serde(skip_serializing_if = "Option::is_none")]
package_contents: Option<PackageContent>,
}

impl Test {
/// Returns true if the test has its default configuration.
pub fn is_default(&self) -> bool {
self == &Self::default()
}
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
/// PackageContent
pub struct PackageContent {
/// file paths, direct and/or globs
#[serde(default, skip_serializing_if = "Vec::is_empty")]
files: Vec<String>,
/// checks existence of package init in env python site packages dir
/// eg: mamba.api -> ${SITE_PACKAGES}/mamba/api/__init__.py
#[serde(default, skip_serializing_if = "Vec::is_empty")]
site_packages: Vec<String>,
/// search for binary in prefix path: eg, %PREFIX%/bin/mamba
#[serde(default, skip_serializing_if = "Vec::is_empty")]
bins: Vec<String>,
/// check for dynamic or static library file path
#[serde(default, skip_serializing_if = "Vec::is_empty")]
libs: Vec<String>,
/// check if include path contains the file, direct or glob?
#[serde(default, skip_serializing_if = "Vec::is_empty")]
includes: Vec<String>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,4 @@ Recipe {
),
prelink_message: None,
},
extra: (),
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,9 @@ expression: recipe
package:
name: blib
version: 0.1.0
source: []
build:
number: 0
string: ~
skip: false
script:
- test succeeded
noarch: false
requirements: {}
test:
imports: []
commands: []
requires: []
source_files: []
files: []
package_contents: ~
about:
homepage: ~
repository: ~
documentation: ~
license: ~
license_family: ~
license_files: []
license_url: ~
summary: ~
description: ~
prelink_message: ~
extra: ~

Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,4 @@ Recipe {
),
prelink_message: None,
},
extra: (),
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,25 @@ recipe:
source:
- url: "http://curl.haxx.se/download/curl-8.0.1.tar.bz2"
sha256: 9b6b1e96b748d04b968786b6bdf407aa5c75ab53a3d37c1c8c81cdb736555ccf
file_name: ~
patches: []
folder: ~
build:
number: 0
string: h60d57d3_0
skip: false
script: []
noarch: false
requirements:
build:
- __COMPILER c
- make
- perl
- pkg-config
- libtool
test:
imports: []
commands: []
requires: []
source_files: []
files: []
package_contents: ~
about:
homepage: "http://curl.haxx.se/"
repository: "https://github.com/curl/curl"
documentation: "https://curl.haxx.se/docs/"
license: curl
license_family: ~
license_files:
- COPYING
license_url: ~
summary: tool and library for transferring data with URL syntax
description: "Curl is an open source command line tool and library for transferring data\nwith URL syntax. It is used in command lines or scripts to transfer data."
prelink_message: ~
extra: ~
build_configuration:
target_platform: osx-arm64
host_platform: osx-arm64
Expand Down
11 changes: 0 additions & 11 deletions src/snapshots/rattler_build__metadata__test__read_full_recipe.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ recipe:
source:
- url: "https://pypi.io/packages/source/r/rich/rich-13.4.2.tar.gz"
sha256: d653d6bccede5844304c605d5aac802c7cf9621efd700b46c7ec2b51ea914898
file_name: ~
patches: []
folder: ~
build:
number: 0
string: pyh4616a5c_0
skip: false
script:
- python -m pip install . -vv --no-deps --no-build-isolation
noarch: python
Expand All @@ -36,22 +32,15 @@ recipe:
- pip check
requires:
- pip
source_files: []
files: []
package_contents: ~
about:
homepage: "https://github.com/Textualize/rich"
repository: "https://github.com/Textualize/rich"
documentation: "https://rich.readthedocs.io/"
license: MIT
license_family: ~
license_files:
- LICENSE
license_url: ~
summary: "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
description: "Rich is a Python library for rich text and beautiful formatting in the terminal.\n\nThe Rich API makes it easy to add color and style to terminal output. Rich\ncan also render pretty tables, progress bars, markdown, syntax highlighted\nsource code, tracebacks, and more — out of the box."
prelink_message: ~
extra: ~
build_configuration:
target_platform: noarch
host_platform: osx-arm64
Expand Down
19 changes: 0 additions & 19 deletions test-data/rendered_recipes/curl_recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,27 @@ recipe:
source:
- url: http://curl.haxx.se/download/curl-8.0.1.tar.bz2
sha256: 9b6b1e96b748d04b968786b6bdf407aa5c75ab53a3d37c1c8c81cdb736555ccf
file_name: null
patches: []
folder: null
build:
number: 0
string: h60d57d3_0
skip: false
script: []
noarch: false
requirements:
build:
- __COMPILER c
- make
- perl
- pkg-config
- libtool
host: []
run: []
run_constrained: []
test:
imports: []
commands: []
requires: []
source_files: []
files: []
about:
homepage: http://curl.haxx.se/
repository: https://github.com/curl/curl
documentation: https://curl.haxx.se/docs/
license: curl
license_family: null
license_files:
- COPYING
license_url: null
summary: tool and library for transferring data with URL syntax
description: |-
Curl is an open source command line tool and library for transferring data
with URL syntax. It is used in command lines or scripts to transfer data.
prelink_message: null
extra: null
build_configuration:
target_platform: osx-arm64
host_platform: osx-arm64
Expand Down
Loading

0 comments on commit e77f97a

Please sign in to comment.