Skip to content

Commit d6c7748

Browse files
committed
testing
1 parent ecc5659 commit d6c7748

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Current status: 11/79 tests pass.
154154

155155
Known issues:
156156

157-
* `google.protobuf.Any` serialization doesn't follow Connect-RPC
157+
* `google.protobuf.Any` serialization [doesn't follow](https://github.com/connectrpc/conformance/issues/948) Connect-RPC
158158
spec: [#32](https://github.com/igor-vovk/connect-rpc-scala/issues/32)
159159

160160
## Future improvements

core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class ConnectHandler[F[_] : Async](
180180
Response[F](httpStatus).withEntity(connectrpc.Error(
181181
code = connectCode,
182182
message = messageWithDetails.map(_._1),
183-
details = Seq.empty // details
183+
details = details
184184
))
185185
}
186186
}

core/src/main/scala/org/ivovk/connect_rpc_scala/Mappings.scala

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.ivovk.connect_rpc_scala
22

3+
import com.google.protobuf.struct.{ListValue, NullValue, Struct, Value}
34
import io.grpc.{Metadata, Status}
45
import org.http4s.{Header, Headers}
56
import org.typelevel.ci.CIString
67
import scalapb.GeneratedMessage
8+
import scalapb.descriptors.*
79

8-
object Mappings extends HeaderMappings, StatusCodeMappings, AnyMappings
10+
object Mappings extends HeaderMappings, StatusCodeMappings, ProtoMappings
911

1012
trait HeaderMappings {
1113

@@ -103,14 +105,35 @@ trait StatusCodeMappings {
103105

104106
}
105107

106-
trait AnyMappings {
108+
trait ProtoMappings {
107109

108110
extension [T <: GeneratedMessage](t: T) {
109111
def toProtoAny: com.google.protobuf.any.Any =
110112
com.google.protobuf.any.Any(
111113
typeUrl = "type.googleapis.com/" + t.companion.scalaDescriptor.fullName,
112114
value = t.toByteString
113115
)
116+
117+
def toProtoStruct: Struct = toValue(t.toPMessage).kind match {
118+
case Value.Kind.StructValue(struct) => struct
119+
case _ => throw new IllegalArgumentException("Expected a struct value")
120+
}
121+
}
122+
123+
def toValue(value: PValue): Value = {
124+
value match {
125+
case PEmpty => Value.of(Value.Kind.NullValue(NullValue.NULL_VALUE))
126+
case PInt(value) => Value.of(Value.Kind.NumberValue(value.toDouble))
127+
case PLong(value) => Value.of(Value.Kind.NumberValue(value.toDouble))
128+
case PString(value) => Value.of(Value.Kind.StringValue(value))
129+
case PDouble(value) => Value.of(Value.Kind.NumberValue(value))
130+
case PFloat(value) => Value.of(Value.Kind.NumberValue(value.toDouble))
131+
case PByteString(value) => Value.of(Value.Kind.StringValue(value.toStringUtf8))
132+
case PBoolean(value) => Value.of(Value.Kind.BoolValue(value))
133+
case PEnum(value) => Value.of(Value.Kind.StringValue(value.index.toString))
134+
case PMessage(value) => Value.of(Value.Kind.StructValue(Struct(value.map((k, v) => k.name -> toValue(v)))))
135+
case PRepeated(value) => Value.of(Value.Kind.ListValue(ListValue(value.map(toValue))))
136+
}
114137
}
115138

116139
}

core/src/main/scala/org/ivovk/connect_rpc_scala/http/ConnectAnyFormat.scala renamed to core/src/main/scala/org/ivovk/connect_rpc_scala/http/json/ConnectAnyFormat.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.ivovk.connect_rpc_scala.http
1+
package org.ivovk.connect_rpc_scala.http.json
22

33
import com.google.protobuf.any.Any as PBAny
44
import org.json4s.JsonAST.{JObject, JString, JValue}

0 commit comments

Comments
 (0)