Skip to content

Commit

Permalink
Merge pull request #317 from soundcloud/support-proto3-optional
Browse files Browse the repository at this point in the history
Add support for 'optional' in proto3 files
  • Loading branch information
ccmtaylor authored Feb 15, 2022
2 parents 2b369d0 + a1e8a16 commit 87c2991
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ Please ensure that `sbt scalafmtCheckAll +test scripted` passes when submitting

* IntelliJ [doesn't run plugins][intellij] during project build. Before importing,
`sbt compile` may be necessary.

* In order to run the full test suite (i.e. the unit tests & the end-to-end tests
for code-generation) use `sbt +test scripted`
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ object ServerClientCodeGenerator extends CodeGenApp {
for {
file <- request.filesToGenerate
serviceFile <- generateServiceFiles(file, implicits)
} yield serviceFile
} yield serviceFile,
supportedFeatures = Set(CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL)
)
case Left(error) =>
CodeGenResponse.fail(error)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// A small test case for support of optional fields in proto3.

syntax = "proto3";

package proto.test;

message IHaveOptionalFields {
optional int32 maybe_int = 1;
optional string maybe_string = 2;
}

service OptionalFieldsTest {
rpc DoSomething(IHaveOptionalFields) returns (IHaveOptionalFields);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package proto.test

import com.twitter.util.Future
import com.soundcloud.twinagle.ServerBuilder

import org.specs2.mutable.Specification

class OptionalFieldsSpec extends Specification {
// if this compiles, we're good
"Proto files with optional scalar values are generated correctly" in {
val svc = new OptionalFieldsTestService {
override def doSomething(req: IHaveOptionalFields): Future[IHaveOptionalFields] = {
iTakeAnOptionInt(req.maybeInt)
iTakeAnOptionString(req.maybeString)
Future.value(req)
}

@annotation.nowarn
private def iTakeAnOptionInt[T](a: T)(implicit ev: T =:= Option[Int]): Unit = ()

@annotation.nowarn
private def iTakeAnOptionString[T](a: T)(implicit ev: T =:= Option[String]): Unit = ()
}

val httpService = ServerBuilder()
.register(svc)
.build

new OptionalFieldsTestClientProtobuf(httpService)
new OptionalFieldsTestClientJson(httpService)
ok
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HaberdasherSpec extends Specification {
Hat(
size = size.inches,
color = "brown",
name = "bowler"
name = "bowler",
)
)
} else {
Expand Down

0 comments on commit 87c2991

Please sign in to comment.