-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
121 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package generator.elm | ||
|
||
import cats.data.ValidatedNec | ||
import cats.implicits._ | ||
import io.apibuilder.spec.v0.models.Union | ||
|
||
case class ElmUnion(args: GenArgs) { | ||
private val elmJson = ElmJson(args.imports) | ||
|
||
def generate(union: Union): ValidatedNec[String, String] = { | ||
genDecoder(union).map { decoder => | ||
( | ||
Seq(genTypeAlias(union)) ++ Seq(decoder.code) | ||
).mkString("\n\n") | ||
} | ||
} | ||
|
||
private def genTypeAlias(union: Union): String = { | ||
val unionName = Names.pascalCase(union.name) | ||
Seq( | ||
s"type $unionName =", | ||
union.types.map { t => | ||
val typeName = Names.pascalCase(t.`type`) | ||
s"$unionName${typeName} $typeName" | ||
}.mkString("\n| ").indent(4).stripTrailing(), | ||
).mkString("\n") | ||
} | ||
|
||
private def genDecoder(m: Union): ValidatedNec[String, ElmFunction] = { | ||
m.discriminator match { | ||
case None => "Only union types with discriminators are currently supported".invalidNec | ||
case Some(disc) => genDecoderType(m, disc).validNec | ||
} | ||
} | ||
|
||
private def decoderByDiscriminatorName(u: Union, disc: String): String = { | ||
elmJson.decoderName(u.name) + "By" + Names.pascalCase(disc) | ||
} | ||
|
||
private def genDecoderType(u: Union, disc: String): ElmFunction = { | ||
args.imports.addAs("Json.Decode", "Decode") | ||
elmJson.decoder(u.name) { | ||
Seq( | ||
s"Decode.field ${Names.wrapInQuotes(disc)} Decode.string", | ||
s" |> Decode.andThen (\\disc ->", | ||
s" case disc of", | ||
genDecoderDiscriminator(u, disc).indent(12).stripTrailing, | ||
s" )" | ||
).mkString("\n") | ||
} | ||
} | ||
|
||
private def genDecoderDiscriminator(u: Union, disc: String): String = { | ||
val unionName = Names.pascalCase(u.name) | ||
val all = u.types.map { t => | ||
s""" | ||
|${Names.wrapInQuotes(t.`type`)} -> | ||
| ${elmJson.decoderName(t.`type`)} |> Decode.map $unionName${Names.pascalCase(t.`type`)} | ||
|""".stripMargin.strip | ||
} ++ Seq(s"_ ->\n Decode.fail (\"Unknown ${Names.maybeQuote(disc)}: \" ++ disc)") | ||
all.mkString("\n\n").strip() | ||
} | ||
|
||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package lib | ||
|
||
import play.api.libs.json.{JsArray, JsObject, JsDefined, JsValue, Json} | ||
|
||
/** | ||
* Version 0.4.28 of apibuilder-validation has an invalid Service spec definition which makes | ||
* the apidoc field required. This dependency is pulled in by apibuilder-graphql. Once we can | ||
* migrate the generator to scala3 we can pull in the latest dependencies. Until then to keep | ||
* things working, we inject a default "apidoc" node where not specified. | ||
*/ | ||
object ServiceApidocBug { | ||
private val DefaultApiDoc = Json.obj( | ||
"apidoc" -> Json.obj( | ||
"version" -> "0.16.0" | ||
) | ||
) | ||
|
||
def rewrite(js: JsValue): JsValue = { | ||
js match { | ||
case o: JsObject => rewriteObject(o) | ||
case _ => js | ||
} | ||
} | ||
|
||
private def rewriteObject(js: JsObject): JsObject = { | ||
rewriteImportedServices( | ||
rewriteService(js) | ||
) | ||
} | ||
|
||
private def rewriteService(js: JsObject): JsObject = { | ||
js \ "service" match { | ||
case JsDefined(svc: JsObject) => { | ||
js ++ Json.obj("service" -> maybeAddApidoc(svc)) | ||
} | ||
case _ => js | ||
} | ||
} | ||
|
||
private def rewriteImportedServices(js: JsObject): JsObject = { | ||
js \ "imported_services" match { | ||
case JsDefined(svc: JsArray) => { | ||
js ++ Json.obj( | ||
"imported_services" -> JsArray( | ||
svc.value.toSeq.map { | ||
case o: JsObject => maybeAddApidoc(o) | ||
case o => o | ||
} | ||
) | ||
) | ||
} | ||
case _ => js | ||
} | ||
} | ||
|
||
private def maybeAddApidoc(js: JsObject): JsObject = { | ||
if (js.fields.contains("apidoc")) { | ||
js | ||
} else { | ||
js ++ DefaultApiDoc | ||
} | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
scala-generator/src/main/scala/models/FeatureMigration.scala
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.