Skip to content
Merged
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
4 changes: 2 additions & 2 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "GPL-3.0-only"
# Consumed by git rev (see ComlineProject/docs → Design → Runtime & generation
# repository structure — no crates.io publishing at this stage). Local
# iteration: add a `[patch]` onto a sibling checkout.
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "da81b688c949a0ebad4f213d6fdb648920cf641d" }
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "922847b608764687b9d75020ff46a3f24d43b5bd" }
comline-core = { git = "https://github.com/ComlineProject/core", rev = "868e8310e33df317e3cba4f76b81fbcd8639c1c2", default-features = false }

eyre = "0.6.8"
Expand All @@ -21,6 +21,6 @@ path = "src/bin/comline-gen-typescript.rs"
# The conformance corpus: every fixture is a whole package. Same generation
# rev as `comline-codegen` above, so the corpus and the generator share one
# `comline-codegen`.
comline-conformance = { git = "https://github.com/ComlineProject/generation", rev = "da81b688c949a0ebad4f213d6fdb648920cf641d" }
comline-conformance = { git = "https://github.com/ComlineProject/generation", rev = "922847b608764687b9d75020ff46a3f24d43b5bd" }
# The tests compile schemas, so they need core's compiler.
comline-core = { git = "https://github.com/ComlineProject/core", rev = "868e8310e33df317e3cba4f76b81fbcd8639c1c2" }
27 changes: 19 additions & 8 deletions codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ pub fn generate_typescript(req: &GenerateRequest) -> Result<Vec<GeneratedFile>>
comline_codegen::facts::check(req)?;
let codec = type_of(&req.encodings, &req.wire_format);
// JSON-RPC protocols sharing a connection whose bare method names one
// side couldn't tell apart.
connections::check(&req.schemas, &req.encodings)?;
// side couldn't tell apart; a dependency's protocols aren't on it.
connections::check_in(req)?;
let external_std = req.external_std;

