
Adds open, typed polymorphism to Protocol Buffers through interface extensions; generates typed client code and runtimes, enforces extension-number rules, and yields compact on-wire representations.
Polymorphism for Protocol Buffers: protoc plugins for Kotlin and Swift, a small runtime for each, and a Gradle plugin.
Protobuf has no interfaces or inheritance. A oneof is closed: the message that declares it must
import every case, which inverts library layering and creates import cycles in recursive trees.
google.protobuf.Any is open but untyped, and costs a ~80-byte type URL per value on the wire.
ProtoMorphic adds interfaces: open, typed polymorphism across libraries.
option (protomorphic.interface) = true; with an extension range
holds exactly one implementation. Other libraries implement it by extending it, so the declaring
library never imports its implementations and third parties add implementations without changing
it. On the wire an implementation costs its field tag (2-3 bytes), not a type URL.Implementation interface.Input and Output), and the Kotlin interface is
generic, Action.Implementation<Input, Output>.Horizontal.center, Vertical.center); their Kotlin
ProtoExtensions live in a nested object per scope.Implementation interface its implementations implement, typed factories and accessors,
and an implementationNumber for dispatch.Implementation protocol per interface (abstract types become
associated types), typed factories and accessors, and implementationNumber. The same protos
generate both, so a Kotlin and a Swift client agree on the wire.option (protomorphic.library). The build fails on numbers outside a library's range, overlapping
ranges, imports of undeclared libraries, or implementations missing an interface's fields or types.protoc --encode on
JVM, iOS, Android and macOS (:conformance), and against protobuf's official conformance suite
(:conformance-suite).// ui/shape.proto
message Shape {
option (protomorphic.interface) = true;
string test_tag = 1; // abstract: every implementation declares it
extensions 100 to max;
}
// foundation/shape.proto: implements ui's Shape without ui importing it
message RoundedCornerShape { float radius_dp = 1; string test_tag = 2; }
extend ui.Shape { RoundedCornerShape rounded_corner = 1000; }val shape: Shape = RoundedCornerShape(radiusDp = 8f).toShape() // or Shape.roundedCorner(...)
shape.roundedCorner?.radiusDp // 8f
val impl: Shape.Implementation = RoundedCornerShape(testTag = "card")
impl.testTag // "card"
when (shape.implementationNumber) { ShapeExtensions.roundedCorner.number -> ... }let shape: Ui_Shape = Foundation_RoundedCornerShape(radiusDp: 8).toShape() // or .roundedCorner(...)
shape.roundedCorner?.radiusDp // 8
let impl: any Ui_Shape.Implementation = Foundation_RoundedCornerShape(testTag: "card")
impl.testTag // "card"
switch shape.implementationNumber { case Foundation_ShapeExtensions.roundedCorner.number: ... }Swift has one namespace per module, so Swift types carry the file's swift_prefix, or else a
prefix derived from its package (ui -> Ui_), as protoc-gen-swift names them.
plugins {
kotlin("multiplatform")
id("studio.dabble.protomorphic") version "0.1.0"
}
protomorphic {
// Default: src/commonMain/proto (src/main/proto for Kotlin/JVM).
// sourceDir = layout.projectDirectory.dir("src/commonMain/proto")
// Proto roots of dependencies, read for imports only:
// includes.from(project(":other").file("src/commonMain/proto"))
}The plugin and its artifacts are on Maven Central, so mavenCentral() must be in both
pluginManagement and dependencyResolutionManagement repositories.
protomorphic/options.proto is on the proto path automatically.
The same Gradle plugin generates Swift, with ./gradlew generateProtomorphicSwift:
protomorphic {
// Default: build/generated/protomorphic-swift. The task replaces the directory's contents.
swiftOutputDir = rootProject.layout.projectDirectory.dir("swift/Sources/Contracts/Generated/ui")
}The Swift target that compiles the output depends on the runtime in this repository's Swift package:
.package(url: "https://github.com/DabbleStudios/protomorphic.git", exact: "0.2.0"),
// ...
.target(name: "Contracts", dependencies: [.product(name: "ProtoMorphic", package: "protomorphic")]),Well-known types map to the runtime's Swift types: AnyMessage, ProtoDuration, ProtoTimestamp,
ProtoEmpty, ProtoValue (a google.protobuf.Struct is a [String: ProtoValue]), and optionals of
Swift scalars for the wrapper types.
| Module | Artifact | Contents |
|---|---|---|
rules |
studio.dabble.protomorphic:rules |
The schema rules and the language-independent schema model (JVM) |
protoc-gen-protomorphic-kotlin |
studio.dabble.protomorphic:protoc-gen-protomorphic-kotlin |
The Kotlin generator: a protoc plugin that checks the rules and generates Kotlin (JVM) |
protoc-gen-protomorphic-swift |
studio.dabble.protomorphic:protoc-gen-protomorphic-swift |
The Swift generator: a protoc plugin that checks the rules and generates Swift (JVM) |
runtime |
studio.dabble.protomorphic:runtime |
ProtoExtension, ProtoExtensions, packAny (KMP) |
swift/ |
Swift package ProtoMorphic (Package.swift) |
The Swift runtime: wire format, ProtoMessage, extensions, ProtoFields, well-known types |
gradle-plugin |
plugin studio.dabble.protomorphic
|
protoc + generator wiring, protomorphic/options.proto
|
conformance |
- | Tests against protoc --encode, in Kotlin and in Swift (conformance/swift) |
conformance-suite |
- | Testees for protobuf's official conformance suite (binary proto3 and editions), in Kotlin and in Swift (conformance-suite/swift); known failures in failure_list.txt and failure_list_swift.txt
|
Push a v<version> tag. The release workflow sets the version from the tag and publishes every
artifact to Maven Central, signed. Swift Package Manager resolves the Swift runtime from the same tag.
Apache License 2.0. See LICENSE.
Polymorphism for Protocol Buffers: protoc plugins for Kotlin and Swift, a small runtime for each, and a Gradle plugin.
Protobuf has no interfaces or inheritance. A oneof is closed: the message that declares it must
import every case, which inverts library layering and creates import cycles in recursive trees.
google.protobuf.Any is open but untyped, and costs a ~80-byte type URL per value on the wire.
ProtoMorphic adds interfaces: open, typed polymorphism across libraries.
option (protomorphic.interface) = true; with an extension range
holds exactly one implementation. Other libraries implement it by extending it, so the declaring
library never imports its implementations and third parties add implementations without changing
it. On the wire an implementation costs its field tag (2-3 bytes), not a type URL.Implementation interface.Input and Output), and the Kotlin interface is
generic, Action.Implementation<Input, Output>.Horizontal.center, Vertical.center); their Kotlin
ProtoExtensions live in a nested object per scope.Implementation interface its implementations implement, typed factories and accessors,
and an implementationNumber for dispatch.Implementation protocol per interface (abstract types become
associated types), typed factories and accessors, and implementationNumber. The same protos
generate both, so a Kotlin and a Swift client agree on the wire.option (protomorphic.library). The build fails on numbers outside a library's range, overlapping
ranges, imports of undeclared libraries, or implementations missing an interface's fields or types.protoc --encode on
JVM, iOS, Android and macOS (:conformance), and against protobuf's official conformance suite
(:conformance-suite).// ui/shape.proto
message Shape {
option (protomorphic.interface) = true;
string test_tag = 1; // abstract: every implementation declares it
extensions 100 to max;
}
// foundation/shape.proto: implements ui's Shape without ui importing it
message RoundedCornerShape { float radius_dp = 1; string test_tag = 2; }
extend ui.Shape { RoundedCornerShape rounded_corner = 1000; }val shape: Shape = RoundedCornerShape(radiusDp = 8f).toShape() // or Shape.roundedCorner(...)
shape.roundedCorner?.radiusDp // 8f
val impl: Shape.Implementation = RoundedCornerShape(testTag = "card")
impl.testTag // "card"
when (shape.implementationNumber) { ShapeExtensions.roundedCorner.number -> ... }let shape: Ui_Shape = Foundation_RoundedCornerShape(radiusDp: 8).toShape() // or .roundedCorner(...)
shape.roundedCorner?.radiusDp // 8
let impl: any Ui_Shape.Implementation = Foundation_RoundedCornerShape(testTag: "card")
impl.testTag // "card"
switch shape.implementationNumber { case Foundation_ShapeExtensions.roundedCorner.number: ... }Swift has one namespace per module, so Swift types carry the file's swift_prefix, or else a
prefix derived from its package (ui -> Ui_), as protoc-gen-swift names them.
plugins {
kotlin("multiplatform")
id("studio.dabble.protomorphic") version "0.1.0"
}
protomorphic {
// Default: src/commonMain/proto (src/main/proto for Kotlin/JVM).
// sourceDir = layout.projectDirectory.dir("src/commonMain/proto")
// Proto roots of dependencies, read for imports only:
// includes.from(project(":other").file("src/commonMain/proto"))
}The plugin and its artifacts are on Maven Central, so mavenCentral() must be in both
pluginManagement and dependencyResolutionManagement repositories.
protomorphic/options.proto is on the proto path automatically.
The same Gradle plugin generates Swift, with ./gradlew generateProtomorphicSwift:
protomorphic {
// Default: build/generated/protomorphic-swift. The task replaces the directory's contents.
swiftOutputDir = rootProject.layout.projectDirectory.dir("swift/Sources/Contracts/Generated/ui")
}The Swift target that compiles the output depends on the runtime in this repository's Swift package:
.package(url: "https://github.com/DabbleStudios/protomorphic.git", exact: "0.2.0"),
// ...
.target(name: "Contracts", dependencies: [.product(name: "ProtoMorphic", package: "protomorphic")]),Well-known types map to the runtime's Swift types: AnyMessage, ProtoDuration, ProtoTimestamp,
ProtoEmpty, ProtoValue (a google.protobuf.Struct is a [String: ProtoValue]), and optionals of
Swift scalars for the wrapper types.
| Module | Artifact | Contents |
|---|---|---|
rules |
studio.dabble.protomorphic:rules |
The schema rules and the language-independent schema model (JVM) |
protoc-gen-protomorphic-kotlin |
studio.dabble.protomorphic:protoc-gen-protomorphic-kotlin |
The Kotlin generator: a protoc plugin that checks the rules and generates Kotlin (JVM) |
protoc-gen-protomorphic-swift |
studio.dabble.protomorphic:protoc-gen-protomorphic-swift |
The Swift generator: a protoc plugin that checks the rules and generates Swift (JVM) |
runtime |
studio.dabble.protomorphic:runtime |
ProtoExtension, ProtoExtensions, packAny (KMP) |
swift/ |
Swift package ProtoMorphic (Package.swift) |
The Swift runtime: wire format, ProtoMessage, extensions, ProtoFields, well-known types |
gradle-plugin |
plugin studio.dabble.protomorphic
|
protoc + generator wiring, protomorphic/options.proto
|
conformance |
- | Tests against protoc --encode, in Kotlin and in Swift (conformance/swift) |
conformance-suite |
- | Testees for protobuf's official conformance suite (binary proto3 and editions), in Kotlin and in Swift (conformance-suite/swift); known failures in failure_list.txt and failure_list_swift.txt
|
Push a v<version> tag. The release workflow sets the version from the tag and publishes every
artifact to Maven Central, signed. Swift Package Manager resolves the Swift runtime from the same tag.
Apache License 2.0. See LICENSE.