Skip to content

[Swift] Schema IDL Codegen and Swift gRPC Integration for Apache Fory #3370

@chaokunyang

Description

@chaokunyang

This issue is reserved for gsoc, do not take over it currently.

Feature Request

Swift Schema IDL Code Generation and Swift gRPC Integration in Apache Fory

Is your feature request related to a problem? Please describe

Apache Fory has a mature compiler pipeline for FDL, Protocol Buffers, and FlatBuffers frontends, plus code generators for Java, Python, Go, Rust, and C++. The compiler also already includes service IR parsing and a --grpc generation path, but Swift code generation is not yet supported.
This project adds end-to-end Swift support in two layers:

  1. Swift schema and model code generation from Fory IR.
  2. Swift gRPC generation from service definitions, including transport bindings and a Fory-backed codec.

The implementation should follow existing compiler conventions and prioritize low-overhead, allocation-conscious runtime behavior.

Problem Statement

The repository already contains a Swift runtime (swift/Sources/Fory) but lacks compiler-generated Swift model code and Swift gRPC bindings from IDL files. This creates a gap:

  • Swift users cannot use foryc to generate model types from .fdl, .proto, or .fbs.
  • Service definitions parsed into compiler IR cannot yet target Swift transport code.
  • There is no official Fory codec integration for grpc-swift.

Why This Project Matters

  • Completes the Swift developer workflow: IDL -> generated models -> generated service APIs -> runnable gRPC client and server.
  • Reuses existing multi-frontend service parsing support in compiler IR.
  • Aligns Swift with other language targets and improves cross-language consistency.
  • Enables high-performance Swift service communication using Fory serialization semantics.

Expected Outcomes

  • Add Swift as a first-class compiler target (--lang swift, --swift_out).
  • Generate Swift model code from schema definitions (messages, enums, unions, nested types).
  • Generate service.swift and service_grpc.swift from service definitions.
  • Generate grpc-swift compatible async server and client wrappers.
  • Implement a custom grpc-swift codec backed by Fory serialization and deserialization.
  • Implement inbound zero-copy decode support with a safe copy fallback path.
  • Add golden-style codegen tests for filenames and key generated signatures.
  • Add cross-frontend parity tests for FDL, proto, and fbs service definitions.
  • Provide runnable Swift server and client example(s) using generated code and codec.
  • Document compiler usage, constraints, and integration steps.

Detailed Scope

1) Compiler and CLI Integration

  • Add SwiftGenerator under compiler/fory_compiler/generators/.
  • Register generator in compiler/fory_compiler/generators/__init__.py.
  • Extend CLI output mapping and options to support --swift_out.
  • Ensure --lang swift works with existing recursive import compilation flow.

2) Swift Model Code Generation

Generate Swift for:

  • Enums
  • Messages
  • Unions
  • Nested types
  • Type registration helper APIs

Requirements:

  • Follow Fory type ID behavior (explicit IDs, auto IDs, namespace/name registration fallback).
  • Match existing cross-language semantics where applicable.
  • Integrate with existing Swift runtime abstractions (Serializer, type resolver, registration APIs).

3) Swift Service and gRPC Code Generation

For each schema service:

  • Generate service.swift containing service protocol and method shape declarations.
  • Generate service_grpc.swift containing grpc-swift server and client transport bindings.

Required RPC support:

  • Unary
  • Client streaming
  • Server streaming
  • Bidirectional streaming

4) Fory Codec for grpc-swift

  • Implement codec encode and decode using Fory Swift runtime.
  • Ensure request and response types map correctly to generated Swift types.
  • Provide clear error mapping for decode and type mismatch failures.

5) Zero-Copy Decode and Fallback

  • Add a zero-copy-friendly decode path for inbound payload handling when safe ownership and lifecycle constraints are satisfied.
  • Add a fallback path that copies payload bytes when zero-copy cannot be safely applied.
  • Ensure behavior is deterministic and memory-safe.

6) Tests

Compiler tests:

  • Add codegen tests validating generated Swift file names and key signatures.
  • Add service generation tests for all RPC modes.
  • Add cross-frontend equivalence tests for FDL/proto/fbs service definitions.

Swift runtime and integration tests:

  • Codec round-trip tests.
  • Error-path tests (invalid payload, type mismatch, unsupported mode).
  • Zero-copy path and fallback path coverage.

7) Examples and Documentation

  • Add runnable Swift gRPC server/client example using generated files.
  • Update docs/compiler/compiler-guide.md for Swift codegen options and usage.
  • Update docs/compiler/generated-code.md with Swift output layout and generated API shape.
  • Add concise Swift integration documentation for grpc-swift + Fory codec.

Non-Goals

  • Replacing existing protobuf-native grpc-swift workflows.
  • Implementing unrelated Swift runtime features outside schema or gRPC integration scope.
  • Introducing wire compatibility with protobuf payload encoding in this task.

Performance and Quality Requirements

  • Keep allocation count low on encode and decode paths.
  • Avoid unnecessary data copies in transport integration.
  • Keep generated code predictable and stable for golden-style testing.
  • Preserve compiler behavior for existing languages and frontends.

Risks and Mitigations

  • grpc-swift API evolution risk:
    • Mitigation: pin tested grpc-swift version and document supported versions.
  • Zero-copy safety risk:
    • Mitigation: enforce strict ownership checks, test fallback path as first-class behavior.
  • Cross-frontend parity drift:
    • Mitigation: parity tests from identical service IR expectations.

Milestones (Recommended)

Community Bonding

  • Finalize generated API naming and file layout.
  • Confirm Swift option strategy and dependency constraints.
  • Agree on test matrix and acceptance checklist.

Phase 1

  • Implement Swift generator base and CLI wiring.
  • Generate core model types and registration helpers.
  • Add baseline model codegen tests.

Phase 2

  • Implement service generation (service.swift, service_grpc.swift).
  • Support unary and all streaming RPC method shapes.
  • Add service signature and transport generation tests.

Phase 3

  • Implement and validate Fory grpc-swift codec.
  • Implement zero-copy decode path and fallback path.
  • Add integration example and end-to-end tests.

Finalization

  • Documentation updates.
  • Stability pass and cleanup.
  • Final validation across compiler and Swift test suites.

Acceptance Criteria

  1. foryc supports Swift generation through --lang swift and --swift_out.
  2. Swift model code compiles and integrates with Fory Swift runtime.
  3. Service generation outputs service.swift and service_grpc.swift with correct signatures.
  4. Unary, client-streaming, server-streaming, and bidi-streaming methods are correctly generated.
  5. Fory-backed grpc-swift codec works for request and response round-trip.
  6. Zero-copy decode path exists with tested fallback behavior.
  7. Added tests pass and no regressions are introduced in existing compiler suites.
  8. Documentation and runnable Swift example are complete and usable.

Skills Required

  • Swift
  • grpc-swift
  • Compiler and code generation
  • Serialization internals
  • Async and streaming APIs
  • Testing and performance profiling

Difficulty

Medium to Hard

Project Size

Preferred: 350 hours

De-scoped 175-hour variant is possible by limiting scope to:

  • Unary RPC only
  • No bidi streaming in initial version
  • Basic codec path first, zero-copy optimization as stretch goal

Potential Mentors

  • Chaokun Yang
  • Weipeng Wang

Source Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions