Internally tagged enums
This commit is contained in:
parent
b0ae29094e
commit
a3076dde63
5 changed files with 110 additions and 213 deletions
|
@ -1,12 +1,12 @@
|
||||||
mod util;
|
mod util;
|
||||||
use schemars::MakeSchema;
|
use schemars::{MakeSchema, Map};
|
||||||
use util::*;
|
use util::*;
|
||||||
|
|
||||||
#[derive(Debug, MakeSchema)]
|
#[derive(Debug, MakeSchema)]
|
||||||
#[schemars(rename_all = "camelCase")]
|
#[schemars(rename_all = "camelCase")]
|
||||||
pub enum External {
|
pub enum External {
|
||||||
UnitOne,
|
UnitOne,
|
||||||
String(String),
|
StringMap(Map<String, String>),
|
||||||
Struct{ foo: i32, bar: bool },
|
Struct{ foo: i32, bar: bool },
|
||||||
UnitTwo,
|
UnitTwo,
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,12 @@ pub enum External {
|
||||||
fn enum_external_tag() -> TestResult {
|
fn enum_external_tag() -> TestResult {
|
||||||
test_default_generated_schema::<External>("enum-external")
|
test_default_generated_schema::<External>("enum-external")
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
#[derive(Debug, MakeSchema)]
|
#[derive(Debug, MakeSchema)]
|
||||||
#[schemars(tag = "typeProperty")]
|
#[schemars(tag = "typeProperty")]
|
||||||
pub enum Internal {
|
pub enum Internal {
|
||||||
UnitOne,
|
UnitOne,
|
||||||
String(String),
|
StringMap(Map<String, String>),
|
||||||
Struct{ foo: i32, bar: bool },
|
Struct{ foo: i32, bar: bool },
|
||||||
UnitTwo,
|
UnitTwo,
|
||||||
}
|
}
|
||||||
|
@ -29,12 +29,12 @@ pub enum Internal {
|
||||||
fn enum_internal_tag() -> TestResult {
|
fn enum_internal_tag() -> TestResult {
|
||||||
test_default_generated_schema::<Internal>("enum-internal")
|
test_default_generated_schema::<Internal>("enum-internal")
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
#[derive(Debug, MakeSchema)]
|
#[derive(Debug, MakeSchema)]
|
||||||
#[schemars(untagged)]
|
#[schemars(untagged)]
|
||||||
pub enum Untagged {
|
pub enum Untagged {
|
||||||
UnitOne,
|
UnitOne,
|
||||||
String(String),
|
StringMap(Map<String, String>),
|
||||||
Struct{ foo: i32, bar: bool }
|
Struct{ foo: i32, bar: bool }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,15 @@
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"string": {
|
"stringMap": {
|
||||||
"type": "string"
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
"string"
|
"stringMap"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,222 +1,73 @@
|
||||||
{
|
{
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"title": "Schema",
|
"title": "Internal",
|
||||||
"anyOf": [
|
"anyOf": [
|
||||||
{
|
{
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/Ref"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/SchemaObject"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"definitions": {
|
|
||||||
"InstanceType": {
|
|
||||||
"enum": [
|
|
||||||
"null",
|
|
||||||
"boolean",
|
|
||||||
"object",
|
|
||||||
"array",
|
|
||||||
"number",
|
|
||||||
"string",
|
|
||||||
"integer"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"Schema": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/Ref"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/SchemaObject"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"SchemaObject": {
|
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"$id": {
|
"typeProperty": {
|
||||||
"anyOf": [
|
"type": "string",
|
||||||
{
|
"enum": [
|
||||||
"type": "string"
|
"UnitOne"
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
},
|
|
||||||
"$schema": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"allOf": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"$ref": "#/definitions/Schema"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"anyOf": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"$ref": "#/definitions/Schema"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"definitions": {
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": {
|
|
||||||
"$ref": "#/definitions/Schema"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"description": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"enum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "array",
|
|
||||||
"items": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"items": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/SingleOrVec_For_Schema"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"not": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/Schema"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"oneOf": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"$ref": "#/definitions/Schema"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"properties": {
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": {
|
|
||||||
"$ref": "#/definitions/Schema"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": {
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"title": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"type": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/SingleOrVec_For_InstanceType"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"additionalProperties": true
|
|
||||||
},
|
|
||||||
"Ref": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"$ref": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": [
|
||||||
"$ref"
|
"typeProperty"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"SingleOrVec_For_InstanceType": {
|
{
|
||||||
"anyOf": [
|
"type": "object",
|
||||||
{
|
"properties": {
|
||||||
"$ref": "#/definitions/InstanceType"
|
"typeProperty": {
|
||||||
},
|
"type": "string",
|
||||||
{
|
"enum": [
|
||||||
"type": "array",
|
"StringMap"
|
||||||
"items": {
|
]
|
||||||
"$ref": "#/definitions/InstanceType"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"typeProperty"
|
||||||
|
],
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"typeProperty": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"Struct"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"bar": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"foo": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"bar",
|
||||||
|
"foo",
|
||||||
|
"typeProperty"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"SingleOrVec_For_Schema": {
|
{
|
||||||
"anyOf": [
|
"type": "object",
|
||||||
{
|
"properties": {
|
||||||
"$ref": "#/definitions/Schema"
|
"typeProperty": {
|
||||||
},
|
"type": "string",
|
||||||
{
|
"enum": [
|
||||||
"type": "array",
|
"UnitTwo"
|
||||||
"items": {
|
]
|
||||||
"$ref": "#/definitions/Schema"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"typeProperty"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
]
|
||||||
}
|
}
|
|
@ -6,7 +6,10 @@
|
||||||
"type": "null"
|
"type": "null"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "string"
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|
|
@ -104,6 +104,7 @@ fn schema_for_enum(variants: &[Variant], cattrs: &attr::Container) -> TokenStrea
|
||||||
match cattrs.tag() {
|
match cattrs.tag() {
|
||||||
EnumTag::External => schema_for_external_tagged_enum(variants, cattrs),
|
EnumTag::External => schema_for_external_tagged_enum(variants, cattrs),
|
||||||
EnumTag::None => schema_for_untagged_enum(variants, cattrs),
|
EnumTag::None => schema_for_untagged_enum(variants, cattrs),
|
||||||
|
EnumTag::Internal { tag } => schema_for_internal_tagged_enum(variants, cattrs, tag),
|
||||||
_ => unimplemented!("Adjacent/internal tagged enums not yet supported."),
|
_ => unimplemented!("Adjacent/internal tagged enums not yet supported."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,6 +153,45 @@ fn schema_for_external_tagged_enum(variants: &[Variant], cattrs: &attr::Containe
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn schema_for_internal_tagged_enum(
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
tag_name: &str,
|
||||||
|
) -> TokenStream {
|
||||||
|
let schemas = variants.into_iter().map(|variant| {
|
||||||
|
let name = variant.attrs.name().deserialize_name();
|
||||||
|
let type_schema = wrap_schema_fields(quote! {
|
||||||
|
instance_type: Some(schemars::schema::InstanceType::String.into()),
|
||||||
|
enum_values: Some(vec![#name.into()]),
|
||||||
|
});
|
||||||
|
let schema = wrap_schema_fields(quote! {
|
||||||
|
instance_type: Some(schemars::schema::InstanceType::Object.into()),
|
||||||
|
properties: {
|
||||||
|
let mut props = schemars::Map::new();
|
||||||
|
props.insert(#tag_name.to_owned(), #type_schema);
|
||||||
|
props
|
||||||
|
},
|
||||||
|
required: {
|
||||||
|
let mut required = schemars::Set::new();
|
||||||
|
required.insert(#tag_name.to_owned());
|
||||||
|
required
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if is_unit_variant(&variant) {
|
||||||
|
schema
|
||||||
|
} else {
|
||||||
|
let sub_schema = schema_for_untagged_enum_variant(variant, cattrs);
|
||||||
|
quote! {
|
||||||
|
#schema.flatten(#sub_schema)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
wrap_schema_fields(quote! {
|
||||||
|
any_of: Some(vec![#(#schemas),*]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn schema_for_untagged_enum(variants: &[Variant], cattrs: &attr::Container) -> TokenStream {
|
fn schema_for_untagged_enum(variants: &[Variant], cattrs: &attr::Container) -> TokenStream {
|
||||||
let schemas = variants
|
let schemas = variants
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue