Skip to content

Commit 699be41

Browse files
committed
Add struct_name to Client
1 parent 2f20fad commit 699be41

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ Now call the BAML function:
6565
```elixir
6666
# from: The path to the baml_src directory.
6767
# namespace: The module name under which the returned structs will be nested.
68-
%BamlElixir.Client{from: "priv/baml_src", namespace: "MyApp.BamlClient"}
68+
# struct_name: The module name which will be used for the returned struct.
69+
%BamlElixir.Client{from: "priv/baml_src", namespace: "MyApp.BamlClient", struct_name: MyResumeStruct}
6970
|> BamlElixir.Native.call(c, "ExtractResume", %{resume: "John Doe is the CTO of Acme Inc."})
7071
```
7172

lib/baml_elixir/client.ex

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
defmodule BamlElixir.Client do
2-
defstruct namespace: "BamlClient",
3-
from: "baml_src"
2+
defstruct namespace: nil,
3+
from: "baml_src",
4+
struct_name: nil
5+
6+
def call(client, function_name, args) do
7+
client = %{
8+
client
9+
| namespace: client.namespace || "",
10+
struct_name: struct_name(client.struct_name)
11+
}
12+
13+
BamlElixir.Native.call(client, function_name, args)
14+
end
15+
16+
defp struct_name(struct_name) do
17+
if struct_name do
18+
struct_name
19+
|> Module.split()
20+
|> Enum.join(".")
21+
else
22+
""
23+
end
24+
end
425
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule BamlElixir.MixProject do
22
use Mix.Project
33

4-
@version "0.1.0"
4+
@version "0.2.0"
55

66
def project do
77
[

native/baml_elixir/src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,19 @@ fn baml_value_to_term<'a>(env: Env<'a>, value: &BamlValue, client: &Client) -> N
9797
Ok(result_map)
9898
}
9999
BamlValue::Class(class_name, fields) => {
100-
// Create an Elixir struct for the class using the client's namespace
101-
let module_name = format!("Elixir.{}.{}", client.namespace, class_name);
100+
// Create an Elixir struct for the class using the client's namespace and struct_name
101+
let struct_or_class = if client.struct_name.is_empty() {
102+
class_name
103+
} else {
104+
&client.struct_name
105+
};
106+
107+
let module_name = if !client.namespace.is_empty() {
108+
format!("Elixir.{}.{}", client.namespace, struct_or_class)
109+
} else {
110+
format!("Elixir.{}", struct_or_class)
111+
};
112+
102113
let mut struct_term = elixir_struct::make_ex_struct(env, &module_name)
103114
.map_err(|_| Error::Term(Box::new("Failed to create struct")))?;
104115
// Add all fields
@@ -116,6 +127,7 @@ fn baml_value_to_term<'a>(env: Env<'a>, value: &BamlValue, client: &Client) -> N
116127
#[module = "BamlElixir.Client"]
117128
struct Client {
118129
namespace: String,
130+
struct_name: String,
119131
from: String,
120132
}
121133

0 commit comments

Comments
 (0)