rpc_dart_generator
Code generator for caller/responder wrappers in rpc_dart.
- Generates
Names(service + method constants,Names.instance(suffix)for multiple instances). - Generates a
finalcaller and anabstractresponder (you implement handlers;setup()registers them). - Codecs: by default inserts
RpcCodec<T>.withDecoder(T.fromJson); iftransferModeiszeroCopyno codecs/serialization checks are added. - Cooperative with other
source_genbuilders: usesSharedPartBuilder/combining_builderto merge into the same*.g.dartinstead of writing it directly.
Annotations
@RpcService(name, transferMode?)— declares service name and default transfer mode (auto/codec/zeroCopy).@RpcMethod(name, kind, requestCodec?, responseCodec?, transferMode?)— method id, RPC kind, optional codecs and per-method transfer mode.
Per-method transferMode overrides the service. In zeroCopy codecs are omitted and IRpcSerializable check is skipped. In auto/codec, if codecs are not provided, RpcCodec<T>.withDecoder(T.fromJson) is added (requires static fromJson).
Example (zero-copy, multiple instances)
See example/lib/calculator_contract.dart and example/bin/main.dart:
responderEndpoint.registerServiceContract(
CalculatorContractResponder(
serviceNameOverride: CalculatorContractNames.instance('alpha'),
),
);
responderEndpoint.registerServiceContract(
CalculatorContractResponder(
serviceNameOverride: CalculatorContractNames.instance('beta'),
),
);
final caller = CalculatorContractCaller(
RpcCallerEndpoint(transport: callerTransport),
serviceNameOverride: CalculatorContractNames.instance('beta'),
);
final res = await caller.sum(SumRequest(values: [1, 2, 3]));
Example (auto codec)
example/lib/calculator_with_codec.dart:
@RpcService(name: 'CalculatorCodec')
abstract class ICalculatorCodecContract {
@RpcMethod(name: 'sum')
Future<SumResponse> sum(SumRequest request, {RpcContext? context});
}
example/bin/main_codec.dart registers under Names.instance('json') and calls it.
Generate
fvm dart pub get
fvm dart run build_runner build
The builder emits .rpc_dart.g.part fragments to the build cache; source_gen|combining_builder produces the final *.g.dart, allowing rpc_dart output to live alongside other generators (e.g. json_serializable) without collisions.
Transfer modes
auto/codec: if codecs are absent → defaultRpcCodec<T>.withDecoder(T.fromJson); if provided → use them.zeroCopy: no codecs generated, noIRpcSerializablecheck (anyObject), intended for zero-copy transports.
Codec/transfer matrix
| Service transfer | Method transfer | Explicit codecs | Generated codecs | Serialization check |
|---|---|---|---|---|
| auto | (inherit) | none | RpcCodec<T>.withDecoder |
yes |
| auto | (inherit) | set | provided codecs | yes |
| codec | (inherit) | none | RpcCodec<T>.withDecoder |
yes |
| codec | (inherit) | set | provided codecs | yes |
| zeroCopy | (inherit) | any/none | none | no |
| any | zeroCopy | any/none | none | no |
| any | auto/codec | none | RpcCodec<T>.withDecoder |
yes |
| any | auto/codec | set | provided codecs | yes |
Validation
- Signatures must match
kind: unary →Future<T> func(T req, {RpcContext? context}); streams → correspondingStream/Future. RpcContextis the only allowed named parameter.- In
auto/codec, types must be serializable (IRpcSerializableor primitive), otherwise generation fails.