Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

be more permissive of simple enum variants #500

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions typify-impl/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ impl TypeSpace {
// variant that we'd never use... I guess.
Schema::Bool(false) => todo!(),

// Strings must be simple enumerations.
// Strings must be simple enumerations or constants.
Schema::Object(SchemaObject {
metadata,
instance_type: Some(SingleOrVec::Single(single)),
instance_type,
format: None,
enum_values: Some(values),
const_value: None,
Expand All @@ -103,7 +103,13 @@ impl TypeSpace {
object: _,
reference: None,
extensions: _,
}) if single.as_ref() == &InstanceType::String => {
}) => {
match instance_type {
Some(SingleOrVec::Single(single))
if single.as_ref() == &InstanceType::String => {}
None => {}
_ => return None,
};
// Confirm that all values are, in fact, simple strings.
// Simple strings become simple variants. If any is not
// a string, we'll end up returning None
Expand All @@ -117,6 +123,33 @@ impl TypeSpace {
})
.collect()
}
Schema::Object(SchemaObject {
metadata,
instance_type,
format: None,
enum_values: None,
const_value: Some(value),
subschemas: None,
number: _,
string: _,
array: _,
object: _,
reference: None,
extensions: _,
}) => {
match instance_type {
Some(SingleOrVec::Single(single))
if single.as_ref() == &InstanceType::String => {}
None => {}
_ => return None,
};
std::iter::once(value.as_str().map(|variant_name| ProtoVariant::Simple {
name: variant_name,
description: metadata_description(metadata),
}))
.collect()
}

other => match get_object(other) {
// Objects must have a single property, and that
// property must be required. The type of that lone
Expand Down
20 changes: 20 additions & 0 deletions typify/tests/schemas/various-enums.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,26 @@
}
}
]
},
"commented-variants": {
"oneOf": [
{
"enum": [
"A"
],
"description": "An A"
},
{
"enum": [
"B"
],
"description": "A B"
},
{
"const": "C",
"description": "a pirate's favorite letter"
}
]
}
}
}
79 changes: 79 additions & 0 deletions typify/tests/schemas/various-enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,85 @@ impl Default for AlternativeEnum {
AlternativeEnum::Choice2
}
}
#[doc = "CommentedVariants"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"oneOf\": ["]
#[doc = " {"]
#[doc = " \"description\": \"An A\","]
#[doc = " \"enum\": ["]
#[doc = " \"A\""]
#[doc = " ]"]
#[doc = " },"]
#[doc = " {"]
#[doc = " \"description\": \"A B\","]
#[doc = " \"enum\": ["]
#[doc = " \"B\""]
#[doc = " ]"]
#[doc = " },"]
#[doc = " {"]
#[doc = " \"description\": \"a pirate's favorite letter\","]
#[doc = " \"const\": \"C\""]
#[doc = " }"]
#[doc = " ]"]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum CommentedVariants {
#[doc = "An A"]
A,
#[doc = "A B"]
B,
#[doc = "a pirate's favorite letter"]
C,
}
impl From<&CommentedVariants> for CommentedVariants {
fn from(value: &CommentedVariants) -> Self {
value.clone()
}
}
impl ToString for CommentedVariants {
fn to_string(&self) -> String {
match *self {
Self::A => "A".to_string(),
Self::B => "B".to_string(),
Self::C => "C".to_string(),
}
}
}
impl std::str::FromStr for CommentedVariants {
type Err = self::error::ConversionError;
fn from_str(value: &str) -> Result<Self, self::error::ConversionError> {
match value {
"A" => Ok(Self::A),
"B" => Ok(Self::B),
"C" => Ok(Self::C),
_ => Err("invalid value".into()),
}
}
}
impl std::convert::TryFrom<&str> for CommentedVariants {
type Error = self::error::ConversionError;
fn try_from(value: &str) -> Result<Self, self::error::ConversionError> {
value.parse()
}
}
impl std::convert::TryFrom<&String> for CommentedVariants {
type Error = self::error::ConversionError;
fn try_from(value: &String) -> Result<Self, self::error::ConversionError> {
value.parse()
}
}
impl std::convert::TryFrom<String> for CommentedVariants {
type Error = self::error::ConversionError;
fn try_from(value: String) -> Result<Self, self::error::ConversionError> {
value.parse()
}
}
#[doc = "DiskAttachment"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
Expand Down
Loading