-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for derives clause for messages and sealed oneofs.
For #1584
- Loading branch information
Showing
11 changed files
with
216 additions
and
47 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
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
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
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,24 @@ | ||
syntax = "proto3"; | ||
|
||
package com.thesamet.proto.e2e.derives; | ||
|
||
import "scalapb/scalapb.proto"; | ||
|
||
message Foo { | ||
option (scalapb.message).derives = "scalapb.derives.Show"; | ||
option (scalapb.message).derives = "scalapb.derives.TC"; | ||
|
||
int32 a = 1; | ||
string b = 2; | ||
} | ||
|
||
message M1 {} | ||
message M2 {} | ||
|
||
message Expr { | ||
option (scalapb.message).sealed_oneof_derives = "scalapb.derives.Show"; | ||
oneof sealed_value { | ||
M1 m1 = 1; | ||
M2 m2 = 2; | ||
} | ||
} |
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,24 @@ | ||
syntax = "proto3"; | ||
|
||
package com.thesamet.proto.e2e.derives; | ||
|
||
import "scalapb/scalapb.proto"; | ||
|
||
message Foo { | ||
option (scalapb.message).derives = "scalapb.derives.Show"; | ||
option (scalapb.message).derives = "scalapb.derives.TC"; | ||
|
||
int32 a = 1; | ||
string b = 2; | ||
} | ||
|
||
message M1 {} | ||
message M2 {} | ||
|
||
message Expr { | ||
option (scalapb.message).sealed_oneof_derives = "scalapb.derives.Show"; | ||
oneof sealed_value { | ||
M1 m1 = 1; | ||
M2 m2 = 2; | ||
} | ||
} |
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,21 @@ | ||
package scalapb.derives | ||
|
||
import scalapb.{GeneratedMessage, GeneratedSealedOneof} | ||
import scala.compiletime.erasedValue | ||
import scala.annotation.targetName | ||
|
||
trait Show[T]: | ||
def show(t: T): String | ||
|
||
object Show: | ||
@targetName("derivedMessage") def derived[T <: GeneratedMessage]: Show[T] = new Show[T]: | ||
def show(t: T): String = t.toProtoString | ||
|
||
@targetName("derivedSealedOneof") def derived[T <: GeneratedSealedOneof]: Show[T] = new Show[T]: | ||
def show(t: T): String = "Sealed!" | ||
|
||
trait TC[T]: | ||
def tc(t: T): Unit | ||
|
||
object TC: | ||
def derived[T <: GeneratedMessage]: TC[T] = null |
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,21 @@ | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.must.Matchers | ||
import org.scalatest.OptionValues | ||
import scalapb.derives.{Show, TC} | ||
import com.thesamet.proto.e2e.derives.cases | ||
|
||
class DerivesSpec extends AnyFlatSpec with Matchers with OptionValues: | ||
"Show typeclass" should "be summonable for derives.foo" in: | ||
val s = summon[Show[cases.Foo]] | ||
s.show(cases.Foo(3,"xyz")) must be( | ||
"""a: 3 | ||
|b: "xyz" | ||
|""".stripMargin) | ||
|
||
"TC typeclass" should "be summonable for derives.foo and be null" in: | ||
val s = summon[TC[cases.Foo]] | ||
s must be(null) | ||
|
||
"Show" should "return sealed for Expr" in: | ||
val s = summon[Show[cases.Expr]] | ||
s.show(cases.M1()) must be("Sealed!") |
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,12 @@ | ||
import com.thesamet.proto.e2e.scala3.issue1576.Foo | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.must.Matchers | ||
import org.scalatest.OptionValues | ||
import scalapb.lenses.{MessageLens, ObjectLens} | ||
|
||
class Scala3CompatSpec extends AnyFlatSpec with Matchers with OptionValues { | ||
"message lens" should "extend MessageLens and ObjectLens" in { | ||
classOf[MessageLens[_, _]].isAssignableFrom(classOf[Foo.FooLens[_]]) must be(true) | ||
classOf[ObjectLens[_, _]].isAssignableFrom(classOf[Foo.FooLens[_]]) must be(true) | ||
} | ||
} |
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
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
Oops, something went wrong.