Refactory of private functions

This commit is contained in:
Graham Esau 2021-04-15 13:34:04 +01:00
parent 5dc644000c
commit 55b860428e
8 changed files with 74 additions and 87 deletions

49
schemars/src/_private.rs Normal file
View file

@ -0,0 +1,49 @@
use crate::flatten::Merge;
use crate::gen::SchemaGenerator;
use crate::schema::{Metadata, Schema, SchemaObject};
use crate::JsonSchema;
// Helper for generating schemas for flattened `Option` fields.
pub fn json_schema_for_flatten<T: ?Sized + JsonSchema>(gen: &mut SchemaGenerator) -> Schema {
let mut schema = T::_schemars_private_non_optional_json_schema(gen);
if T::_schemars_private_is_option() {
if let Schema::Object(SchemaObject {
object: Some(ref mut object_validation),
..
}) = schema
{
object_validation.required.clear();
}
}
schema
}
// Helper for generating schemas for `Option` fields.
pub fn add_schema_as_property<T: ?Sized + JsonSchema>(
gen: &mut SchemaGenerator,
parent: &mut SchemaObject,
name: String,
metadata: Option<Metadata>,
required: bool,
) {
let mut schema = gen.subschema_for::<T>();
schema = apply_metadata(schema, metadata);
let object = parent.object();
if required && !T::_schemars_private_is_option() {
object.required.insert(name.clone());
}
object.properties.insert(name, schema);
}
pub fn apply_metadata(schema: Schema, metadata: Option<Metadata>) -> Schema {
match metadata {
None => schema,
Some(ref metadata) if *metadata == Metadata::default() => schema,
Some(metadata) => {
let mut schema_obj = schema.into_object();
schema_obj.metadata = Some(Box::new(metadata)).merge(schema_obj.metadata);
Schema::Object(schema_obj)
}
}
}

View file

