Skip to content

Commit

Permalink
Merge branch 'main' into upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 1, 2024
2 parents a4df9c6 + 0cce276 commit 902924e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion elm-generator/src/main/scala/generator/elm/ElmCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ case class ElmTypeAliasBuilder(
case (name, typ) :: Nil => {
import ElmType._
typ match {
case ElmString | ElmInt | ElmBool | ElmDate | ElmFloat | ElmPosix | ElmDict(_) | ElmList(_) | ElmUserDefined(_) => Some(
case ElmString | ElmInt | ElmBool | ElmDate | ElmFloat | ElmPosix | ElmDict(_) | ElmList(_) | ElmEnumLocal(_) | _: ElmEnumImported | ElmUserDefinedLocal(_) | _: ElmUserDefinedImported => Some(
ElmParameter(name, typ.declaration)
)
case ElmNothing | ElmMaybe(_) => None
Expand Down
27 changes: 23 additions & 4 deletions elm-generator/src/main/scala/generator/elm/ElmResource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ case class ElmResource(args: GenArgs) {
case ElmInt => wrap("String.fromInt")
case ElmFloat => wrap("String.fromFloat")
case ElmBool => wrap("boolToString")
case _ => sys.error(s"Do not know how to convert parameter named ${p.name} with type ${p.typ} to String")
case ElmEnumLocal(name) => wrap(Names.camelCase(name) + "ToString")
case ElmEnumImported(ns, name) => {
args.imports.addExposing(ns, name)
wrap(Names.camelCase(name) + "ToString")
}
case ElmNothing | ElmDate |
ElmDict(_) |
ElmList(_) |
ElmMaybe(_) |
ElmPosix |
_: ElmUserDefinedLocal |
_: ElmUserDefinedImported => sys.error(s"Do not know how to convert parameter named ${p.name} with type ${p.typ} to String")
}
}

}

private[this] def url(variable: ElmVariable, params: Seq[ValidatedParameter]): String = {
Expand Down Expand Up @@ -156,6 +166,11 @@ case class ElmResource(args: GenArgs) {
case ElmBool => asString("boolToString")
case ElmInt => asString("String.fromInt")
case ElmFloat => asString("String.fromFlow")
case ElmEnumLocal(name) => asString(Names.camelCase(name) + "ToString")
case ElmEnumImported(ns, name) => {
args.imports.addExposing(ns, name)
asString(Names.camelCase(name) + "ToString")
}
case ElmMaybe(inner) => {
val code = inner match {
case ElmList(_) => s"${innerType(inner)}"
Expand All @@ -164,8 +179,12 @@ case class ElmResource(args: GenArgs) {
s"(Maybe.withDefault [] (Maybe.map (\\$nextVar -> $code) $declaration))"
}
case ElmList(inner) => s"List.map (\\$nextVar -> ${innerType(inner)}) $currentVar"
case ElmUserDefined(inner) => asString(Names.camelCase(inner) + "ToString")
case _ => sys.error(s"Do not know how to convert parameter named ${p.name} with type ${p.typ} to a query parameter")
case ElmUserDefinedLocal(inner) => asString(Names.camelCase(inner) + "ToString")
case ElmUserDefinedImported(ns, inner) => {
args.imports.addExposing(ns, inner)
asString(Names.camelCase(inner) + "ToString")
}
case ElmDate | ElmDict(_) | ElmPosix | ElmNothing => sys.error(s"Do not know how to convert parameter named ${p.name} with type ${p.typ} to a query parameter")
}
}

Expand Down
33 changes: 28 additions & 5 deletions elm-generator/src/main/scala/generator/elm/ElmTypeLookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ object ElmType {
case object ElmNothing extends ElmType {
override def declaration: String = "Nothing"
}
case class ElmUserDefined(name: String) extends ElmType {
case class ElmEnumLocal(name: String) extends ElmType {
assert(name == Names.pascalCase(name), "Name must be pascal case")
override def declaration: String = name
}
case class ElmEnumImported(namespace: String, name: String) extends ElmType {
assert(name == Names.pascalCase(name), "Name must be pascal case")
override def declaration: String = name
}
case class ElmUserDefinedLocal(name: String) extends ElmType {
assert(name == Names.pascalCase(name), "Name must be pascal case")
override def declaration: String = name
}
case class ElmUserDefinedImported(namespace: String, name: String) extends ElmType {
assert(name == Names.pascalCase(name), "Name must be pascal case")
override def declaration: String = name
}
Expand Down Expand Up @@ -101,10 +113,21 @@ case class ElmTypeLookup(args: GenArgs) {
}
case u: UserDefined => {
NamespaceParser.parse(u.name) match {
case ParsedName.Local(name) => ElmUserDefined(Names.pascalCase(name)).validNec
case ParsedName.Imported(namespace, name) => {
args.imports.addExposingAll(s"Generated.${Names.pascalCase(namespace)}")
ElmUserDefined(Names.pascalCase(name)).validNec
case ParsedName.Local(n) => {
val name = Names.pascalCase(n)
u match {
case _: UserDefined.Enum => ElmEnumLocal(name).validNec
case _: UserDefined.Model | _: UserDefined.Union => ElmUserDefinedLocal(name).validNec
}
}
case ParsedName.Imported(ns, n) => {
val namespace = s"Generated.${Names.pascalCase(ns)}"
val name = Names.pascalCase(n)
args.imports.addExposingAll(namespace)
u match {
case _: UserDefined.Enum => ElmEnumImported(namespace, name).validNec
case _: UserDefined.Model | _: UserDefined.Union => ElmUserDefinedImported(namespace, name).validNec
}
}
}
}
Expand Down

0 comments on commit 902924e

Please sign in to comment.