Skip to content

fix: use service name const instead of string literal in generated clients#16

Merged
iainmcgin merged 3 commits intoanthropics:mainfrom
paikend:fix/use-service-name-const-in-client
Apr 2, 2026
Merged

fix: use service name const instead of string literal in generated clients#16
iainmcgin merged 3 commits intoanthropics:mainfrom
paikend:fix/use-service-name-const-in-client

Conversation

@paikend
Copy link
Copy Markdown
Contributor

@paikend paikend commented Mar 26, 2026

Summary

Addresses #9 — the codegen emits a *_SERVICE_NAME const for each service, but generated client methods were repeating the fully-qualified name as a string literal instead of referencing the const.

  • Generated client methods (call_unary, call_server_stream, call_client_stream, call_bidi_stream) were using the fully-qualified service name as a string literal, even though the codegen already emits a *_SERVICE_NAME const at the top of each generated file.
  • The server-side router already references the const (3 sites per service in the ELIZA example), but client call sites did not — this inconsistency meant renaming a service required updating both the const definition and every client call site.
  • The dispatcher's strip_prefix("service.name/") calls use a trailing-slash form and stay as-is (as noted in the issue).

Problem (from #9)

// Codegen emits this const:
pub const ELIZA_SERVICE_SERVICE_NAME: &str = "connectrpc.eliza.v1.ElizaService";

// But generated client methods repeated the literal:
call_unary(
    &self.transport, &self.config,
    "connectrpc.eliza.v1.ElizaService",  // ← should be ELIZA_SERVICE_SERVICE_NAME
    "Say",
    request, options,
).await

Fix

Changed generate_client_method to accept the const Ident and emit it at all four call sites:

// AFTER:
call_unary(
    &self.transport, &self.config,
    ELIZA_SERVICE_SERVICE_NAME,  // ← references the const
    "Say",
    request, options,
).await

Changes

  • connectrpc-codegen/src/codegen.rs:
    • Added Ident to proc_macro2 import
    • generate_client_method now takes service_name_const: &Ident + full_service_name: &str (kept for doc strings)
    • All 4 call sites (call_unary, call_server_stream, call_client_stream, call_bidi_stream) emit the const ident instead of the string literal

Type safety

  • call_unary etc. expect service: &str
  • The const is pub const *_SERVICE_NAME: &str = "..." → coerces to &str

Test plan

  • cargo check --package connectrpc-codegen — compiles cleanly
  • cargo test --package connectrpc-codegen — 10 tests passed
  • Regenerate examples with buf generate to verify generated output uses the const (requires buf CLI + protoc)

Closes #9

🤖 Generated with Claude Code

…ients

The codegen already emits a `*_SERVICE_NAME` const for each service,
and the server-side router uses it. However, generated client methods
repeated the fully-qualified service name as a string literal.

This changes `generate_client_method` to accept the const ident and
emit it in all four call sites (unary, server-stream, client-stream,
bidi), improving consistency and making rename-refactors easier.

Closes anthropics#9

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 26, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@paikend
Copy link
Copy Markdown
Contributor Author

paikend commented Mar 26, 2026

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Mar 26, 2026
@iainmcgin
Copy link
Copy Markdown
Collaborator

Thanks for the fix! I'll push the formatting fix onto your branch and merge.

Copy link
Copy Markdown
Collaborator

@iainmcgin iainmcgin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Service-name const reference brings client emission into structural symmetry with the server-side dispatcher (which already uses the same const at 4 sites). Pushed an rustfmt fix to wrap the call.

@iainmcgin iainmcgin enabled auto-merge (squash) April 2, 2026 21:19
@iainmcgin iainmcgin merged commit 5537db5 into anthropics:main Apr 2, 2026
10 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Apr 2, 2026
@paikend paikend deleted the fix/use-service-name-const-in-client branch April 3, 2026 12:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated client repeats service name literal instead of using the const

2 participants