我对刀刀和原始人很陌生。
我试着为我的刀刀的发电机创建单元测试。我已经生成了proto文件,并试图在测试中使用它们。
我有这个proto文件:
syntax = "proto3";
package hellogrpc.calc;
import "google/api/annotations.proto";
option (scalapb.options) = {
flat_package: true
};
service CalcService {
rpc CalcSum (SumRequest) returns (CalcResponse) {
option (google.api.http) = {
post: "/calcService/sum"
body: "*"
};
}
}有一个方法CalcSum被注释。
以及相应生成的proto文件:
// Generated by the Scala Plugin for the Protocol Buffer Compiler.
// Do not edit!
//
// Protofile syntax: PROTO3
package hellogrpc.calc
object CalcServiceProto extends _root_.com.trueaccord.scalapb.GeneratedFileObject {
lazy val dependencies: Seq[_root_.com.trueaccord.scalapb.GeneratedFileObject] = Seq(
com.trueaccord.scalapb.scalapb.ScalapbProto,
com.google.api.annotations.AnnotationsProto
)
lazy val messagesCompanions: Seq[_root_.com.trueaccord.scalapb.GeneratedMessageCompanion[_]] = Seq(
hellogrpc.calc.SumRequest,
hellogrpc.calc.CalcResponse
)
private lazy val ProtoBytes: Array[Byte] =
com.trueaccord.scalapb.Encoding.fromBase64(scala.collection.Seq(
"""ChtoZWxsb2dycGMvQ2FsY1NlcnZpY2UucHJvdG8SDmhlbGxvZ3JwYy5jYWxjGhVzY2FsYXBiL3NjYWxhcGIucHJvdG8aHGdvb
2dsZS9hcGkvYW5ub3RhdGlvbnMucHJvdG8iKAoKU3VtUmVxdWVzdBIMCgFhGAEgASgFUgFhEgwKAWIYAiABKAVSAWIiJgoMQ2FsY
1Jlc3BvbnNlEhYKBnJlc3VsdBgBIAEoBVIGcmVzdWx0Mm8KC0NhbGNTZXJ2aWNlEmAKB0NhbGNTdW0SGi5oZWxsb2dycGMuY2FsY
y5TdW1SZXF1ZXN0GhwuaGVsbG9ncnBjLmNhbGMuQ2FsY1Jlc3BvbnNlIhuC0+STAhUiEC9jYWxjU2VydmljZS9zdW06ASpCBeI/A
hABYgZwcm90bzM="""
).mkString)
lazy val scalaDescriptor: _root_.scalapb.descriptors.FileDescriptor = {
val scalaProto = com.google.protobuf.descriptor.FileDescriptorProto.parseFrom(ProtoBytes)
_root_.scalapb.descriptors.FileDescriptor.buildFrom(scalaProto, dependencies.map(_.scalaDescriptor))
}
lazy val javaDescriptor: com.google.protobuf.Descriptors.FileDescriptor = {
val javaProto = com.google.protobuf.DescriptorProtos.FileDescriptorProto.parseFrom(ProtoBytes)
com.google.protobuf.Descriptors.FileDescriptor.buildFrom(javaProto, Array(
com.trueaccord.scalapb.scalapb.ScalapbProto.javaDescriptor,
com.google.api.annotations.AnnotationsProto.javaDescriptor
))
}
@deprecated("Use javaDescriptor instead. In a future version this will refer to scalaDescriptor.", "ScalaPB 0.5.47")
def de
```scriptor: com.google.protobuf.Descriptors.FileDescriptor = javaDescriptor}
I inspect CalcServiceProto.javaDescriptor in intellj idea:

Method descriptor has this proto definition:
```javascript名称:"CalcSum“
input_type:".hellogrpc.calc.SumRequest“
output_type:".hellogrpc.calc.CalcResponse“
选项{
72295728:“\\”\020/calcService/sum:\001*“
}
But generator works just fine! I debug generator, toggled breakpoint on generator and method CalcSum has this proto definition:
```javascript名称:"CalcSum“
input_type:".hellogrpc.calc.SumRequest“
output_type:".hellogrpc.calc.CalcResponse“
选项{
google.api.http {
post: "/calcService/sum"body: "*"}
}
May be this works differently because i didn't register extensions like generator do.
Any way i want this test to be passed:
```javascriptval = CalcServiceProto.javaDescriptor.getServices.get(0)
val = s.getMethods.get(0)
m.getOptions.getExtension(AnnotationsProto.http).getPost shouldBe“/煅烧服务/sum”
发布于 2017-12-30 02:48:13
如果需要Java扩展可用,则需要在启用Java转换的情况下生成代码。这将使javaDescriptor依赖于官方的Java实现,您的测试将通过。
当禁用Java转换时,ScalaPB解析描述符,但它不能保证编译了Java扩展,因此它不会尝试注册它们。
我想要的是Scala描述符在这种情况下可以工作,但是它们还不支持服务和方法。我注册了https://github.com/scalapb/ScalaPB/issues/382来跟踪这方面的进展。
同时,正如我前面所写的,您可以使用java转换来强制ScalaPB为您提供Java描述符。在您的build.sbt中,有:
PB.targets in Compile := Seq(
scalapb.gen(grpc=true, javaConversions=true) -> (sourceManaged in Compile).value,
PB.gens.java -> (sourceManaged in Compile).value
)https://stackoverflow.com/questions/48029470
复制相似问题