api-macro: remove now-unnecessary PropertySchema type
Schemas can now generally refer to an externally defined schema. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
3e5927a1b4
commit
79c9d6ab08
@ -133,7 +133,7 @@ impl Schema {
|
||||
}
|
||||
}
|
||||
|
||||
fn find_obj_property_by_ident(&self, key: &str) -> Option<&(FieldName, bool, PropertySchema)> {
|
||||
fn find_obj_property_by_ident(&self, key: &str) -> Option<&(FieldName, bool, Schema)> {
|
||||
self.as_object()
|
||||
.and_then(|obj| obj.find_property_by_ident(key))
|
||||
}
|
||||
@ -141,7 +141,7 @@ impl Schema {
|
||||
fn find_obj_property_by_ident_mut(
|
||||
&mut self,
|
||||
key: &str,
|
||||
) -> Option<&mut (FieldName, bool, PropertySchema)> {
|
||||
) -> Option<&mut (FieldName, bool, Schema)> {
|
||||
self.as_object_mut()
|
||||
.and_then(|obj| obj.find_property_by_ident_mut(key))
|
||||
}
|
||||
@ -307,28 +307,10 @@ impl SchemaItem {
|
||||
}
|
||||
}
|
||||
|
||||
/// A property in an object either has its own schema or references an external schema.
|
||||
enum PropertySchema {
|
||||
Schema(Schema),
|
||||
Ref(ExprPath),
|
||||
}
|
||||
|
||||
impl PropertySchema {
|
||||
fn to_schema(&self, ts: &mut TokenStream) -> Result<(), Error> {
|
||||
match self {
|
||||
PropertySchema::Schema(s) => s.to_schema(ts),
|
||||
PropertySchema::Ref(path) => {
|
||||
ts.extend(quote! { & #path });
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
/// Contains a sorted list of properties:
|
||||
struct SchemaObject {
|
||||
properties: Vec<(FieldName, bool, PropertySchema)>,
|
||||
properties: Vec<(FieldName, bool, Schema)>,
|
||||
}
|
||||
|
||||
impl SchemaObject {
|
||||
@ -353,17 +335,7 @@ impl SchemaObject {
|
||||
.transpose()?
|
||||
.unwrap_or(false);
|
||||
|
||||
let external_schema = schema
|
||||
.remove("schema")
|
||||
.map(ExprPath::try_from)
|
||||
.transpose()?;
|
||||
|
||||
let schema = match external_schema {
|
||||
Some(path) => PropertySchema::Ref(path),
|
||||
None => PropertySchema::Schema(schema.try_into()?),
|
||||
};
|
||||
|
||||
properties.push((key, optional, schema));
|
||||
properties.push((key, optional, schema.try_into()?));
|
||||
|
||||
Ok(properties)
|
||||
},
|
||||
@ -387,7 +359,7 @@ impl SchemaObject {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn find_property_by_ident(&self, key: &str) -> Option<&(FieldName, bool, PropertySchema)> {
|
||||
fn find_property_by_ident(&self, key: &str) -> Option<&(FieldName, bool, Schema)> {
|
||||
match self
|
||||
.properties
|
||||
.binary_search_by(|p| p.0.as_ident_str().cmp(key))
|
||||
@ -397,10 +369,7 @@ impl SchemaObject {
|
||||
}
|
||||
}
|
||||
|
||||
fn find_property_by_ident_mut(
|
||||
&mut self,
|
||||
key: &str,
|
||||
) -> Option<&mut (FieldName, bool, PropertySchema)> {
|
||||
fn find_property_by_ident_mut(&mut self, key: &str) -> Option<&mut (FieldName, bool, Schema)> {
|
||||
match self
|
||||
.properties
|
||||
.binary_search_by(|p| p.0.as_ident_str().cmp(key))
|
||||
|
@ -7,7 +7,7 @@ use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
use syn::Ident;
|
||||
|
||||
use super::{PropertySchema, Schema, SchemaItem};
|
||||
use super::{Schema, SchemaItem};
|
||||
use crate::util::{self, FieldName, JSONObject};
|
||||
|
||||
/// Parse `input`, `returns` and `protected` attributes out of an function annotated
|
||||
@ -140,7 +140,7 @@ enum ParameterType<'a> {
|
||||
Value,
|
||||
ApiMethod,
|
||||
RpcEnv,
|
||||
Other(&'a syn::Type, bool, &'a PropertySchema),
|
||||
Other(&'a syn::Type, bool, &'a Schema),
|
||||
}
|
||||
|
||||
fn check_input_type(input: &syn::FnArg) -> Result<(&syn::PatType, &syn::PatIdent), Error> {
|
||||
@ -181,14 +181,11 @@ fn handle_function_signature(
|
||||
let schema: &mut Schema = if let Some((_ident, _optional, schema)) =
|
||||
input_schema.find_obj_property_by_ident_mut(&pat.ident.to_string())
|
||||
{
|
||||
match schema {
|
||||
PropertySchema::Schema(schema) => match &mut schema.item {
|
||||
// ... if it has no `type` property (item = SchemaItem::Inferred), get a mutable
|
||||
// reference to the schema, so that we can...
|
||||
SchemaItem::Inferred(_span) => schema,
|
||||
// other types are fine:
|
||||
_ => continue,
|
||||
},
|
||||
match &mut schema.item {
|
||||
// ... if it has no `type` property (item = SchemaItem::Inferred), get a mutable
|
||||
// reference to the schema, so that we can...
|
||||
SchemaItem::Inferred(_span) => schema,
|
||||
// other types are fine:
|
||||
_ => continue,
|
||||
}
|
||||
} else {
|
||||
@ -248,10 +245,8 @@ fn handle_function_signature(
|
||||
let param_type = if let Some((name, optional, schema)) =
|
||||
input_schema.find_obj_property_by_ident(&pat.ident.to_string())
|
||||
{
|
||||
if let PropertySchema::Schema(schema) = schema {
|
||||
if let SchemaItem::Inferred(span) = &schema.item {
|
||||
bail!(*span, "failed to infer type");
|
||||
}
|
||||
if let SchemaItem::Inferred(span) = &schema.item {
|
||||
bail!(*span, "failed to infer type");
|
||||
}
|
||||
param_name = name.clone();
|
||||
// Found an explicit parameter: extract it:
|
||||
|
Loading…
x
Reference in New Issue
Block a user