Skip to content

Commit 7105b13

Browse files
authored
Fix optional struct fields (#1889)
Now optional struct fields can be encoded and decoded properly I also made it so that gofmt/goimports runs on generate so this is the last time the diff should be this annoying! <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Fixes encoding/decoding of optional struct fields and adds `gofmt` to Go code generation. > > - **Behavior**: > - Fixes encoding and decoding of optional struct fields in `generate_types.rs`. > - Updates `Decode` functions in `types.go` to handle nil values for optional fields. > - **Code Generation**: > - Adds `gofmt` execution to `generators.baml` and `primary.yml` to format Go code on generation. > - **Misc**: > - Updates `generators.baml` in multiple `inlinedbaml` files to include `gofmt` command. > > <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 2c08b82. 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 9bcea07 commit 7105b13

18 files changed

Lines changed: 5553 additions & 7218 deletions

File tree

.github/workflows/primary.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ jobs:
5555
steps:
5656
- uses: actions/checkout@v4
5757
- uses: jdx/mise-action@v2
58+
- uses: actions/setup-go@v5
59+
with:
60+
go-version: "1.21.x"
5861
- uses: actions/setup-node@v4
5962
with:
6063
node-version: 20
@@ -133,6 +136,9 @@ jobs:
133136
- uses: actions/checkout@v4
134137
- uses: jdx/mise-action@v2
135138
- uses: astral-sh/setup-uv@v5
139+
- uses: actions/setup-go@v5
140+
with:
141+
go-version: "1.21.x"
136142
- uses: dtolnay/rust-toolchain@stable
137143
with:
138144
toolchain: stable

engine/language_client_codegen/src/go/generate_types.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,20 @@ pub(crate) fn cast_value(container_variable_name: &str, field_type: &GoType) ->
5454
}
5555

5656
fn render_value_coercion(container_variable_name: &str, field_type: &GoType) -> String {
57-
if field_type.is_class {
57+
if field_type.is_pointer {
58+
let inner_type = field_type.underlying_type.as_ref().unwrap();
59+
return format!(
60+
"func () {} {{
61+
val := baml.Decode({})
62+
if val == nil {{
63+
return nil
64+
}}
65+
castVal := val.({})
66+
return &castVal
67+
}}()",
68+
field_type.name, container_variable_name, inner_type.name,
69+
);
70+
} else if field_type.is_class {
5871
return format!(
5972
"*baml.Decode({}).(*{})",
6073
container_variable_name,

engine/language_client_codegen/src/go/mod.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,18 @@ class Foo {
287287
.unwrap()
288288
}
289289

290-
#[test]
291-
fn generate_streaming_go() {
292-
let ir = mk_ir();
293-
let generator_args = mk_gen();
294-
let res = generate(&ir, &generator_args).unwrap();
295-
let partial_types = res
296-
.get(&PathBuf::from("stream_types/stream_types.go"))
297-
.unwrap();
298-
eprintln!("{}", partial_types);
299-
}
290+
// Commenting out this test because streaming is not yet supported.
291+
//
292+
// TODO: re-enable this test once streaming is supported.
293+
//
294+
// #[test]
295+
// fn generate_streaming_go() {
296+
// let ir = mk_ir();
297+
// let generator_args = mk_gen();
298+
// let res = generate(&ir, &generator_args).unwrap();
299+
// let partial_types = res
300+
// .get(&PathBuf::from("stream_types/stream_types.go"))
301+
// .unwrap();
302+
// eprintln!("{}", partial_types);
303+
// }
300304
}

engine/language_client_codegen/src/go/templates/partial_types.go.j2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ type StreamState[T any] struct {
6464
}
6565

6666

67-
6867
{% for class in partial_classes %}
6968
{%- if let Some(docstring) = class.docstring %}
7069
{{ docstring }}

integ-tests/baml_src/generators.baml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ generator lang_go {
4343
output_dir "../go"
4444
version "0.86.1"
4545
client_package_name "example.com/integ-tests"
46+
on_generate "gofmt -w ."
4647
}
4748

4849

0 commit comments

Comments
 (0)