-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a code gen that supports metadata
- Loading branch information
Showing
18 changed files
with
505 additions
and
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ target/ | |
project/target/ | ||
*.class | ||
|
||
.bloop | ||
.metals | ||
metals.sbt |
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
53 changes: 53 additions & 0 deletions
53
code-gen/src/main/scala/scalapb/grpcweb/GrpcWebCodeGenerator.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,53 @@ | ||
package scalapb.grpcweb | ||
|
||
import com.google.protobuf.Descriptors.FileDescriptor | ||
import com.google.protobuf.ExtensionRegistry | ||
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse | ||
import protocbridge.codegen.{CodeGenApp, CodeGenRequest, CodeGenResponse} | ||
import scalapb.compiler._ | ||
import scalapb.options.compiler.Scalapb | ||
import scala.jdk.CollectionConverters._ | ||
|
||
object GrpcWebCodeGenerator extends CodeGenApp { | ||
override def registerExtensions(registry: ExtensionRegistry): Unit = | ||
Scalapb.registerAllExtensions(registry) | ||
|
||
def process(request: CodeGenRequest): CodeGenResponse = { | ||
ProtobufGenerator.parseParameters(request.parameter) match { | ||
case Right(params) => | ||
try { | ||
val implicits = | ||
new DescriptorImplicits(params, request.allProtos) | ||
val generatedFiles = request.filesToGenerate.flatMap { file => | ||
generateServiceFiles(file, implicits) | ||
} | ||
CodeGenResponse.succeed( | ||
generatedFiles | ||
) | ||
} catch { | ||
case e: GeneratorException => | ||
CodeGenResponse.fail(e.message) | ||
} | ||
case Left(error) => | ||
CodeGenResponse.fail(error) | ||
} | ||
|
||
} | ||
|
||
private def generateServiceFiles( | ||
file: FileDescriptor, | ||
implicits: DescriptorImplicits | ||
): Seq[CodeGeneratorResponse.File] = { | ||
import implicits._ | ||
file.getServices.asScala.map { service => | ||
val p = new GrpcWebServicePrinter(service, implicits) | ||
val code = p.printService(FunctionalPrinter()).result() | ||
CodeGeneratorResponse.File | ||
.newBuilder() | ||
.setName(p.scalaFileName) | ||
.setContent(code) | ||
.build | ||
}.toSeq | ||
} | ||
|
||
} |
Oops, something went wrong.