Cleanup, init resource
This commit is contained in:
parent
2f082b63e9
commit
42ae72256c
@ -24,7 +24,10 @@ use serde_json::{Map, Value};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error_codes,
|
error_codes,
|
||||||
schemas::{json_schema::{export_type, JsonSchemaBevyType}, open_rpc::OpenRpcDocument},
|
schemas::{
|
||||||
|
json_schema::{export_type, JsonSchemaBevyType},
|
||||||
|
open_rpc::OpenRpcDocument,
|
||||||
|
},
|
||||||
BrpError, BrpResult,
|
BrpError, BrpResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1229,17 +1232,6 @@ pub fn export_registry_types(In(params): In<Option<Value>>, world: &World) -> Br
|
|||||||
let schemas = types
|
let schemas = types
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|type_reg| {
|
.filter(|type_reg| {
|
||||||
// EXTRA FILTER, not decided yet if gonna make it in the end
|
|
||||||
match type_reg.type_info() {
|
|
||||||
bevy_reflect::TypeInfo::Tuple(_) => return false,
|
|
||||||
bevy_reflect::TypeInfo::TupleStruct(_) => return false,
|
|
||||||
bevy_reflect::TypeInfo::List(_) => return false,
|
|
||||||
bevy_reflect::TypeInfo::Array(_) => return false,
|
|
||||||
bevy_reflect::TypeInfo::Map(_) => return false,
|
|
||||||
bevy_reflect::TypeInfo::Set(_) => return false,
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let path_table = type_reg.type_info().type_path_table();
|
let path_table = type_reg.type_info().type_path_table();
|
||||||
if let Some(crate_name) = &path_table.crate_name() {
|
if let Some(crate_name) = &path_table.crate_name() {
|
||||||
if !filter.with_crates.is_empty()
|
if !filter.with_crates.is_empty()
|
||||||
|
|||||||
@ -539,6 +539,7 @@ impl Plugin for RemotePlugin {
|
|||||||
.insert_after(Last, RemoteLast);
|
.insert_after(Last, RemoteLast);
|
||||||
|
|
||||||
app.insert_resource(remote_methods)
|
app.insert_resource(remote_methods)
|
||||||
|
.init_resource::<schemas::SchemaTypesMetadata>()
|
||||||
.init_resource::<RemoteWatchingRequests>()
|
.init_resource::<RemoteWatchingRequests>()
|
||||||
.add_systems(PreStartup, setup_mailbox_channel)
|
.add_systems(PreStartup, setup_mailbox_channel)
|
||||||
.configure_sets(
|
.configure_sets(
|
||||||
|
|||||||
@ -2,7 +2,8 @@
|
|||||||
//! It tries to follow this standard: <https://json-schema.org/specification>
|
//! It tries to follow this standard: <https://json-schema.org/specification>
|
||||||
use bevy_platform::collections::HashMap;
|
use bevy_platform::collections::HashMap;
|
||||||
use bevy_reflect::{
|
use bevy_reflect::{
|
||||||
GetTypeRegistration, NamedField, OpaqueInfo, TypeInfo, TypeRegistration, TypeRegistry, VariantInfo
|
GetTypeRegistration, NamedField, OpaqueInfo, TypeInfo, TypeRegistration, TypeRegistry,
|
||||||
|
VariantInfo,
|
||||||
};
|
};
|
||||||
use core::any::TypeId;
|
use core::any::TypeId;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -10,19 +11,16 @@ use serde_json::{json, Map, Value};
|
|||||||
|
|
||||||
use crate::schemas::SchemaTypesMetadata;
|
use crate::schemas::SchemaTypesMetadata;
|
||||||
|
|
||||||
|
|
||||||
/// Helper trait for converting TypeRegistration to JsonSchemaBevyType
|
/// Helper trait for converting TypeRegistration to JsonSchemaBevyType
|
||||||
pub trait TypeRegistrySchemaReader {
|
pub trait TypeRegistrySchemaReader {
|
||||||
/// Export type JSON Schema with definitions.
|
/// Export type JSON Schema.
|
||||||
/// It can be useful for generating schemas for assets validation.
|
|
||||||
fn export_type_json_schema<T: GetTypeRegistration>(
|
fn export_type_json_schema<T: GetTypeRegistration>(
|
||||||
&self,
|
&self,
|
||||||
extra_info: &SchemaTypesMetadata,
|
extra_info: &SchemaTypesMetadata,
|
||||||
) -> Option<JsonSchemaBevyType> {
|
) -> Option<JsonSchemaBevyType> {
|
||||||
self.export_type_json_schema_for_id(extra_info, T::get_type_registration().type_id())
|
self.export_type_json_schema_for_id(extra_info, T::get_type_registration().type_id())
|
||||||
}
|
}
|
||||||
/// Export type JSON Schema with definitions.
|
/// Export type JSON Schema.
|
||||||
/// It can be useful for generating schemas for assets validation.
|
|
||||||
fn export_type_json_schema_for_id(
|
fn export_type_json_schema_for_id(
|
||||||
&self,
|
&self,
|
||||||
extra_info: &SchemaTypesMetadata,
|
extra_info: &SchemaTypesMetadata,
|
||||||
@ -37,13 +35,19 @@ impl TypeRegistrySchemaReader for TypeRegistry {
|
|||||||
type_id: TypeId,
|
type_id: TypeId,
|
||||||
) -> Option<JsonSchemaBevyType> {
|
) -> Option<JsonSchemaBevyType> {
|
||||||
let type_reg = self.get(type_id)?;
|
let type_reg = self.get(type_id)?;
|
||||||
Some((type_reg,extra_info).into())
|
Some((type_reg, extra_info).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Exports schema info for a given type
|
/// Exports schema info for a given type
|
||||||
pub fn export_type(reg: &TypeRegistration, metadata: &SchemaTypesMetadata) -> (String, JsonSchemaBevyType) {
|
pub fn export_type(
|
||||||
(reg.type_info().type_path().to_owned(), (reg,metadata).into())
|
reg: &TypeRegistration,
|
||||||
|
metadata: &SchemaTypesMetadata,
|
||||||
|
) -> (String, JsonSchemaBevyType) {
|
||||||
|
(
|
||||||
|
reg.type_info().type_path().to_owned(),
|
||||||
|
(reg, metadata).into(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<(&TypeRegistration, &SchemaTypesMetadata)> for JsonSchemaBevyType {
|
impl From<(&TypeRegistration, &SchemaTypesMetadata)> for JsonSchemaBevyType {
|
||||||
@ -365,11 +369,11 @@ impl SchemaJsonReference for &NamedField {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use bevy_ecs::prelude::ReflectComponent;
|
||||||
use bevy_ecs::prelude::ReflectResource;
|
use bevy_ecs::prelude::ReflectResource;
|
||||||
use bevy_ecs::{component::Component, reflect::AppTypeRegistry, resource::Resource};
|
use bevy_ecs::{component::Component, reflect::AppTypeRegistry, resource::Resource};
|
||||||
use bevy_reflect::prelude::ReflectDefault;
|
use bevy_reflect::prelude::ReflectDefault;
|
||||||
use bevy_ecs::prelude::ReflectComponent;
|
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
|
||||||
use bevy_reflect::{Reflect,ReflectSerialize, ReflectDeserialize};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn reflect_export_struct() {
|
fn reflect_export_struct() {
|
||||||
@ -435,7 +439,7 @@ mod tests {
|
|||||||
.get(TypeId::of::<EnumComponent>())
|
.get(TypeId::of::<EnumComponent>())
|
||||||
.expect("SHOULD BE REGISTERED")
|
.expect("SHOULD BE REGISTERED")
|
||||||
.clone();
|
.clone();
|
||||||
let (_, schema) = export_type(&foo_registration,&SchemaTypesMetadata::default());
|
let (_, schema) = export_type(&foo_registration, &SchemaTypesMetadata::default());
|
||||||
assert!(
|
assert!(
|
||||||
schema.reflect_types.contains(&"Component".to_owned()),
|
schema.reflect_types.contains(&"Component".to_owned()),
|
||||||
"Should be a component"
|
"Should be a component"
|
||||||
@ -470,7 +474,7 @@ mod tests {
|
|||||||
.get(TypeId::of::<EnumComponent>())
|
.get(TypeId::of::<EnumComponent>())
|
||||||
.expect("SHOULD BE REGISTERED")
|
.expect("SHOULD BE REGISTERED")
|
||||||
.clone();
|
.clone();
|
||||||
let (_, schema) = export_type(&foo_registration,&SchemaTypesMetadata::default());
|
let (_, schema) = export_type(&foo_registration, &SchemaTypesMetadata::default());
|
||||||
assert!(
|
assert!(
|
||||||
!schema.reflect_types.contains(&"Component".to_owned()),
|
!schema.reflect_types.contains(&"Component".to_owned()),
|
||||||
"Should not be a component"
|
"Should not be a component"
|
||||||
@ -558,12 +562,12 @@ mod tests {
|
|||||||
assert_normalized_values(schema_as_value, value);
|
assert_normalized_values(schema_as_value, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assert_normalized_values(one: Value, two: Value){
|
fn assert_normalized_values(one: Value, two: Value) {
|
||||||
let mut one = one.clone();
|
let mut one = one.clone();
|
||||||
normalize_json(&mut one);
|
normalize_json(&mut one);
|
||||||
let mut two = two.clone();
|
let mut two = two.clone();
|
||||||
normalize_json(&mut two);
|
normalize_json(&mut two);
|
||||||
assert_eq!(one,two);
|
assert_eq!(one, two);
|
||||||
|
|
||||||
/// Recursively sorts arrays in a serde_json::Value
|
/// Recursively sorts arrays in a serde_json::Value
|
||||||
fn normalize_json(value: &mut Value) {
|
fn normalize_json(value: &mut Value) {
|
||||||
|
|||||||
@ -1,17 +1,22 @@
|
|||||||
//! Module with schemas used for various BRP endpoints
|
//! Module with schemas used for various BRP endpoints
|
||||||
|
|
||||||
use std::any::TypeId;
|
|
||||||
use bevy_asset::{ReflectAsset, ReflectHandle};
|
use bevy_asset::{ReflectAsset, ReflectHandle};
|
||||||
use bevy_ecs::{reflect::{ReflectComponent, ReflectResource}, resource::Resource};
|
use bevy_ecs::{
|
||||||
|
reflect::{ReflectComponent, ReflectResource},
|
||||||
|
resource::Resource,
|
||||||
|
};
|
||||||
use bevy_platform::collections::HashMap;
|
use bevy_platform::collections::HashMap;
|
||||||
use bevy_reflect::{prelude::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize, TypeData, TypeRegistration};
|
use bevy_reflect::{
|
||||||
|
prelude::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize, TypeData,
|
||||||
|
TypeRegistration,
|
||||||
|
};
|
||||||
|
use std::any::TypeId;
|
||||||
|
|
||||||
pub mod json_schema;
|
pub mod json_schema;
|
||||||
pub mod open_rpc;
|
pub mod open_rpc;
|
||||||
|
|
||||||
|
/// Holds mapping of reflect data types to strings,
|
||||||
/// Holds mapping of reflect data types to strings,
|
/// later on used in Bevy Json Schema.
|
||||||
/// later on used in Bevy Json Schema.
|
|
||||||
#[derive(Debug, Resource, Reflect)]
|
#[derive(Debug, Resource, Reflect)]
|
||||||
#[reflect(Resource)]
|
#[reflect(Resource)]
|
||||||
pub struct SchemaTypesMetadata {
|
pub struct SchemaTypesMetadata {
|
||||||
@ -48,4 +53,4 @@ impl SchemaTypesMetadata {
|
|||||||
.flat_map(|(id, name)| reg.data_by_id(*id).and(Some(name.clone())))
|
.flat_map(|(id, name)| reg.data_by_id(*id).and(Some(name.clone())))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user