Skip to content

Commit 3d83f99

Browse files
authored
Maps are being returned not as pointers as they are already pointers (#1956)
This was incorrectly attempting to coerce maps/lists as pointers when Decode does not return them that way. Added a test that failed before and now passes. <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Fixes incorrect pointer coercion for maps and lists in `render_value_coercion()` and adds a test to verify the fix. > > - **Behavior**: > - Fixes incorrect pointer coercion for maps and lists in `render_value_coercion()` in `generate_types.rs`. > - Adds a test case in `cffi_test.go` to verify correct handling of maps and lists. > - **Models**: > - Adds `is_map` field to `GoType` struct in `generate_types.rs`. > - Updates `ToTypeReferenceInTypeDefinition` implementation to set `is_map` for `FieldType::Map`. > - **Misc**: > - Updates `Decode` calls in `types.go` and `unions.go` to remove unnecessary pointer dereferencing for maps. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for e1fc8a0. You can [customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent ae13c4d commit 3d83f99

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

engine/language_client_codegen/src/go/generate_types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ fn render_value_coercion(container_variable_name: &str, field_type: &GoType) ->
6565
inner_type.name,
6666
render_value_coercion("__holder", inner_type),
6767
);
68+
} else if field_type.is_slice || field_type.is_map {
69+
return format!(
70+
"baml.Decode({container_variable_name}).({})",
71+
filters::type_name_without_pointer(&field_type.name).unwrap()
72+
);
6873
} else {
6974
return format!(
7075
"*baml.Decode({container_variable_name}).(*{})",
@@ -229,6 +234,7 @@ pub struct GoType {
229234
name: String,
230235
is_pointer: bool,
231236
is_slice: bool,
237+
is_map: bool,
232238
is_primitive: bool,
233239
is_class: bool,
234240
is_integer: bool,
@@ -530,6 +536,7 @@ impl ToTypeReferenceInTypeDefinition for FieldType {
530536
is_pointer: self.is_optional(),
531537
is_union: matches!(simplified, FieldType::Union(_)),
532538
is_slice: matches!(simplified, FieldType::List(_)),
539+
is_map: matches!(simplified, FieldType::Map(_, _)),
533540
is_primitive: self.is_primitive(),
534541
is_class: matches!(simplified, FieldType::Class(_)),
535542
is_integer: matches!(simplified, FieldType::Primitive(TypeValue::Int)),

integ-tests/go/baml_client/types/types.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integ-tests/go/baml_client/types/unions.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integ-tests/go/cffi_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ func TestEncodeDecode(t *testing.T) {
6565
Name: &[]string{"John Doe"}[0],
6666
Hair_color: &[]b.Color{b.ColorRED}[0],
6767
}},
68+
{&b.Recipe{
69+
Recipe_type: *b.Union__string_breakfast__string_dinnerNewWithString_breakfast(&[]string{"breakfast"}[0]),
70+
Ingredients: map[string]b.Quantity{
71+
"a": {
72+
Amount: *b.Union__int__floatNewWithInt(&[]int64{1}[0]),
73+
},
74+
},
75+
}},
6876
// {b.RecursiveUnion(*b.Union__string__Map__string_RecursiveUnionNewWithMap__string_RecursiveUnion(&map[string]b.RecursiveUnion{
6977
// "key": b.RecursiveUnion(*b.Union__string__Map__string_RecursiveUnionNewWithString(&[]string{"value"}[0])),
7078
// "key2": b.RecursiveUnion(*b.Union__string__Map__string_RecursiveUnionNewWithMap__string_RecursiveUnion(&map[string]b.RecursiveUnion{

0 commit comments

Comments
 (0)