Skip to content

Commit

Permalink
Report error on multiple type builder blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniosarosi committed Jan 20, 2025
1 parent 3f00ac5 commit deef90b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ test ReturnDynamicClassTest {
- Java
"#
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ function ExtractResume(from_text: string) -> Resume {
}
}


test ReturnDynamicClassTest {
functions [ExtractResume]
type_builder {
class Foo {
foo string
}
dynamic Bar {
bar int
}
}
type_builder {
class A {
a string
}
dynamic B {
b int
}
}
args {
from_text "Test"
}
}

// error: Error validating: Only tests may have a type_builder block.
// --> tests/invalid_dynamic_types.baml:4
// |
Expand All @@ -24,3 +48,16 @@ function ExtractResume(from_text: string) -> Resume {
// 10 | }
// 11 | }
// |
// error: Error validating: Definition of multiple `type_builder` blocks in the same parent block
// --> tests/invalid_dynamic_types.baml:25
// |
// 24 | }
// 25 | type_builder {
// 26 | class A {
// 27 | a string
// 28 | }
// 29 | dynamic B {
// 30 | b int
// 31 | }
// 32 | }
// |
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ pub(crate) fn parse_value_expression_block(
}

Rule::type_builder_block => {
type_builder = Some(parse_type_builder_block(item, diagnostics)?);
let block = parse_type_builder_block(item, diagnostics)?;

match type_builder {
None => type_builder = Some(block),

Some(_) => diagnostics.push_error(DatamodelError::new_validation_error(
"Definition of multiple `type_builder` blocks in the same parent block",
block.span
)),
}
}

Rule::comment_block => pending_field_comment = Some(item),
Expand Down Expand Up @@ -140,10 +149,11 @@ pub(crate) fn parse_value_expression_block(
));
};

// Only test blocks can have `type_builder` blocks in them.
// Only test blocks can have `type_builder` blocks in them. This is not a
// "syntax" error so we won't fail yet.
if let Some(ref t) = type_builder {
if sub_type != Some(ValueExprBlockType::Test) {
return Err(DatamodelError::new_validation_error(
diagnostics.push_error(DatamodelError::new_validation_error(
"Only tests may have a type_builder block.",
t.span.to_owned(),
));
Expand Down

0 comments on commit deef90b

Please sign in to comment.