Skip to content

Commit c731344

Browse files
author
Bijan Chokoufe Nejad
committed
Separate list and enum decoders so they work independently of each other
1 parent a0e23bb commit c731344

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

openapi-scala/src/main/scala/com/enfore/apis/generator/http4s/Http4sGenerator.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,9 @@ object Http4sGenerator {
129129

130130
val refinementDecoders: List[String] = buildRefinementDecoders(routeDefinitions, indentationLevel)
131131
val listDecoder: List[String] = RouteGenerator.listDecoder(routeDefinitions, indentationLevel)
132+
val enumDecoder: List[String] = RouteGenerator.enumDecoders(routeDefinitions, indentationLevel)
132133
val matchers: List[String] = RouteGenerator.buildMatchers(routeDefinitions, indentationLevel)
133-
134-
val output = (listDecoder, matchers) match {
135-
case (Nil, m) => (refinementDecoders :+ "\n") ++ m
136-
case (l, m) => ((l :+ "\n") ++ refinementDecoders) ++ m
137-
}
134+
val output = listDecoder ++ enumDecoder ++ (refinementDecoders :+ "\n") ++ matchers
138135
output.distinct
139136
}
140137

openapi-scala/src/main/scala/com/enfore/apis/generator/http4s/RouteGenerator.scala

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object RouteGenerator {
6565
* Array query string is assumed to be split with comma, i.e. ?q=a,b,c,d
6666
*/
6767
def listDecoder(routes: List[RouteDefinition], indentationLevel: Int): List[String] = {
68-
val decoder =
68+
val decoder: List[String] =
6969
s"""implicit def listDecoder[T](implicit decoder: QueryParamDecoder[T]): QueryParamDecoder[List[T]] =
7070
| QueryParamDecoder[String]
7171
| .map(
@@ -76,20 +76,22 @@ object RouteGenerator {
7676
.toList
7777

7878
if (routes.exists(anyListQueryParameter)) {
79+
decoder.map(`\t` * indentationLevel + _)
80+
} else {
81+
Nil
82+
}
83+
}
7984

80-
val refDecoders: List[String] =
81-
routes
82-
.flatMap(getEnumQueryParameter)
83-
.flatten
84-
.flatMap(typeName => enumDecoder(typeName, indentationLevel))
85-
86-
if (refDecoders.nonEmpty) {
87-
println("Generating http4s decoders for request references, assuming they all are enums")
88-
val importLine = List("import org.http4s.QueryParamDecoder.fromUnsafeCast\n")
89-
(importLine ++ refDecoders ++ decoder).map(`\t` * indentationLevel + _)
90-
} else {
91-
decoder.map(`\t` * indentationLevel + _)
92-
}
85+
def enumDecoders(routes: List[RouteDefinition], indentationLevel: Int): List[String] = {
86+
val refDecoders: List[String] =
87+
routes
88+
.flatMap(getEnumQueryParameter)
89+
.flatten
90+
.flatMap(typeName => enumDecoder(typeName, indentationLevel))
91+
if (refDecoders.nonEmpty) {
92+
println("Generating http4s decoders for request references, assuming they all are enums")
93+
val importLine = List("import org.http4s.QueryParamDecoder.fromUnsafeCast\n")
94+
(importLine ++ refDecoders).map(`\t` * indentationLevel + _)
9395
} else {
9496
Nil
9597
}
@@ -103,7 +105,7 @@ object RouteGenerator {
103105
*
104106
* @return generated decoder string lines
105107
*/
106-
def enumDecoder(enumFullTypeName: String, indentationLevel: Int): List[String] = {
108+
private def enumDecoder(enumFullTypeName: String, indentationLevel: Int): List[String] = {
107109
val typeNameLowerCase = enumFullTypeName.toLowerCase
108110
.split("\\.")
109111
.last

0 commit comments

Comments
 (0)