// With `external_std`, std's schemas aren't this package's to emit - it
Expand Down Expand Up @@ -1284,6 +1284,17 @@ fn package_module(header: &str, req: &GenerateRequest, emitted: &[(String, Vec<F
Some(s)
}

/// A protocol's path as the generated text shows it: `chat::admin::Admin`,
/// every segment joined by `::` however the namespace is spelled (a request's
/// is `/`-joined, the facts' `::`-joined). Display only; no number, order or
/// hash is made from it.
fn display_path(namespace: &str, name: &str) -> String {
let mut path = comline_codegen::facts::namespace_segments(namespace).collect::<Vec<_>>().join("::");
path.push_str("::");
path.push_str(name);
path
}

/// The package module's `comline@2` items: this end's Hello (the facts it was
/// generated from), `handshake()` with the parameters the package states, and
/// `negotiated` / `warnings` for what a connection agreed.
Expand All @@ -1307,10 +1318,9 @@ fn comline2_items(
.map(|(f, n)| format!("`{f}` {n}"))
.collect();
s.push_str(&format!(
" * - {} `{}::{}`, line {}: {}\n",
" * - {} `{}`, line {}: {}\n",
p.number,
p.namespace,
p.name,
display_path(&p.namespace, &p.name),
p.line,
functions.join(", ")
));
Expand Down Expand Up @@ -1339,7 +1349,7 @@ fn comline2_items(
));
let paths: Vec<String> = protocols
.iter()
.map(|p| format!("[{}, \"{}::{}\"]", p.number, p.namespace, p.name))
.map(|p| format!("[{}, \"{}\"]", p.number, display_path(&p.namespace, &p.name)))
.collect();
s.push_str(&format!(
"/** The package's protocols on the connection, by wire number. */\n\
Expand Down Expand Up @@ -1769,14 +1779,15 @@ fn protocol(
_ => None,
})
.collect();
let path = display_path(namespace, proto);
for (i, f) in fns.iter().enumerate() {
let id = calls[i];
// Under `comline@2`, checked against what the connection agreed
// before it's written.
let check = match &checks {
Some(c) => format!(
" {check_call}(this.peer, {proto}Sender.PROTOCOL, {}, \"{namespace}::{proto}\", \"{}\");\n",
c[i], fn_names[i]
" {check_call}(this.peer, {proto}Sender.PROTOCOL, {}, \"{}\", \"{}\");\n",
c[i], path, fn_names[i]
),
None => String::new(),
};
Expand Down
165 changes: 165 additions & 0 deletions codegen/tests/nested_namespaces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//! A protocol in a nested schema (`chat/admin`, generation#148) and a package
//! whose dependency's protocols share a method name with its own
//! (generation#147).
//!
//! A protocol's path in the generated text is `chat::admin::Admin`, every
//! segment joined by `::`, not the request's `chat/admin::Admin`; only the
//! text, no number, order or hash is made from it. The output is kept in
//! `runtime/test/generated/nested/` and `runtime/test/generated/clash/` so the
//! Node job type-checks it (`npm run typecheck:generated`). Regenerate with
//! `TS_BLESS=1 cargo test -p comline-codegen-typescript`.

use comline_codegen::validation::ValidateOnDecode;
use comline_codegen::{connections, facts, GenerateRequest, PackageMeta, TargetLanguage};
use comline_codegen_typescript::generate_typescript;
use comline_conformance::{dependency_and_own_protocols, nested_protocol, Fixture};
use comline_core::schema::ir::frozen::unit::{ConfigValue, FrozenUnit};

/// The request `comline generation` would send for `fx`: with the facts of the
/// package's own schemas when they're needed.
fn request(fx: &Fixture) -> GenerateRequest {
let dependencies: Vec<String> = fx.dependencies.iter().map(|d| d.to_string()).collect();
let own: Vec<_> = fx
.schemas
.iter()
.filter(|(namespace, _)| !connections::is_dependency(&dependencies, namespace))
.cloned()
.collect();
let mut req = GenerateRequest {
mode: fx.mode,
schemas: fx.schemas.clone(),
package: PackageMeta { name: "conformance".into(), version: "0.1.0".into() },
target: TargetLanguage {
language: "typescript".into(),
version: comline_codegen_typescript::LANG_VERSION.into(),
},
wire_format: "packed@1".into(),
external_std: false,
validate_on_decode: ValidateOnDecode::Off,
async_api: false,
encodings: comline_codegen::encodings::official_for("typescript"),
facts: None,
dependencies,
};
if facts::needed_in(&req) {
req.facts = Some(comline_conformance::facts_for(&own, "packed@1"));
}
req
}

/// Every file generated for `req`, checked against `runtime/test/generated/<dir>`.
fn generated(req: &GenerateRequest, dir: &str) -> Vec<(String, String)> {
let files = generate_typescript(req).unwrap_or_else(|e| panic!("{dir}: {e:#}"));
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../runtime/test/generated").join(dir);
let mut out = Vec::new();
for file in files {
let path = root.join(&file.path);
if std::env::var_os("TS_BLESS").is_some() {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, &file.contents).unwrap();
} else {
let committed = std::fs::read_to_string(&path).unwrap_or_default();
assert_eq!(
file.contents, committed,
"generated output drifted from {} — re-bless with TS_BLESS=1 cargo test -p comline-codegen-typescript",
path.display()
);
}
out.push((file.path.to_str().unwrap().to_string(), file.contents));
}
out
}

fn file<'a>(files: &'a [(String, String)], path: &str) -> &'a str {
&files.iter().find(|(p, _)| p == path).unwrap_or_else(|| panic!("no `{path}` in {:?}", files.iter().map(|(p, _)| p).collect::<Vec<_>>())).1
}

