Skip to content

Commit d4ab4c7

Browse files
committed
feat(bridge-gen): custom derive rules and optional client feature
1 parent ba0a4a5 commit d4ab4c7

11 files changed

Lines changed: 274 additions & 22 deletions

File tree

cli/golem-cli/src/app/build/gen_bridge.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::app::build::up_to_date_check::new_task_up_to_date_check;
44
use crate::app::context::BuildContext;
55
use crate::bridge_gen::rust::RustBridgeGenerator;
66
use crate::bridge_gen::typescript::TypeScriptBridgeGenerator;
7-
use crate::bridge_gen::{BridgeGenerator, bridge_client_directory_name};
7+
use crate::bridge_gen::{
8+
BridgeGenerator, BridgeGeneratorConfig, DeriveRule, bridge_client_directory_name,
9+
};
810
use crate::command::GolemCliCommand;
911
use crate::error::NonSuccessfulExit;
1012
use crate::fs;
@@ -103,6 +105,19 @@ async fn collect_manifest_targets(ctx: &BuildContext<'_>) -> anyhow::Result<Vec<
103105
agent_type,
104106
target_language,
105107
output_dir,
108+
derive_rules: sdks_targets
109+
.additional_derives
110+
.as_ref()
111+
.map(|rules| {
112+
rules
113+
.iter()
114+
.map(|r| DeriveRule {
115+
pattern: r.pattern.clone(),
116+
derives: r.derives.clone(),
117+
})
118+
.collect()
119+
})
120+
.unwrap_or_default(),
106121
});
107122
}
108123
}
@@ -173,6 +188,7 @@ async fn collect_custom_targets(
173188
agent_type,
174189
target_language,
175190
output_dir,
191+
derive_rules: custom_target.derive_rules.clone(),
176192
});
177193
}
178194
}
@@ -222,17 +238,24 @@ async fn gen_bridge_sdk_target(
222238
);
223239
let _indent = LogIndent::new();
224240

241+
let config = BridgeGeneratorConfig {
242+
derive_rules: target.derive_rules,
243+
};
225244
let mut generator: Box<dyn BridgeGenerator> = match target.target_language {
226245
GuestLanguage::Rust => Box::new(RustBridgeGenerator::new(
227246
target.agent_type,
228247
&output_dir,
229248
false,
249+
config,
230250
)?),
231-
GuestLanguage::TypeScript => Box::new(TypeScriptBridgeGenerator::new(
232-
target.agent_type,
233-
&output_dir,
234-
false,
235-
)?),
251+
GuestLanguage::TypeScript => {
252+
Box::new(<TypeScriptBridgeGenerator as BridgeGenerator>::new(
253+
target.agent_type,
254+
&output_dir,
255+
false,
256+
config,
257+
)?)
258+
}
236259
GuestLanguage::Scala => {
237260
bail!("Bridge generation is not yet supported for Scala")
238261
}

cli/golem-cli/src/app/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ impl ApplicationContext {
235235
agent_type_names: Default::default(),
236236
target_language: Some(language),
237237
output_dir: Some(repl_root_bridge_sdk_dir.clone()),
238+
derive_rules: Vec::new(),
238239
}
239240
}
240241

cli/golem-cli/src/bridge_gen/mod.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,44 @@ use camino::Utf8Path;
2121
use golem_common::model::agent::{AgentType, AgentTypeName};
2222
use heck::ToKebabCase;
2323

24+
/// A rule that adds derive macros to generated types whose names match a regex pattern.
25+
///
26+
/// Multiple rules can match the same type; their derives are merged and deduplicated.
27+
///
28+
/// # Examples
29+
///
30+
/// Add `PartialEq` to all types:
31+
/// ```yaml
32+
/// { pattern: ".*", derives: ["PartialEq"] }
33+
/// ```
34+
///
35+
/// Add `Eq` and `Hash` only to `Uuid`:
36+
/// ```yaml
37+
/// { pattern: "^Uuid$", derives: ["Eq", "Hash"] }
38+
/// ```
39+
#[derive(Debug, Clone)]
40+
pub struct DeriveRule {
41+
/// Regex pattern matched against the generated type name.
42+
pub pattern: String,
43+
/// Derive macros to add when the pattern matches (e.g., "PartialEq", "Eq", "Hash").
44+
pub derives: Vec<String>,
45+
}
46+
47+
/// Configuration options for bridge SDK code generation.
48+
#[derive(Debug, Clone, Default)]
49+
pub struct BridgeGeneratorConfig {
50+
/// Rules for adding derive macros to generated types. Each rule pairs a regex
51+
/// pattern (matched against type names) with a list of derives to add.
52+
pub derive_rules: Vec<DeriveRule>,
53+
}
54+
2455
pub trait BridgeGenerator {
25-
fn new(agent_type: AgentType, target_path: &Utf8Path, testing: bool) -> anyhow::Result<Self>
56+
fn new(
57+
agent_type: AgentType,
58+
target_path: &Utf8Path,
59+
testing: bool,
60+
config: BridgeGeneratorConfig,
61+
) -> anyhow::Result<Self>
2662
where
2763
Self: Sized;
2864
fn generate(&mut self) -> anyhow::Result<()>;

0 commit comments

Comments
 (0)