Skip to content

Commit 0119127

Browse files
(GH-538) Return Schema for subschema extension methods
This change: - Updates the following functions to return instances of `schemars::Schema` instead of `Map<String, Value>`, since the returned data is _always_ a subschema, if it exists: - `get_defs_subschema_from_id()` - `get_defs_subschema_from_id_mut()` - `get_defs_subschema_from_reference()` - `get_defs_subschema_from_reference_mut()` - `get_property_subschema()` - `get_property_subschema_mut()` - Removes the type aliases `Object` (for `Map<String, Value>`) and `Array` (for `Vec<Value>`), as these conveniences weren't saving much typing and Rust Analyzer wasn't always plumbing them through for IntelliSense. The uses of these aliases now revert to calling the underlying types. - Updates documentation and tests for the modified functions.
1 parent befe473 commit 0119127

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

lib/dsc-lib-jsonschema/src/schema_utility_extensions.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub trait SchemaUtilityExtensions {
344344
/// None
345345
/// )
346346
/// ```
347-
fn get_keyword_as_object(&self, key: &str) -> Option<&Map<String, Value>>;
347+
fn get_keyword_as_object(&self, key: &str) -> Option<&Object>;
348348
/// Checks a JSON Schema for a given keyword and mutably borrows the value of that keyword,
349349
/// if it exists, as a [`Map`].
350350
///
@@ -395,7 +395,7 @@ pub trait SchemaUtilityExtensions {
395395
/// None
396396
/// )
397397
/// ```
398-
fn get_keyword_as_object_mut(&mut self, key: &str) -> Option<&mut Map<String, Value>>;
398+
fn get_keyword_as_object_mut(&mut self, key: &str) -> Option<&mut Object>;
399399
/// Checks a JSON schema for a given keyword and borrows the value of that keyword, if it
400400
/// exists, as a [`Number`].
401401
///
@@ -800,7 +800,7 @@ pub trait SchemaUtilityExtensions {
800800
/// defs_json.as_object()
801801
/// );
802802
/// ```
803-
fn get_defs(&self) -> Option<&Map<String, Value>>;
803+
fn get_defs(&self) -> Option<&Object>;
804804
/// Retrieves the `$defs` keyword and mutably borrows the object if it exists.
805805
///
806806
/// If the keyword isn't defined or isn't an object, the function returns [`None`].
@@ -828,9 +828,9 @@ pub trait SchemaUtilityExtensions {
828828
/// defs_json.as_object_mut()
829829
/// );
830830
/// ```
831-
fn get_defs_mut(&mut self) -> Option<&mut Map<String, Value>>;
832-
/// Looks up a reference in the `$defs` keyword by `$id` and returns the subschema entry as a
833-
/// [`Schema`] if it exists.
831+
fn get_defs_mut(&mut self) -> Option<&mut Object>;
832+
/// Looks up a reference in the `$defs` keyword by `$id` and returns the subschema entry as an
833+
/// object if it exists.
834834
///
835835
/// The value for the `id` _must_ be the absolute URL of the target subschema's `$id` keyword.
836836
/// If the target subschema doesn't define the `$id` keyword, this function can't resolve the
@@ -1107,7 +1107,7 @@ pub trait SchemaUtilityExtensions {
11071107
/// Some(&mut new_definition)
11081108
/// )
11091109
/// ```
1110-
fn insert_defs_subschema(&mut self, definition_key: &str, definition_value: &Map<String, Value>) -> Option< Map<String, Value>>;
1110+
fn insert_defs_subschema(&mut self, definition_key: &str, definition_value: &Object) -> Option<Object>;
11111111
/// Looks up a subschema in the `$defs` keyword by reference and, if it exists, renames the
11121112
/// _key_ for the definition.
11131113
///
@@ -1177,7 +1177,7 @@ pub trait SchemaUtilityExtensions {
11771177
/// properties_json.as_object()
11781178
/// );
11791179
/// ```
1180-
fn get_properties(&self) -> Option<&Map<String, Value>>;
1180+
fn get_properties(&self) -> Option<&Object>;
11811181
/// Retrieves the `properties` keyword and mutably borrows the object if it exists.
11821182
///
11831183
/// If the keyword isn't defined or isn't an object, the function returns [`None`].
@@ -1205,7 +1205,7 @@ pub trait SchemaUtilityExtensions {
12051205
/// properties_json.as_object_mut()
12061206
/// );
12071207
/// ```
1208-
fn get_properties_mut(&mut self) -> Option<&mut Map<String, Value>>;
1208+
fn get_properties_mut(&mut self) -> Option<&mut Object>;
12091209
/// Looks up a property in the `properties` keyword by name and returns the subschema entry as
12101210
/// a [`Schema`] if it exists.
12111211
///
@@ -1292,11 +1292,11 @@ impl SchemaUtilityExtensions for Schema {
12921292
self.get(key)
12931293
.and_then(Value::as_number)
12941294
}
1295-
fn get_keyword_as_object(&self, key: &str) -> Option<&Map<String, Value>> {
1295+
fn get_keyword_as_object(&self, key: &str) -> Option<&Object> {
12961296
self.get(key)
12971297
.and_then(Value::as_object)
12981298
}
1299-
fn get_keyword_as_object_mut(&mut self, key: &str) -> Option<&mut Map<String, Value>> {
1299+
fn get_keyword_as_object_mut(&mut self, key: &str) -> Option<&mut Object> {
13001300
self.get_mut(key)
13011301
.and_then(Value::as_object_mut)
13021302
}
@@ -1321,10 +1321,10 @@ impl SchemaUtilityExtensions for Schema {
13211321
self.get(key)
13221322
.and_then(Value::as_u64)
13231323
}
1324-
fn get_defs(&self) -> Option<&Map<String, Value>> {
1324+
fn get_defs(&self) -> Option<&Object> {
13251325
self.get_keyword_as_object("$defs")
13261326
}
1327-
fn get_defs_mut(&mut self) -> Option<&mut Map<String, Value>> {
1327+
fn get_defs_mut(&mut self) -> Option<&mut Object> {
13281328
self.get_keyword_as_object_mut("$defs")
13291329
}
13301330
fn get_defs_subschema_from_id(&self, id: &str) -> Option<&Schema> {
@@ -1468,10 +1468,10 @@ impl SchemaUtilityExtensions for Schema {
14681468
self.insert("$id".to_string(), Value::String(id_uri.to_string()))
14691469
.and(old_id)
14701470
}
1471-
fn get_properties(&self) -> Option<&Map<String, Value>> {
1471+
fn get_properties(&self) -> Option<&Object> {
14721472
self.get_keyword_as_object("properties")
14731473
}
1474-
fn get_properties_mut(&mut self) -> Option<&mut Map<String, Value>> {
1474+
fn get_properties_mut(&mut self) -> Option<&mut Object> {
14751475
self.get_keyword_as_object_mut("properties")
14761476
}
14771477
fn get_property_subschema(&self, property_name: &str) -> Option<&Schema> {

0 commit comments

Comments
 (0)