/// The protocol's path reads `chat::admin::Admin` everywhere the generated
/// text shows it: the Hello comment, `PROTOCOLS` and the `checkCall` the
/// sender makes (which is what an `Unavailable` error names). The request's
/// `chat/admin` spelling is nowhere in it.
#[test]
fn a_nested_protocols_path_is_spelled_with_colons() {
let files = generated(&request(&nested_protocol()), "nested");

let connection = file(&files, "connection.ts");
assert!(connection.contains(" * - 0 `chat::admin::Admin`, line 0.1.0: `greet` 0\n"), "{connection}");
assert!(connection.contains("new Map<number, string>([[0, \"chat::admin::Admin\"]])"), "{connection}");

let admin = file(&files, "chat/admin.ts");
assert!(admin.contains(", \"chat::admin::Admin\", \"greet\");"), "{admin}");

for (path, text) in &files {
assert!(!text.contains("chat/admin::"), "{path}: {text}");
assert!(!text.contains("`chat/admin"), "{path}: {text}");
}
}

/// The display path is the only thing that changed: the facts-addressed wire
/// numbers and the connection's hash are the same whichever way the namespace
/// is spelled.
#[test]
fn the_display_path_changes_no_number_or_hash() {
let req = request(&nested_protocol());
let files = generated(&req, "nested");
let connection = file(&files, "connection.ts");
let hash = format!("CONNECTION_IR_HASH = {:#018x}n;", connections::connection_ir_hash(&req.schemas));
assert!(connection.contains(&hash), "{connection}");
let admin = file(&files, "chat/admin.ts");
assert!(admin.contains("checkCall(this.peer, AdminSender.PROTOCOL, 0, "), "{admin}");
}

/// A dependency's `jsonrpc` protocol sharing a function name with the
/// package's own is no clash: the dependency's isn't on the package's
/// connection (generation#147), so generation isn't refused.
#[test]
fn a_dependencys_jsonrpc_method_name_does_not_refuse_generation() {
let mut fixture = dependency_and_own_protocols();
let jsonrpc = || FrozenUnit::Property { name: "framing".to_string(), value: ConfigValue::Str("jsonrpc".to_string()) };
for (_, units) in &mut fixture.schemas {
for unit in units {
if let FrozenUnit::Protocol { parameters, .. } = unit {
for p in parameters.iter_mut() {
if matches!(p, FrozenUnit::Property { name, .. } if name == "framing") {
*p = jsonrpc();
}
}
}
}
}
// The client provides `Events`, which is what puts `jsonrpc` protocols on a
// connection where bare method names matter.
let api = &mut fixture.schemas.iter_mut().find(|(n, _)| n == "api").unwrap().1;
let mut events = api.iter().find(|u| matches!(u, FrozenUnit::Protocol { name, .. } if name == "Chat")).unwrap().clone();
if let FrozenUnit::Protocol { name, parameters, functions, .. } = &mut events {
*name = "Events".to_string();
parameters.push(FrozenUnit::Property { name: "provider".to_string(), value: ConfigValue::Name("Client".to_string()) });
if let Some(FrozenUnit::Function { name, .. }) = functions.first_mut() {
*name = "notify".to_string();
}
}
api.push(events);
let req = request(&fixture);

// `localdeps`' `Foo` and the package's `Chat` both have `greet`, which the
// check unaware of dependencies refuses.
assert!(connections::check(&req.schemas, &req.encodings).is_err());
assert!(connections::check_in(&req).is_ok());

let files = generated(&req, "clash");
assert!(file(&files, "localdeps/greeting.ts").contains("export interface Foo"));
let connection = file(&files, "connection.ts");
assert!(connection.contains("chat: ns_api.Chat;"), "{connection}");
assert!(connection.contains("events: ns_api.Events;"), "{connection}");
assert!(!connection.contains("Foo"), "{connection}");

// Two of the package's own still clash, the dependency or not.
let mut own = req.clone();
let api = &mut own.schemas.iter_mut().find(|(n, _)| n == "api").unwrap().1;
let mut admin = api.iter().find(|u| matches!(u, FrozenUnit::Protocol { name, .. } if name == "Chat")).unwrap().clone();
if let FrozenUnit::Protocol { name, .. } = &mut admin {
*name = "Admin".to_string();
}
api.push(admin);
assert!(connections::check_in(&own).is_err());
}
Loading
Loading