Skip to content

Commit

Permalink
fix: Fix serialization of custom languages. (#739)
Browse files Browse the repository at this point in the history
* Test serde.

* Test deser.

* Bump version.
  • Loading branch information
milesj authored Mar 23, 2023
1 parent 2e17313 commit 9b598b3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .yarn/versions/4745cdf3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
releases:
"@moonrepo/cli": patch
"@moonrepo/core-linux-arm64-gnu": patch
"@moonrepo/core-linux-arm64-musl": patch
"@moonrepo/core-linux-x64-gnu": patch
"@moonrepo/core-linux-x64-musl": patch
"@moonrepo/core-macos-arm64": patch
"@moonrepo/core-macos-x64": patch
"@moonrepo/core-windows-x64-msvc": patch
72 changes: 70 additions & 2 deletions crates/core/config/src/project/language_platform.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use moon_error::MoonError;
use moon_utils::regex::clean_id;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{fmt, str::FromStr};
use strum::{Display, EnumIter};

#[derive(Clone, Debug, Default, Deserialize, EnumIter, Eq, JsonSchema, PartialEq, Serialize)]
#[derive(Clone, Debug, Default, EnumIter, Eq, JsonSchema, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ProjectLanguage {
Bash,
Expand All @@ -26,6 +26,26 @@ pub enum ProjectLanguage {
Other(String),
}

impl<'de> Deserialize<'de> for ProjectLanguage {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let lang = String::deserialize(deserializer)?.to_lowercase();

Ok(ProjectLanguage::from_str(&lang).unwrap())
}
}

impl Serialize for ProjectLanguage {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

impl FromStr for ProjectLanguage {
type Err = MoonError;

Expand Down Expand Up @@ -137,4 +157,52 @@ mod tests {
"dotnet"
);
}

#[test]
fn serializes_lang_to_string() {
assert_eq!(
serde_json::to_string(&ProjectLanguage::Go).unwrap(),
"\"go\""
);
assert_eq!(
serde_json::to_string(&ProjectLanguage::JavaScript).unwrap(),
"\"javascript\""
);
assert_eq!(
serde_json::to_string(&ProjectLanguage::Ruby).unwrap(),
"\"ruby\""
);
assert_eq!(
serde_json::to_string(&ProjectLanguage::Unknown).unwrap(),
"\"unknown\""
);
assert_eq!(
serde_json::to_string(&ProjectLanguage::Other("dotnet".into())).unwrap(),
"\"dotnet\""
);
}

#[test]
fn deserializes_lang_to_enum() {
assert_eq!(
serde_json::from_str::<ProjectLanguage>("\"go\"").unwrap(),
ProjectLanguage::Go,
);
assert_eq!(
serde_json::from_str::<ProjectLanguage>("\"javascript\"").unwrap(),
ProjectLanguage::JavaScript,
);
assert_eq!(
serde_json::from_str::<ProjectLanguage>("\"ruby\"").unwrap(),
ProjectLanguage::Ruby,
);
assert_eq!(
serde_json::from_str::<ProjectLanguage>("\"unknown\"").unwrap(),
ProjectLanguage::Unknown,
);
assert_eq!(
serde_json::from_str::<ProjectLanguage>("\"dotnet\"").unwrap(),
ProjectLanguage::Other("dotnet".into()),
);
}
}
6 changes: 6 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

#### 🐞 Fixes

- Fixed an issue where using a custom project language would break the project graph cache.

## 0.26.5

#### 🐞 Fixes
Expand Down

0 comments on commit 9b598b3

Please sign in to comment.