Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions cli/golem-cli/src/app/build/gen_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::app::build::up_to_date_check::new_task_up_to_date_check;
use crate::app::context::BuildContext;
use crate::bridge_gen::rust::RustBridgeGenerator;
use crate::bridge_gen::typescript::TypeScriptBridgeGenerator;
use crate::bridge_gen::{BridgeGenerator, bridge_client_directory_name};
use crate::bridge_gen::{
BridgeGenerator, BridgeGeneratorConfig, DeriveRule, bridge_client_directory_name,
};
use crate::command::GolemCliCommand;
use crate::error::NonSuccessfulExit;
use crate::fs;
Expand Down Expand Up @@ -103,6 +105,19 @@ async fn collect_manifest_targets(ctx: &BuildContext<'_>) -> anyhow::Result<Vec<
agent_type,
target_language,
output_dir,
derive_rules: sdks_targets
.additional_derives
.as_ref()
.map(|rules| {
rules
.iter()
.map(|r| DeriveRule {
pattern: r.pattern.clone(),
derives: r.derives.clone(),
})
.collect()
})
.unwrap_or_default(),
});
}
}
Expand Down Expand Up @@ -173,6 +188,7 @@ async fn collect_custom_targets(
agent_type,
target_language,
output_dir,
derive_rules: custom_target.derive_rules.clone(),
});
}
}
Expand Down Expand Up @@ -222,17 +238,24 @@ async fn gen_bridge_sdk_target(
);
let _indent = LogIndent::new();

let config = BridgeGeneratorConfig {
derive_rules: target.derive_rules,
};
let mut generator: Box<dyn BridgeGenerator> = match target.target_language {
GuestLanguage::Rust => Box::new(RustBridgeGenerator::new(
target.agent_type,
&output_dir,
false,
config,
)?),
GuestLanguage::TypeScript => Box::new(TypeScriptBridgeGenerator::new(
target.agent_type,
&output_dir,
false,
)?),
GuestLanguage::TypeScript => {
Box::new(<TypeScriptBridgeGenerator as BridgeGenerator>::new(
target.agent_type,
&output_dir,
false,
config,
)?)
}
GuestLanguage::Scala => {
bail!("Bridge generation is not yet supported for Scala")
}
Expand Down
1 change: 1 addition & 0 deletions cli/golem-cli/src/app/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ impl ApplicationContext {
agent_type_names: Default::default(),
target_language: Some(language),
output_dir: Some(repl_root_bridge_sdk_dir.clone()),
derive_rules: Vec::new(),
}
}

Expand Down
38 changes: 37 additions & 1 deletion cli/golem-cli/src/bridge_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,44 @@ use camino::Utf8Path;
use golem_common::model::agent::{AgentType, AgentTypeName};
use heck::ToKebabCase;

/// A rule that adds derive macros to generated types whose names match a regex pattern.
///
/// Multiple rules can match the same type; their derives are merged and deduplicated.
///
/// # Examples
///
/// Add `PartialEq` to all types:
/// ```yaml
/// { pattern: ".*", derives: ["PartialEq"] }
/// ```
///
/// Add `Eq` and `Hash` only to `Uuid`:
/// ```yaml
/// { pattern: "^Uuid$", derives: ["Eq", "Hash"] }
/// ```
#[derive(Debug, Clone)]
pub struct DeriveRule {
/// Regex pattern matched against the generated type name.
pub pattern: String,
/// Derive macros to add when the pattern matches (e.g., "PartialEq", "Eq", "Hash").
pub derives: Vec<String>,
}

/// Configuration options for bridge SDK code generation.
#[derive(Debug, Clone, Default)]
pub struct BridgeGeneratorConfig {
/// Rules for adding derive macros to generated types. Each rule pairs a regex
/// pattern (matched against type names) with a list of derives to add.
pub derive_rules: Vec<DeriveRule>,
}

pub trait BridgeGenerator {
fn new(agent_type: AgentType, target_path: &Utf8Path, testing: bool) -> anyhow::Result<Self>
fn new(
agent_type: AgentType,
target_path: &Utf8Path,
testing: bool,
config: BridgeGeneratorConfig,
) -> anyhow::Result<Self>
where
Self: Sized;
fn generate(&mut self) -> anyhow::Result<()>;
Expand Down
Loading
Loading