Merge pull request #64 from oli-obk/missing_renamed_field
missing field errors displayed original field name instead of renamed
This commit is contained in:
commit
3b44792ff3
@ -639,9 +639,11 @@ fn deserialize_map(
|
||||
let extract_values: Vec<P<ast::Stmt>> = field_names.iter()
|
||||
.zip(struct_def.fields.iter())
|
||||
.map(|(field_name, field)| {
|
||||
let name_str = match field.node.kind {
|
||||
ast::NamedField(name, _) => builder.expr().str(name),
|
||||
ast::UnnamedField(_) => panic!("struct contains unnamed fields"),
|
||||
let rename = field::field_rename(field, &field::Direction::Deserialize);
|
||||
let name_str = match (rename, field.node.kind) {
|
||||
(Some(rename), _) => builder.expr().build_lit(P(rename.clone())),
|
||||
(None, ast::NamedField(name, _)) => builder.expr().str(name),
|
||||
(None, ast::UnnamedField(_)) => panic!("struct contains unnamed fields"),
|
||||
};
|
||||
|
||||
let missing_expr = if field::default_value(field) {
|
||||
|
@ -10,7 +10,7 @@ pub enum Direction {
|
||||
Deserialize,
|
||||
}
|
||||
|
||||
fn field_rename<'a>(
|
||||
pub fn field_rename<'a>(
|
||||
field: &'a ast::StructField,
|
||||
direction: &Direction,
|
||||
) -> Option<&'a ast::Lit> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
#![feature(custom_derive, plugin, test)]
|
||||
#![feature(custom_derive, plugin, test, custom_attribute)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate test;
|
||||
@ -1019,3 +1019,26 @@ fn test_missing_field() {
|
||||
))).unwrap();
|
||||
assert_eq!(value, Foo { x: Some(5) });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_renamed_field() {
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
struct Foo {
|
||||
#[serde(rename_deserialize="y")]
|
||||
x: Option<u32>,
|
||||
}
|
||||
|
||||
let value: Foo = from_str("{}").unwrap();
|
||||
assert_eq!(value, Foo { x: None });
|
||||
|
||||
let value: Foo = from_str("{\"y\": 5}").unwrap();
|
||||
assert_eq!(value, Foo { x: Some(5) });
|
||||
|
||||
let value: Foo = from_value(Value::Object(treemap!())).unwrap();
|
||||
assert_eq!(value, Foo { x: None });
|
||||
|
||||
let value: Foo = from_value(Value::Object(treemap!(
|
||||
"y".to_string() => Value::I64(5)
|
||||
))).unwrap();
|
||||
assert_eq!(value, Foo { x: Some(5) });
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user