default value behaviour

main
Ziyang Hu 2 years ago
parent dd39abffc2
commit 2633d191ff

@ -136,7 +136,7 @@ impl AttributeTyping {
fn type_err(&self, val: Value) -> TypeError { fn type_err(&self, val: Value) -> TypeError {
TypeError::Typing(*self, format!("{:?}", val)) TypeError::Typing(*self, format!("{:?}", val))
} }
fn coerce_value<'a>(&self, val: Value<'a>) -> Result<Value<'a>> { pub(crate) fn coerce_value<'a>(&self, val: Value<'a>) -> Result<Value<'a>> {
match self { match self {
AttributeTyping::Ref | AttributeTyping::Component => match val { AttributeTyping::Ref | AttributeTyping::Component => match val {
val @ Value::EnId(_) => Ok(val), val @ Value::EnId(_) => Ok(val),

@ -126,7 +126,7 @@ impl SessionTx {
JsonValue::Object(desc.clone()), JsonValue::Object(desc.clone()),
"expect boolean or number".to_string(), "expect boolean or number".to_string(),
) )
.into()); .into());
} }
recursive = true; recursive = true;
} }
@ -144,7 +144,7 @@ impl SessionTx {
JsonValue::Object(desc.clone()), JsonValue::Object(desc.clone()),
"expect array".to_string(), "expect array".to_string(),
) )
.into()); .into());
} }
}; };
} }
@ -153,7 +153,7 @@ impl SessionTx {
v.into(), v.into(),
"unexpected spec key".to_string(), "unexpected spec key".to_string(),
) )
.into()); .into());
} }
} }
} }
@ -169,7 +169,7 @@ impl SessionTx {
JsonValue::Object(desc.clone()), JsonValue::Object(desc.clone()),
"expect target key".to_string(), "expect target key".to_string(),
) )
.into()); .into());
} }
let input_kw = input_kw.unwrap(); let input_kw = input_kw.unwrap();
@ -188,6 +188,12 @@ impl SessionTx {
recursion_depth = max(recursion_depth, 1); recursion_depth = max(recursion_depth, 1);
} }
let default_val = if default_val == Value::Null {
default_val
} else {
attr.val_type.coerce_value(default_val)?
};
Ok(PullSpec::Attr(AttrPullSpec { Ok(PullSpec::Attr(AttrPullSpec {
attr, attr,
default_val, default_val,

@ -152,7 +152,7 @@ impl SessionTx {
collector, collector,
recursive_seen, recursive_seen,
)?; )?;
} else { } else if spec.default_val != Value::Null {
self.pull_attr_collect( self.pull_attr_collect(
spec, spec,
spec.default_val.clone(), spec.default_val.clone(),
@ -372,7 +372,7 @@ impl SessionTx {
collector, collector,
recursive_seen, recursive_seen,
)?; )?;
} else { } else if spec.default_val != Value::Null {
self.pull_attr_collect( self.pull_attr_collect(
spec, spec,
spec.default_val.clone(), spec.default_val.clone(),

Loading…
Cancel
Save