Skip to content

Commit

Permalink
Update JSONish Parser (#658)
Browse files Browse the repository at this point in the history
* When parsing objects from objects, do as little as possible (match keys only)
* Make all logs debug or trace
  • Loading branch information
hellovai authored Jun 10, 2024
1 parent a464bde commit 87bd9ee
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(super) fn pick_best(
std::cmp::Ordering::Greater => std::cmp::Ordering::Greater,
});

log::warn!(
log::trace!(
"Picking {} from {:?} items. Picked({:?}):\n{}",
target,
res_index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ pub(super) fn coerce_array(
) -> Result<BamlValueWithFlags, ParsingError> {
assert!(matches!(list_target, FieldType::List(_)));

log::debug!(
"scope: {scope} :: coercing to: {name} (current: {current})",
name = list_target.to_string(),
scope = ctx.display_scope(),
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
);

let inner = match list_target {
FieldType::List(inner) => inner,
_ => unreachable!(),
Expand All @@ -34,7 +41,7 @@ pub(super) fn coerce_array(
}
Some(v) => {
flags.add_flag(Flag::SingleToArray);
match inner.coerce(&ctx.enter_scope("0"), inner, Some(v)) {
match inner.coerce(&ctx.enter_scope("<implied>"), inner, Some(v)) {
Ok(v) => items.push(v),
Err(e) => flags.add_flag(Flag::ArrayItemParseError(0, e)),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ pub(super) fn coerce_optional(
value: Option<&crate::jsonish::Value>,
) -> Result<BamlValueWithFlags, ParsingError> {
assert!(matches!(optional_target, FieldType::Optional(_)));
log::debug!(
"scope: {scope} :: coercing to: {name} (current: {current})",
name = optional_target.to_string(),
scope = ctx.display_scope(),
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
);

let inner = match optional_target {
FieldType::Optional(inner) => inner,
_ => unreachable!(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ impl TypeCoercer for TypeValue {
// Parsed from JSONish
value: Option<&crate::jsonish::Value>,
) -> Result<BamlValueWithFlags, ParsingError> {
log::debug!(
"scope: {scope} :: coercing to: {name} (current: {current})",
name = target.to_string(),
scope = ctx.display_scope(),
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
);

match self {
TypeValue::String => coerce_string(ctx, target, value),
TypeValue::Int => coerce_int(ctx, target, value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ pub(super) fn coerce_union(
value: Option<&crate::jsonish::Value>,
) -> Result<BamlValueWithFlags, ParsingError> {
assert!(matches!(union_target, FieldType::Union(_)));
log::debug!(
"scope: {scope} :: coercing to: {name} (current: {current})",
name = union_target.to_string(),
scope = ctx.display_scope(),
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
);

let options = match union_target {
FieldType::Union(options) => options,
_ => unreachable!(),
Expand Down
18 changes: 18 additions & 0 deletions engine/baml-lib/jsonish/src/deserializer/coercer/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ impl TypeCoercer for FieldType {
) -> Result<BamlValueWithFlags, ParsingError> {
match value {
Some(crate::jsonish::Value::AnyOf(candidates, primitive)) => {
log::debug!(
"scope: {scope} :: coercing to: {name} (current: {current})",
name = target.to_string(),
scope = ctx.display_scope(),
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
);
if matches!(target, FieldType::Primitive(TypeValue::String)) {
self.coerce(
ctx,
Expand All @@ -38,6 +44,12 @@ impl TypeCoercer for FieldType {
}
}
Some(crate::jsonish::Value::Markdown(_t, v)) => {
log::debug!(
"scope: {scope} :: coercing to: {name} (current: {current})",
name = target.to_string(),
scope = ctx.display_scope(),
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
);
self.coerce(ctx, target, Some(v)).and_then(|mut v| {
v.add_flag(Flag::ObjectFromMarkdown(
if matches!(target, FieldType::Primitive(TypeValue::String)) {
Expand All @@ -51,6 +63,12 @@ impl TypeCoercer for FieldType {
})
}
Some(crate::jsonish::Value::FixedJson(v, fixes)) => {
log::debug!(
"scope: {scope} :: coercing to: {name} (current: {current})",
name = target.to_string(),
scope = ctx.display_scope(),
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
);
let mut v = self.coerce(ctx, target, Some(v))?;
v.add_flag(Flag::ObjectFromFixedJson(fixes.to_vec()));
Ok(v)
Expand Down
Loading

0 comments on commit 87bd9ee

Please sign in to comment.