-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Derived Type, ParseFromJSON and IntoJSON for prost_wkt_types Struct a…
…nd Value (#689) Co-authored-by: Sunli <[email protected]>
- Loading branch information
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ mod url; | |
#[cfg(feature = "uuid")] | ||
mod uuid; | ||
mod vec; | ||
#[cfg(feature = "prost-wkt-types")] | ||
mod prost_wkt_types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod r#struct; | ||
mod value; |
111 changes: 111 additions & 0 deletions
111
poem-openapi/src/types/external/prost_wkt_types/struct.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use std::borrow::Cow; | ||
|
||
use prost_wkt_types::Value; | ||
|
||
use crate::registry::{MetaSchema, MetaSchemaRef}; | ||
use crate::types::{ParseError, ParseFromJSON, ParseResult, ToJSON, Type}; | ||
|
||
impl Type for prost_wkt_types::Struct { | ||
const IS_REQUIRED: bool = true; | ||
|
||
type RawValueType = Self; | ||
|
||
type RawElementValueType = <Value as Type>::RawValueType; | ||
|
||
fn name() -> Cow<'static, str> { | ||
"Protobuf Struct".into() | ||
} | ||
|
||
fn schema_ref() -> MetaSchemaRef { | ||
MetaSchemaRef::Inline(Box::new(MetaSchema { | ||
additional_properties: Some(Box::new(Value::schema_ref())), | ||
..MetaSchema::new("object") | ||
})) | ||
} | ||
|
||
fn as_raw_value(&self) -> Option<&Self::RawValueType> { | ||
Some(self) | ||
} | ||
|
||
fn raw_element_iter<'a>( | ||
&'a self, | ||
) -> Box<dyn Iterator<Item=&'a Self::RawElementValueType> + 'a> { | ||
self.fields.raw_element_iter() | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.fields.is_empty() | ||
} | ||
} | ||
|
||
impl ParseFromJSON for prost_wkt_types::Struct { | ||
fn parse_from_json(value: Option<serde_json::Value>) -> ParseResult<Self> { | ||
let value = value.unwrap_or_default(); | ||
if let serde_json::Value::Object(_) = &value { | ||
serde_json::from_value::<prost_wkt_types::Struct>(value).map_err(|e| ParseError::custom(e.to_string())) | ||
} else { | ||
Err(ParseError::expected_type(value)) | ||
} | ||
} | ||
} | ||
|
||
impl ToJSON for prost_wkt_types::Struct { | ||
fn to_json(&self) -> Option<serde_json::Value> { | ||
serde_json::to_value(self).ok() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
||
use prost_wkt_types::Value; | ||
use serde_json::json; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn parse_from_parameters() { | ||
let prost_struct = prost_wkt_types::Struct::parse_from_json(Some(json!( | ||
{ | ||
"f1":10_f64, | ||
"f2":"Hi", | ||
"f3":true, | ||
"f4":null, | ||
"f5": {"fa": "Hello"}, | ||
"f6": [1,2,3] | ||
} | ||
))).unwrap(); | ||
|
||
assert_eq!( | ||
prost_struct.fields.get("f1").unwrap(), | ||
&Value::number(10_f64) | ||
); | ||
assert_eq!( | ||
prost_struct.fields.get("f2").unwrap(), | ||
&Value::string("Hi".to_string()) | ||
); | ||
assert_eq!( | ||
prost_struct.fields.get("f3").unwrap(), | ||
&Value::bool(true) | ||
); | ||
assert_eq!( | ||
prost_struct.fields.get("f4").unwrap(), | ||
&Value::null() | ||
); | ||
assert_eq!( | ||
prost_struct.fields.get("f5").unwrap(), | ||
&Value::pb_struct( | ||
HashMap::from([("fa".into(), Value::string("Hello".into()))]) | ||
) | ||
); | ||
assert_eq!( | ||
prost_struct.fields.get("f6").unwrap(), | ||
&Value::pb_list( | ||
vec![Value::number(1_f64), | ||
Value::number(2_f64), | ||
Value::number(3_f64)] | ||
) | ||
); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
poem-openapi/src/types/external/prost_wkt_types/value.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use std::borrow::Cow; | ||
|
||
use prost_wkt_types::value::Kind; | ||
|
||
use crate::registry::{MetaSchema, MetaSchemaRef}; | ||
use crate::types::{ParseError, ParseFromJSON, ParseResult, ToJSON, Type}; | ||
|
||
impl Type for prost_wkt_types::Value { | ||
const IS_REQUIRED: bool = true; | ||
|
||
type RawValueType = prost_wkt_types::Value; | ||
|
||
type RawElementValueType = prost_wkt_types::Value; | ||
|
||
fn name() -> Cow<'static, str> { | ||
"Protobuf Value".into() | ||
} | ||
|
||
fn schema_ref() -> MetaSchemaRef { | ||
MetaSchemaRef::Inline(Box::new(MetaSchema::ANY)) | ||
} | ||
|
||
fn as_raw_value(&self) -> Option<&Self::RawValueType> { | ||
Some(self) | ||
} | ||
|
||
fn raw_element_iter<'a>( | ||
&'a self, | ||
) -> Box<dyn Iterator<Item=&'a Self::RawElementValueType> + 'a> { | ||
Box::new(self.as_raw_value().into_iter()) | ||
} | ||
fn is_empty(&self) -> bool { | ||
matches!(self.kind, Some(Kind::NullValue(_)) | None) | ||
} | ||
} | ||
|
||
|
||
impl ParseFromJSON for prost_wkt_types::Value { | ||
fn parse_from_json(value: Option<serde_json::Value>) -> ParseResult<Self> { | ||
let value = value.unwrap_or_default(); | ||
serde_json::from_value(value).map_err(|e| ParseError::custom(e.to_string())) | ||
} | ||
} | ||
|
||
impl ToJSON for prost_wkt_types::Value { | ||
fn to_json(&self) -> Option<serde_json::Value> { | ||
serde_json::to_value(self).ok() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
||
use prost_wkt_types::Value; | ||
use serde_json::json; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn parse_from_number() { | ||
let value = Value::parse_from_json(Some( | ||
json!(10_f64) | ||
)).unwrap(); | ||
assert_eq!( | ||
value, | ||
Value::number(10_f64) | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_from_string() { | ||
let value = Value::parse_from_json(Some( | ||
json!("Hi") | ||
)).unwrap(); | ||
assert_eq!( | ||
value, | ||
Value::string("Hi".into()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_from_bool() { | ||
let value = Value::parse_from_json(Some( | ||
json!(true) | ||
)).unwrap(); | ||
assert_eq!( | ||
value, | ||
Value::bool(true) | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_from_null() { | ||
let value = Value::parse_from_json(Some( | ||
json!(null) | ||
)).unwrap(); | ||
assert_eq!( | ||
value, | ||
Value::null() | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_from_struct() { | ||
let value = Value::parse_from_json(Some( | ||
json!({"f1": "Hello"}) | ||
)).unwrap(); | ||
assert_eq!( | ||
value, | ||
Value::pb_struct( | ||
HashMap::from([("f1".into(), Value::string("Hello".into()))]) | ||
) | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_from_list() { | ||
let value = Value::parse_from_json(Some( | ||
json!([1,2,3]) | ||
)).unwrap(); | ||
assert_eq!( | ||
value, | ||
Value::pb_list( | ||
vec![Value::number(1_f64), | ||
Value::number(2_f64), | ||
Value::number(3_f64)] | ||
) | ||
); | ||
} | ||
} |