Skip to content

Commit

Permalink
Elm: Add support for date, date time (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek authored Oct 25, 2023
1 parent c215027 commit 9b064d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions elm-generator/src/main/scala/generator/elm/ElmEnum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ case class ElmEnum(args: GenArgs) {
// "type MemberStatus = MemberStatusPending | MemberStatusActive | MemberStatusInactive | MemberStatusUnknown"
def generate(e: Enum): String = {
Seq(
s"type ${Names.pascalCase(e.name)} = " + values(e).mkString(" | "),
s"type ${Names.pascalCase(e.name)}\n = " + values(e).mkString("\n | "),
genToString(e).code,
genFromString(e).code,
genEncoder(e).code,
Expand Down Expand Up @@ -79,11 +79,11 @@ case class ElmEnum(args: GenArgs) {
/*
memberStatusFromString : String -> MemberStatus
memberStatusFromString value =
if (value == "active") then
if value == "active" then
MemberStatusActive
else if (value == "pending") then
else if value == "pending" then
MemberStatusPending
else if (value == "inactive") then
else if value == "inactive" then
MemberStatusInactive
else
MemberStatusUnknown
Expand All @@ -92,7 +92,7 @@ case class ElmEnum(args: GenArgs) {
def singleValue(isFirst: Boolean, name: String, value: String) = {
val prefix = if (isFirst) { "" } else { "else " }
Seq(
s"${prefix}if (value == ${Names.wrapInQuotes(value)}) then",
s"${prefix}if value == ${Names.wrapInQuotes(value)} then",
s" $name"
).mkString("\n").indent(4)
}
Expand Down
20 changes: 16 additions & 4 deletions elm-generator/src/main/scala/generator/elm/ElmModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,14 @@ case class ElmModel(args: GenArgs) {
case Double => "Encode.float"
case Integer => "Encode.int"
case Long => "Encode.int"
case DateIso8601 => "Encode.string"
case DateTimeIso8601 => "Encode.string"
case DateIso8601 => {
args.imports.addAs("Util.DateFormatter", "DateFormatter")
"DateFormatter.encode"
}
case DateTimeIso8601 => {
args.imports.addAs("Iso8601", "Iso8601")
"Iso8601.encode"
}
case Decimal => "Encode.float"
case Object => "Encode.string" // TODO
case JsonValue => "Encode.string" // TODO
Expand Down Expand Up @@ -199,8 +205,14 @@ case class ElmModel(args: GenArgs) {
case Double => "Decode.float"
case Integer => "Decode.int"
case Long => "Decode.int"
case DateIso8601 => "Decode.string"
case DateTimeIso8601 => "Decode.string"
case DateIso8601 => {
args.imports.addAs("Util.DateFormatter", "DateFormatter")
"DateFormatter.decoder"
}
case DateTimeIso8601 => {
args.imports.addAs("Iso8601", "Iso8601")
"Iso8601.decoder"
}
case Decimal => "Decode.float"
case Object => "Decode.string" // TODO
case JsonValue => "Decode.string" // TODO
Expand Down
10 changes: 8 additions & 2 deletions elm-generator/src/main/scala/generator/elm/ElmType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ case class ElmType(args: GenArgs) {
case Double => "Float"
case Integer => "Int"
case Long => "Int"
case DateIso8601 => "String"
case DateTimeIso8601 => "String"
case DateIso8601 => {
args.imports.addExposing("Date", "Date")
"Date"
}
case DateTimeIso8601 => {
args.imports.addExposing("Time", "Posix")
"Posix"
}
case Decimal => "Float"
case Object => "String" // TODO
case JsonValue => "String" // TODO
Expand Down

0 comments on commit 9b064d1

Please sign in to comment.