From 0cce276b2f16d73c888f51ed9ce0762fce591fe7 Mon Sep 17 00:00:00 2001 From: Michael Bryzek Date: Sat, 1 Jun 2024 09:07:15 -0400 Subject: [PATCH] ELM: Support enums in resource paths (#695) --- .../main/scala/generator/elm/ElmCode.scala | 2 +- .../scala/generator/elm/ElmResource.scala | 27 ++++++++++++--- .../scala/generator/elm/ElmTypeLookup.scala | 33 ++++++++++++++++--- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/elm-generator/src/main/scala/generator/elm/ElmCode.scala b/elm-generator/src/main/scala/generator/elm/ElmCode.scala index 7dfc11f9..0469819f 100644 --- a/elm-generator/src/main/scala/generator/elm/ElmCode.scala +++ b/elm-generator/src/main/scala/generator/elm/ElmCode.scala @@ -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 diff --git a/elm-generator/src/main/scala/generator/elm/ElmResource.scala b/elm-generator/src/main/scala/generator/elm/ElmResource.scala index 0429b7cc..d6fbe396 100644 --- a/elm-generator/src/main/scala/generator/elm/ElmResource.scala +++ b/elm-generator/src/main/scala/generator/elm/ElmResource.scala @@ -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 = { @@ -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)}" @@ -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") } } diff --git a/elm-generator/src/main/scala/generator/elm/ElmTypeLookup.scala b/elm-generator/src/main/scala/generator/elm/ElmTypeLookup.scala index c60daa46..c161ddae 100644 --- a/elm-generator/src/main/scala/generator/elm/ElmTypeLookup.scala +++ b/elm-generator/src/main/scala/generator/elm/ElmTypeLookup.scala @@ -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 } @@ -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 + } } } }