-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[transcoding] Better message merging syntax/implementation
- Loading branch information
Showing
4 changed files
with
63 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
core/src/main/scala/org/ivovk/connect_rpc_scala/grpc/MergingBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.ivovk.connect_rpc_scala.grpc | ||
|
||
import com.google.protobuf.ByteString | ||
import scalapb.{GeneratedMessage as Message, GeneratedMessageCompanion as Companion} | ||
|
||
object MergingBuilder { | ||
extension [T <: Message](t: T) { | ||
def merge(other: T)(using Companion[T]): MergingBuilder[T] = | ||
new SingleMessageMergingBuilder(t).merge(other) | ||
} | ||
} | ||
|
||
sealed trait MergingBuilder[T <: Message] { | ||
def merge(other: T): MergingBuilder[T] | ||
|
||
def build: T | ||
} | ||
|
||
private class SingleMessageMergingBuilder[T <: Message](t: T)(using cmp: Companion[T]) extends MergingBuilder[T] { | ||
override def merge(other: T): MergingBuilder[T] = { | ||
val empty = cmp.defaultInstance | ||
|
||
if other == empty then this | ||
else if t == empty then SingleMessageMergingBuilder(other) | ||
else ListMergingBuilder(other :: t :: Nil) | ||
} | ||
|
||
override def build: T = t | ||
} | ||
|
||
private class ListMergingBuilder[T <: Message](ts: List[T])(using cmp: Companion[T]) extends MergingBuilder[T] { | ||
override def merge(other: T): MergingBuilder[T] = { | ||
val empty = cmp.defaultInstance | ||
|
||
if other == empty then this | ||
else ListMergingBuilder(other :: ts) | ||
} | ||
|
||
override def build: T = { | ||
val output = ByteString.newOutput(ts.foldLeft(0)(_ + _.serializedSize)) | ||
ts.reverse.foreach(_.writeTo(output)) | ||
output.close() | ||
cmp.parseFrom(output.toByteString.newCodedInput()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
core/src/test/scala/org/ivovk/connect_rpc_scala/grpc/MergingBuilderTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.ivovk.connect_rpc_scala.grpc | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import test.HttpCommunicationTest.AddRequest | ||
|
||
class MergingBuilderTest extends AnyFunSuite { | ||
import MergingBuilder.* | ||
|
||
test("merges three messages") { | ||
val merge = AddRequest(a = 1).merge(AddRequest(b = 2)).merge(AddRequest(a = 3)).build | ||
|
||
assert(merge.a == 3) | ||
assert(merge.b == 2) | ||
} | ||
} |