Skip to content

Commit

Permalink
Merge pull request #27 from GoNZooo/r.add-type-to-kotlin-enums
Browse files Browse the repository at this point in the history
feat: use enum type for kotlin enums
  • Loading branch information
GoNZooo authored Oct 11, 2022
2 parents 0b70ba9 + efa1580 commit 5cea7d7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gotyno-hs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: gotyno-hs
version: 2.0.0
version: 2.1.0
synopsis: A type definition compiler supporting multiple output languages.
description: Compiles type definitions into F#, TypeScript and Python, with validators, decoders and encoders.
category: Compiler
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: gotyno-hs
version: 2.0.0
version: 2.1.0
synopsis: A type definition compiler supporting multiple output languages.
description: Compiles type definitions into F#, TypeScript and Python, with validators, decoders and encoders.
license: BSD2
Expand Down
16 changes: 11 additions & 5 deletions src/CodeGeneration/Kotlin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ outputDefinition (TypeDefinition name (Struct (GenericStruct typeVariables field
outputDefinition (TypeDefinition name (Union typeTag unionType)) =
pure $ outputUnion name typeTag unionType
-- @TODO: use the type here to set the type of the enumeration
outputDefinition (TypeDefinition name (Enumeration _type enumerationValues)) =
pure $ outputEnumeration name enumerationValues
outputDefinition (TypeDefinition name (Enumeration type' enumerationValues)) =
pure $ outputEnumeration type' name enumerationValues
outputDefinition (TypeDefinition name (UntaggedUnion unionCases)) =
pure $ outputUntaggedUnion name unionCases
outputDefinition (TypeDefinition name (EmbeddedUnion typeTag constructors)) =
Expand Down Expand Up @@ -149,8 +149,8 @@ outputUntaggedUnion unionName cases =
"}"
]

outputEnumeration :: DefinitionName -> [EnumerationValue] -> Text
outputEnumeration name values' =
outputEnumeration :: BasicTypeValue -> DefinitionName -> [EnumerationValue] -> Text
outputEnumeration basicType name values' =
let valuesOutput =
values'
& fmap
Expand All @@ -171,7 +171,13 @@ outputEnumeration name values' =
outputLiteralValue (LiteralFloat f) = tshow f
outputLiteralValue (LiteralBoolean b) = bool "false" "true" b
in mconcat
[ mconcat ["enum class ", nameOf name, "(val data: Any) : java.io.Serializable {\n"],
[ mconcat
[ "enum class ",
nameOf name,
"(val data: ",
outputFieldType $ BasicType basicType,
") : java.io.Serializable {\n"
],
valuesOutput,
";\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion test/reference-output/basic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ data class SearchesParameters(
val filters: ArrayList<GetSearchesFilter>
) : java.io.Serializable

enum class StillSize(val data: Any) : java.io.Serializable {
enum class StillSize(val data: String) : java.io.Serializable {
@JsonProperty("w92") W92("w92"),
@JsonProperty("w185") W185("w185"),
@JsonProperty("w300") W300("w300"),
Expand Down

0 comments on commit 5cea7d7

Please sign in to comment.