Skip to content

Commit ab4c608

Browse files
authored
[transcoding] Switch to ScalaPB's HttpRule definitions to fix CNF errors (#60)
1 parent c24cd23 commit ab4c608

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

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

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

33
import cats.implicits.*
4-
import com.google.api.HttpRule
4+
import com.google.api.http.{CustomHttpPattern, HttpRule}
55
import org.http4s.{Method, Request, Uri}
66
import org.ivovk.connect_rpc_scala
77
import org.ivovk.connect_rpc_scala.grpc.MethodRegistry
@@ -60,7 +60,7 @@ object TranscodingUrlMatcher {
6060
Node(
6161
variableDef,
6262
segment,
63-
mkTree(entries.map(e => e.copy(pattern = e.pattern.splitAt(1)._2)).toVector),
63+
mkTree(entries.map(e => e.copy(pattern = e.pattern.splitAt(1)._2))),
6464
)
6565
)
6666
}
@@ -74,7 +74,7 @@ object TranscodingUrlMatcher {
7474
val result = collection.mutable.LinkedHashMap.empty[B, Vector[A]]
7575

7676
it.foreach { elem =>
77-
val key = f(elem)
77+
val key = f(elem)
7878
val vec = result.getOrElse(key, Vector.empty)
7979
result.update(key, vec :+ elem)
8080
}
@@ -106,7 +106,7 @@ object TranscodingUrlMatcher {
106106
): TranscodingUrlMatcher[F] = {
107107
val entries = methods.flatMap { method =>
108108
method.httpRule.fold(List.empty[Entry]) { httpRule =>
109-
val additionalBindings = httpRule.getAdditionalBindingsList.asScala.toList
109+
val additionalBindings = httpRule.additionalBindings.toList
110110

111111
(httpRule :: additionalBindings).map { rule =>
112112
val (httpMethod, pattern) = extractMethodAndPattern(rule)
@@ -126,13 +126,13 @@ object TranscodingUrlMatcher {
126126
}
127127

128128
private def extractMethodAndPattern(rule: HttpRule): (Option[Method], Uri.Path) = {
129-
val (method, str) = rule.getPatternCase match
130-
case HttpRule.PatternCase.GET => (Method.GET.some, rule.getGet)
131-
case HttpRule.PatternCase.PUT => (Method.PUT.some, rule.getPut)
132-
case HttpRule.PatternCase.POST => (Method.POST.some, rule.getPost)
133-
case HttpRule.PatternCase.DELETE => (Method.DELETE.some, rule.getDelete)
134-
case HttpRule.PatternCase.PATCH => (Method.PATCH.some, rule.getPatch)
135-
case HttpRule.PatternCase.CUSTOM => (none, rule.getCustom.getPath)
129+
val (method, str) = rule.pattern match
130+
case HttpRule.Pattern.Get(value) => (Method.GET.some, value)
131+
case HttpRule.Pattern.Put(value) => (Method.PUT.some, value)
132+
case HttpRule.Pattern.Post(value) => (Method.POST.some, value)
133+
case HttpRule.Pattern.Delete(value) => (Method.DELETE.some, value)
134+
case HttpRule.Pattern.Patch(value) => (Method.PATCH.some, value)
135+
case HttpRule.Pattern.Custom(CustomHttpPattern(kind, value, _)) if kind == "*" => (none, value)
136136
case other => throw new RuntimeException(s"Unsupported pattern case $other (Rule: $rule)")
137137

138138
val path = Uri.Path.unsafeFromString(str).dropEndsWithSlash
@@ -169,13 +169,13 @@ class TranscodingUrlMatcher[F[_]](
169169
JObject(groupFields(pathVars)),
170170
JObject(groupFields(queryParams))
171171
).some
172-
case RootNode(children) =>
173-
children.colFirst(doMatch(_, path, pathVars))
174172
case _ => none
175173
}
176174
}
177175

178-
doMatch(tree, req.uri.path.segments.toList, List.empty)
176+
val path = req.uri.path.segments.toList
177+
178+
tree.children.colFirst(doMatch(_, path, Nil))
179179
}
180180

181181
}

core/src/main/scala/org/ivovk/connect_rpc_scala/grpc/MethodRegistry.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.ivovk.connect_rpc_scala.grpc
22

3-
import com.google.api.{AnnotationsProto, HttpRule}
3+
import com.google.api.http.HttpRule
44
import io.grpc.{MethodDescriptor, ServerMethodDefinition, ServerServiceDefinition}
55
import scalapb.grpc.ConcreteProtoMethodDescriptorSupplier
66
import scalapb.{GeneratedMessage, GeneratedMessageCompanion}
@@ -47,16 +47,19 @@ object MethodRegistry {
4747
new MethodRegistry(entries)
4848
}
4949

50+
private val HttpFieldNumber = 72295728
51+
5052
private def extractHttpRule(methodDescriptor: MethodDescriptor[_, _]): Option[HttpRule] = {
5153
methodDescriptor.getSchemaDescriptor match
5254
case sd: ConcreteProtoMethodDescriptorSupplier =>
5355
val fields = sd.getMethodDescriptor.getOptions.getUnknownFields
54-
val fieldNumber = AnnotationsProto.http.getNumber
5556

56-
if fields.hasField(fieldNumber) then
57-
Some(HttpRule.parseFrom(fields.getField(fieldNumber).getLengthDelimitedList.get(0).toByteArray))
58-
else None
59-
case _ => None
57+
if fields.hasField(HttpFieldNumber) then
58+
Some(HttpRule.parseFrom(fields.getField(HttpFieldNumber).getLengthDelimitedList.get(0).toByteArray))
59+
else
60+
None
61+
case _ =>
62+
None
6063
}
6164

6265
}

core/src/test/scala/org/ivovk/connect_rpc_scala/TranscodingUrlMatcherTest.scala

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

33
import cats.effect.IO
4-
import com.google.api.HttpRule
4+
import com.google.api.http.HttpRule
55
import org.http4s.Uri.Path.Root
66
import org.http4s.implicits.uri
77
import org.http4s.{Method, Request}
@@ -11,24 +11,24 @@ import org.scalatest.funsuite.AnyFunSuiteLike
1111

1212
class TranscodingUrlMatcherTest extends AnyFunSuiteLike {
1313

14-
val matcher = TranscodingUrlMatcher.create[IO](
14+
private val matcher = TranscodingUrlMatcher.create[IO](
1515
Seq(
1616
MethodRegistry.Entry(
1717
MethodName("CountriesService", "CreateCountry"),
1818
null,
19-
Some(HttpRule.newBuilder().setPost("/countries").build()),
19+
Some(HttpRule().withPost("/countries")),
2020
null
2121
),
2222
MethodRegistry.Entry(
2323
MethodName("CountriesService", "ListCountries"),
2424
null,
25-
Some(HttpRule.newBuilder().setGet("/countries/list").build()),
25+
Some(HttpRule().withGet("/countries/list")),
2626
null
2727
),
2828
MethodRegistry.Entry(
2929
MethodName("CountriesService", "GetCountry"),
3030
null,
31-
Some(HttpRule.newBuilder().setGet("/countries/{country_id}").build()),
31+
Some(HttpRule().withGet("/countries/{country_id}")),
3232
null
3333
),
3434
),

core/src/test/scala/org/ivovk/connect_rpc_scala/http/grpc/MethodRegistryTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class MethodRegistryTest extends AnyFunSuite {
3636
val httpRule = entry.get.httpRule.get
3737

3838
assert(httpRule.getPost == "/v1/test/http_annotation_method")
39-
assert(httpRule.getBody == "*")
39+
assert(httpRule.body == "*")
4040
}
4141

4242
}

0 commit comments

Comments
 (0)