Skip to content

Commit

Permalink
Integ test works! Yeah
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniosarosi committed Dec 17, 2024
1 parent 72ea8cb commit 140b3dd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
18 changes: 12 additions & 6 deletions engine/baml-lib/jinja-runtime/src/output_format/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct OutputFormatContent {
pub enums: Arc<IndexMap<String, Enum>>,
pub classes: Arc<IndexMap<String, Class>>,
recursive_classes: Arc<IndexSet<String>>,
structural_recursive_aliases: Arc<IndexSet<String>>,
structural_recursive_aliases: Arc<IndexMap<String, FieldType>>,
pub target: FieldType,
}

Expand All @@ -69,7 +69,7 @@ pub struct Builder {
/// Order matters for this one.
recursive_classes: IndexSet<String>,
/// Recursive aliases introduced maps and lists.
structural_recursive_aliases: IndexSet<String>,
structural_recursive_aliases: IndexMap<String, FieldType>,
target: FieldType,
}

Expand All @@ -79,7 +79,7 @@ impl Builder {
enums: vec![],
classes: vec![],
recursive_classes: IndexSet::new(),
structural_recursive_aliases: IndexSet::new(),
structural_recursive_aliases: IndexMap::new(),
target,
}
}
Expand All @@ -101,7 +101,7 @@ impl Builder {

pub fn structural_recursive_aliases(
mut self,
structural_recursive_aliases: IndexSet<String>,
structural_recursive_aliases: IndexMap<String, FieldType>,
) -> Self {
self.structural_recursive_aliases = structural_recursive_aliases;
self
Expand Down Expand Up @@ -710,13 +710,19 @@ impl OutputFormatContent {
pub fn find_enum(&self, name: &str) -> Result<&Enum> {
self.enums
.get(name)
.ok_or_else(|| anyhow::anyhow!("Enum {} not found", name))
.ok_or_else(|| anyhow::anyhow!("Enum {name} not found"))
}

pub fn find_class(&self, name: &str) -> Result<&Class> {
self.classes
.get(name)
.ok_or_else(|| anyhow::anyhow!("Class {} not found", name))
.ok_or_else(|| anyhow::anyhow!("Class {name} not found"))
}

pub fn find_recursive_alias_target(&self, name: &str) -> Result<&FieldType> {
self.structural_recursive_aliases
.get(name)
.ok_or_else(|| anyhow::anyhow!("Recursive alias {name} not found"))
}
}

Expand Down
15 changes: 12 additions & 3 deletions engine/baml-lib/jsonish/src/deserializer/coercer/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,17 @@ impl TypeCoercer for FieldType {
FieldType::Enum(e) => IrRef::Enum(e).coerce(ctx, target, value),
FieldType::Literal(l) => l.coerce(ctx, target, value),
FieldType::Class(c) => IrRef::Class(c).coerce(ctx, target, value),
// TODO: How to handle this?
FieldType::RecursiveTypeAlias(_) => todo!("Alias with no resolution"),
// TODO: This doesn't look too good compared to the rest of
// match arms. Should we make use of the context like this here?
FieldType::RecursiveTypeAlias(name) => ctx
.of
.find_recursive_alias_target(name)
.map_err(|e| ParsingError {
reason: format!("Failed to find recursive alias target: {e}"),
scope: ctx.scope.clone(),
causes: Vec::new(),
})?
.coerce(ctx, target, value),
FieldType::List(_) => coerce_array(ctx, self, value),
FieldType::Union(_) => coerce_union(ctx, self, value),
FieldType::Optional(_) => coerce_optional(ctx, self, value),
Expand All @@ -90,7 +99,7 @@ impl TypeCoercer for FieldType {
let mut coerced_value = base.coerce(ctx, base, value)?;
let constraint_results = run_user_checks(&coerced_value.clone().into(), self)
.map_err(|e| ParsingError {
reason: format!("Failed to evaluate constraints: {:?}", e),
reason: format!("Failed to evaluate constraints: {e:?}"),
scope: ctx.scope.clone(),
causes: Vec::new(),
})?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::{ParsingContext, ParsingError};
pub(super) enum IrRef<'a> {
Enum(&'a String),
Class(&'a String),
RecursiveAlias(&'a String),
}

impl TypeCoercer for IrRef<'_> {
Expand All @@ -29,6 +30,10 @@ impl TypeCoercer for IrRef<'_> {
Ok(c) => c.coerce(ctx, target, value),
Err(e) => Err(ctx.error_internal(e.to_string())),
},
IrRef::RecursiveAlias(a) => match ctx.of.find_recursive_alias_target(a.as_str()) {
Ok(a) => a.coerce(ctx, target, value),
Err(e) => Err(ctx.error_internal(e.to_string())),
},
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashSet;

use anyhow::Result;
use baml_types::BamlValue;
use indexmap::IndexSet;
use indexmap::{IndexMap, IndexSet};
use internal_baml_core::ir::{
repr::IntermediateRepr, ClassWalker, EnumWalker, FieldType, IRHelper,
};
Expand Down Expand Up @@ -212,12 +212,17 @@ fn relevant_data_models<'a>(
ir: &'a IntermediateRepr,
output: &'a FieldType,
ctx: &RuntimeContext,
) -> Result<(Vec<Enum>, Vec<Class>, IndexSet<String>, IndexSet<String>)> {
) -> Result<(
Vec<Enum>,
Vec<Class>,
IndexSet<String>,
IndexMap<String, FieldType>,
)> {
let mut checked_types = HashSet::new();
let mut enums = Vec::new();
let mut classes = Vec::new();
let mut recursive_classes = IndexSet::new();
let mut structural_recursive_aliases = IndexSet::new();
let mut structural_recursive_aliases = IndexMap::new();
let mut start: Vec<baml_types::FieldType> = vec![output.clone()];

let eval_ctx = ctx.eval_ctx(false);
Expand Down Expand Up @@ -381,10 +386,11 @@ fn relevant_data_models<'a>(
}
(FieldType::RecursiveTypeAlias(name), _) => {
// TODO: Same O(n) problem as above.
// TODO: Do we need the type information or just the name?
for cycle in ir.structural_recursive_alias_cycles() {
if cycle.contains_key(name) {
structural_recursive_aliases.extend(cycle.keys().map(ToOwned::to_owned));
for (alias, target) in cycle.iter() {
structural_recursive_aliases.insert(alias.to_owned(), target.clone());
}
}
}
}
Expand Down

0 comments on commit 140b3dd

Please sign in to comment.