Skip to content

Commit

Permalink
refactor(deserialize): allow setting name for root deserialized value
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jan 8, 2024
1 parent cc3a756 commit e744f99
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 66 deletions.
170 changes: 115 additions & 55 deletions crates/biome_deserialize/src/json.rs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions crates/biome_deserialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ pub use string_set::StringSet;
/// use biome_json_parser::JsonParserOptions;
///
/// let source = r#""A""#;
/// let deserialized = deserialize_from_json_str::<Variant>(&source, JsonParserOptions::default());
/// let deserialized = deserialize_from_json_str::<Variant>(&source, JsonParserOptions::default(), "");
/// assert!(!deserialized.has_errors());
/// assert!(matches!(deserialized.into_deserialized(), Some(Variant::A)));
/// ```
pub trait Deserializable: Sized {
/// Returns the deserialized form of `value`, or `None` if it failed.
/// Any diagnostics emitted during deserialization are appended to `diagnostics`.
/// `name` corresponds to the name used in a diagnostic to designate the value.
/// `name` corresponds to the name used in a diagnostic to designate the deserialized value.
fn deserialize(
value: &impl DeserializableValue,
name: &str,
Expand Down Expand Up @@ -197,7 +197,7 @@ pub trait DeserializableValue: Sized {
/// use biome_json_parser::JsonParserOptions;
///
/// let source = r#"{ "name": "Isaac Asimov" }"#;
/// let deserialized = deserialize_from_json_str::<Person>(&source, JsonParserOptions::default());
/// let deserialized = deserialize_from_json_str::<Person>(&source, JsonParserOptions::default(), "");
/// assert!(!deserialized.has_errors());
/// assert_eq!(deserialized.into_deserialized(), Some(Person { name: "Isaac Asimov".to_string() }));
/// ```
Expand Down Expand Up @@ -252,12 +252,12 @@ pub trait DeserializableValue: Sized {
/// use biome_json_parser::JsonParserOptions;
///
/// let source = r#" "string" "#;
/// let deserialized = deserialize_from_json_str::<Union>(&source, JsonParserOptions::default());
/// let deserialized = deserialize_from_json_str::<Union>(&source, JsonParserOptions::default(), "");
/// assert!(!deserialized.has_errors());
/// assert_eq!(deserialized.into_deserialized(), Some(Union::Str("string".to_string())));
///
/// let source = "true";
/// let deserialized = deserialize_from_json_str::<Union>(&source, JsonParserOptions::default());
/// let deserialized = deserialize_from_json_str::<Union>(&source, JsonParserOptions::default(), "");
/// assert!(!deserialized.has_errors());
/// assert_eq!(deserialized.into_deserialized(), Some(Union::Bool(true)));
/// ```
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_project/src/node_js_project/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Manifest for PackageJson {
type Language = JsonLanguage;

fn deserialize_manifest(root: &LanguageRoot<Self::Language>) -> Deserialized<Self> {
deserialize_from_json_ast::<PackageJson>(root)
deserialize_from_json_ast::<PackageJson>(root, "")
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/biome_service/src/configuration/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ mod test {
fn deserialization_error() {
let content = "{ \n\n\"formatter\" }";
let result =
deserialize_from_json_str::<Configuration>(content, JsonParserOptions::default());
deserialize_from_json_str::<Configuration>(content, JsonParserOptions::default(), "");

assert!(result.has_errors());
for diagnostic in result.into_diagnostics() {
Expand All @@ -343,7 +343,7 @@ mod test {
}
}"#;
let _result =
deserialize_from_json_str::<Configuration>(content, JsonParserOptions::default())
deserialize_from_json_str::<Configuration>(content, JsonParserOptions::default(), "")
.into_deserialized()
.unwrap_or_default();
}
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_service/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ fn load_config(
file_path,
} = auto_search_result;
let deserialized =
deserialize_from_json_str::<Configuration>(&content, JsonParserOptions::default());
deserialize_from_json_str::<Configuration>(&content, JsonParserOptions::default(), "");
Ok(Some(ConfigurationPayload {
deserialized,
configuration_file_path: file_path,
Expand Down Expand Up @@ -803,6 +803,7 @@ impl LoadedConfiguration {
let deserialized = deserialize_from_json_str::<Configuration>(
content.as_str(),
JsonParserOptions::default(),
"",
);
deserialized_configurations.push(deserialized)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_service/src/file_handlers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn lint(params: LintParams) -> LintResults {
// if we're parsing the `biome.json` file, we deserialize it, so we can emit diagnostics for
// malformed configuration
if params.path.ends_with(ROME_JSON) || params.path.ends_with(BIOME_JSON) {
let deserialized = deserialize_from_json_ast::<Configuration>(&root);
let deserialized = deserialize_from_json_ast::<Configuration>(&root, "");
diagnostics.extend(
deserialized
.into_diagnostics()
Expand Down
7 changes: 6 additions & 1 deletion crates/biome_service/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ fn run_invalid_configurations(input: &'static str, _: &str, _: &str, _: &str) {
"json" => deserialize_from_json_str::<Configuration>(
input_code.as_str(),
JsonParserOptions::default(),
"",
),
"jsonc" => deserialize_from_json_str::<Configuration>(
input_code.as_str(),
JsonParserOptions::default()
.with_allow_comments()
.with_allow_trailing_commas(),
"",
),
_ => {
panic!("Extension not supported");
Expand Down Expand Up @@ -69,10 +71,12 @@ fn run_valid_configurations(input: &'static str, _: &str, _: &str, _: &str) {
"json" => deserialize_from_json_str::<Configuration>(
input_code.as_str(),
JsonParserOptions::default(),
"",
),
"jsonc" => deserialize_from_json_str::<Configuration>(
input_code.as_str(),
JsonParserOptions::default().with_allow_comments(),
"",
),
_ => {
panic!("Extension not supported");
Expand Down Expand Up @@ -116,7 +120,8 @@ fn quick_test() {
}
}
}"#;
let result = deserialize_from_json_str::<Configuration>(source, JsonParserOptions::default());
let result =
deserialize_from_json_str::<Configuration>(source, JsonParserOptions::default(), "");

dbg!(result.diagnostics());
assert!(!result.has_errors());
Expand Down
1 change: 1 addition & 0 deletions crates/biome_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn create_analyzer_options(
let deserialized = biome_deserialize::json::deserialize_from_json_str::<Configuration>(
json.as_str(),
JsonParserOptions::default(),
"",
);
if deserialized.has_errors() {
diagnostics.extend(
Expand Down

0 comments on commit e744f99

Please sign in to comment.