Skip to content

Commit

Permalink
Fix test relevant_data_models
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniosarosi committed Dec 17, 2024
1 parent d462e5c commit e0ae448
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
10 changes: 0 additions & 10 deletions engine/baml-lib/jsonish/src/deserializer/coercer/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,13 @@ use super::{
ParsingError,
};

static mut count: u32 = 0;

impl TypeCoercer for FieldType {
fn coerce(
&self,
ctx: &ParsingContext,
target: &FieldType,
value: Option<&crate::jsonish::Value>,
) -> Result<BamlValueWithFlags, ParsingError> {
unsafe {
eprintln!("{self:?} -> {target:?} -> {value:?}");
count += 1;
if count == 20 {
panic!("FUCK");
}
}

match value {
Some(crate::jsonish::Value::AnyOf(candidates, primitive)) => {
log::debug!(
Expand Down
34 changes: 28 additions & 6 deletions engine/baml-lib/jsonish/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use internal_baml_jinja::types::{Class, Enum, Name, OutputFormatContent};
#[macro_use]
pub mod macros;

mod test_aliases;
mod test_basics;
mod test_class;
mod test_class_2;
Expand All @@ -16,7 +17,7 @@ mod test_maps;
mod test_partials;
mod test_unions;

use indexmap::IndexSet;
use indexmap::{IndexMap, IndexSet};
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
Expand Down Expand Up @@ -56,12 +57,14 @@ fn render_output_format(
output: &FieldType,
env_values: &EvaluationContext<'_>,
) -> Result<OutputFormatContent> {
let (enums, classes, recursive_classes) = relevant_data_models(ir, output, env_values)?;
let (enums, classes, recursive_classes, structural_recursive_aliases) =
relevant_data_models(ir, output, env_values)?;

Ok(OutputFormatContent::target(output.clone())
.enums(enums)
.classes(classes)
.recursive_classes(recursive_classes)
.structural_recursive_aliases(structural_recursive_aliases)
.build())
}

Expand Down Expand Up @@ -126,11 +129,17 @@ fn relevant_data_models<'a>(
ir: &'a IntermediateRepr,
output: &'a FieldType,
env_values: &EvaluationContext<'_>,
) -> Result<(Vec<Enum>, Vec<Class>, IndexSet<String>)> {
) -> Result<(
Vec<Enum>,
Vec<Class>,
IndexSet<String>,
IndexMap<String, FieldType>,
)> {
let mut checked_types: HashSet<String> = HashSet::new();
let mut enums = Vec::new();
let mut classes: Vec<Class> = Vec::new();
let mut recursive_classes = IndexSet::new();
let mut structural_recursive_aliases = IndexMap::new();
let mut start: Vec<baml_types::FieldType> = vec![output.clone()];

while let Some(output) = start.pop() {
Expand Down Expand Up @@ -230,8 +239,16 @@ fn relevant_data_models<'a>(
});
}
}
// TODO: Add structural aliases here.
(FieldType::RecursiveTypeAlias(_), _) => {}
(FieldType::RecursiveTypeAlias(name), _) => {
// TODO: Same O(n) problem as above.
for cycle in ir.structural_recursive_alias_cycles() {
if cycle.contains_key(name) {
for (alias, target) in cycle.iter() {
structural_recursive_aliases.insert(alias.to_owned(), target.clone());
}
}
}
}
(FieldType::Literal(_), _) => {}
(FieldType::Primitive(_), _constraints) => {}
(FieldType::Constrained { .. }, _) => {
Expand All @@ -240,7 +257,12 @@ fn relevant_data_models<'a>(
}
}

Ok((enums, classes, recursive_classes))
Ok((
enums,
classes,
recursive_classes,
structural_recursive_aliases,
))
}

const EMPTY_FILE: &str = r#"
Expand Down
25 changes: 25 additions & 0 deletions engine/baml-lib/jsonish/src/tests/test_aliases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use baml_types::LiteralValue;

use super::*;

test_deserializer!(
test_simple_recursive_alias_list,
r#"
type A = A[]
"#,
"[[], [], [[]]]",
FieldType::RecursiveTypeAlias("A".into()),
[[], [], [[]]]
);

test_deserializer!(
test_recursive_alias_cycle,
r#"
type A = B
type B = C
type C = A[]
"#,
"[[], [], [[]]]",
FieldType::RecursiveTypeAlias("A".into()),
[[], [], [[]]]
);

0 comments on commit e0ae448

Please sign in to comment.