@ -7,7 +7,6 @@ There are two main types in this module:two main types in this module:
* [`SchemaGenerator`], which manages the generation of a schema document.
*/
use crate::flatten::Merge;
use crate::schema::*;
use crate::{visit::*, JsonSchema, Map};
use dyn_clone::DynClone;
@ -422,22 +421,6 @@ impl SchemaGenerator {
}
}
/// This function is only public for use by schemars_derive.
///
/// It should not be considered part of the public API.
#[doc(hidden)]
pub fn apply_metadata(&self, schema: Schema, metadata: Option<Metadata>) -> Schema {
match metadata {
None => schema,
Some(ref metadata) if *metadata == Metadata::default() => schema,
Some(metadata) => {
let mut schema_obj = schema.into_object();
schema_obj.metadata = Some(Box::new(metadata)).merge(schema_obj.metadata);
Schema::Object(schema_obj)
}
}
}
fn json_schema_internal<T: ?Sized + JsonSchema>(&mut self, name: &str) -> Schema {
struct PendingSchemaState<'a> {
gen: &'a mut SchemaGenerator,

View file

@ -45,30 +45,12 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
schema
}
fn json_schema_for_flatten(gen: &mut SchemaGenerator) -> Schema {
let mut schema = T::json_schema_for_flatten(gen);
if let Schema::Object(SchemaObject {
object: Some(ref mut object_validation),
..
}) = schema
{
object_validation.required.clear();
}
schema
fn _schemars_private_non_optional_json_schema(gen: &mut SchemaGenerator) -> Schema {
T::_schemars_private_non_optional_json_schema(gen)
}
fn add_schema_as_property(
gen: &mut SchemaGenerator,
parent: &mut SchemaObject,
name: String,
metadata: Option<Metadata>,
_required: bool,
) {
let mut schema = gen.subschema_for::<Self>();
schema = gen.apply_metadata(schema, metadata);
let object = parent.object();
object.properties.insert(name, schema);
fn _schemars_private_is_option() -> bool {
true
}
}

View file

@ -21,18 +21,12 @@ macro_rules! forward_impl {
<$target>::json_schema(gen)
}
fn json_schema_for_flatten(gen: &mut SchemaGenerator) -> Schema {
<$target>::json_schema_for_flatten(gen)
fn _schemars_private_non_optional_json_schema(gen: &mut SchemaGenerator) -> Schema {
<$target>::_schemars_private_non_optional_json_schema(gen)
}
fn add_schema_as_property(
gen: &mut SchemaGenerator,
parent: &mut crate::schema::SchemaObject,
name: String,
metadata: Option<crate::schema::Metadata>,
required: bool,
) {
<$target>::add_schema_as_property(gen, parent, name, metadata, required)
fn _schemars_private_is_option() -> bool {
<$target>::_schemars_private_is_option()
}
}
};

View file

@ -301,6 +301,10 @@ mod ser;
#[macro_use]
mod macros;
/// This module is only public for use by `schemars_derive`. It should not need to be used by code
/// outside of `schemars`, and should not be considered part of the public API.
#[doc(hidden)]
pub mod _private;
pub mod gen;
pub mod schema;
pub mod visit;
@ -314,7 +318,7 @@ pub use schemars_derive::*;
#[doc(hidden)]
pub use serde_json as _serde_json;
use schema::{Schema, SchemaObject};
use schema::Schema;
/// A type which can be described as a JSON Schema document.
///
@ -357,35 +361,16 @@ pub trait JsonSchema {
/// This should not return a `$ref` schema.
fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema;
/// Helper for generating schemas for flattened `Option` fields.
///
/// This should not need to be called or implemented by code outside of `schemars`,
/// and should not be considered part of the public API.
// TODO document and bring into public API?
#[doc(hidden)]
fn json_schema_for_flatten(gen: &mut gen::SchemaGenerator) -> Schema {
fn _schemars_private_non_optional_json_schema(gen: &mut gen::SchemaGenerator) -> Schema {
Self::json_schema(gen)
}
/// Helper for generating schemas for `Option` fields.
///
/// This should not need to be called or implemented by code outside of `schemars`,
/// and should not be considered part of the public API.
// TODO document and bring into public API?
#[doc(hidden)]
fn add_schema_as_property(
gen: &mut gen::SchemaGenerator,
parent: &mut SchemaObject,
name: String,
metadata: Option<schema::Metadata>,
required: bool,
) {
let mut schema = gen.subschema_for::<Self>();
schema = gen.apply_metadata(schema, metadata);
let object = parent.object();
if required {
object.required.insert(name.clone());
}
object.properties.insert(name, schema);
fn _schemars_private_is_option() -> bool {
false
}
}

View file

@ -70,18 +70,12 @@ fn derive_json_schema(
<#ty as schemars::JsonSchema>::json_schema(gen)
}
fn json_schema_for_flatten(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<#ty as schemars::JsonSchema>::json_schema_for_flatten(gen)
fn _schemars_private_non_optional_json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(gen)
}
fn add_schema_as_property(
gen: &mut schemars::gen::SchemaGenerator,
parent: &mut schemars::schema::SchemaObject,
name: String,
metadata: Option<schemars::schema::Metadata>,
required: bool,
) {
<#ty as schemars::JsonSchema>::add_schema_as_property(gen, parent, name, metadata, required)
fn _schemars_private_is_option() -> bool {
<#ty as schemars::JsonSchema>::_schemars_private_is_option()
}
};
};

View file

@ -49,7 +49,7 @@ impl<'a> SchemaMetadata<'a> {
quote! {
{
let schema = #schema_expr;
gen.apply_metadata(schema, #self)
schemars::_private::apply_metadata(schema, #self)
}
}
}

View file

@ -433,7 +433,7 @@ fn expr_for_struct(
let args = quote!(gen, &mut schema_object, #name.to_owned(), #metadata, #required);
quote_spanned! {ty.span()=>
<#ty as schemars::JsonSchema>::add_schema_as_property(#args);
schemars::_private::add_schema_as_property::<#ty>(#args);
}
})
.collect();
@ -448,7 +448,7 @@ fn expr_for_struct(
let gen = quote!(gen);
quote_spanned! {ty.span()=>
.flatten(<#ty as schemars::JsonSchema>::json_schema_for_flatten(#gen))
.flatten(schemars::_private::json_schema_for_flatten::<#ty>(#gen))
}
})
.collect();