Skip to content

Commit

Permalink
Fix mock generator for primitives inside a union
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 21, 2024
1 parent 9c55c0c commit e16a5fe
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scala.generator.mock

import generator.ServiceFileNames
import io.apibuilder.generator.v0.models.{File, InvocationForm}
import lib.Text
import lib.Text._
import lib.generator.CodeGenerator

Expand Down Expand Up @@ -53,10 +54,21 @@ object MockFactoriesGenerator extends CodeGenerator {
}

private[mock] def makeUnion(union: ScalaUnion): String = {
val typ = union.types.headOption.getOrElse {
sys.error(s"Union type[${union.qualifiedName}] does not have any times")
val impl = union.types.headOption match {
case Some(typ) => {
val value = mockValue(typ.datatype)
typ.datatype match {
case p: ScalaPrimitive => {
// Union type wrapping a primitive
val className = Text.pascalCase(union.name) + Text.pascalCase(p.shortName)
union.ssd.namespaces.unions + "." + className + s"($value)"
}
case _ => value
}
}
case None => "???"
}
s"def make${union.name}(): ${union.qualifiedName} = ${mockValue(typ.datatype)}"
s"def make${union.name}(): ${union.qualifiedName} = $impl"
}

private[mock] def mockValue(
Expand Down

0 comments on commit e16a5fe

Please sign in to comment.