Skip to content

Commit a37fe36

Browse files
authored
Merge pull request #76 from MercuryTechnologies/tad/generic-sum-types
Fix codegen for sum-of-products with generic parameters
2 parents 4d5d79b + d2a88d7 commit a37fe36

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@JsonClassDiscriminator("direction")
2+
@Serializable
3+
sealed class CursorInput<A> {
4+
@Serializable
5+
@SerialName("nextPage")
6+
data class NextPage<A>(val key: A?) : CursorInput<A>()
7+
8+
@Serializable
9+
@SerialName("previousPage")
10+
data class PreviousPage<A>(val key: A) : CursorInput<A>()
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum CursorInput<A: Hashable & Codable>: CaseIterable, Hashable, Codable {
2+
case nextPage(A?)
3+
case previousPage(A)
4+
}

moat.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ test-suite spec
9494
SumOfProductWithTaggedObjectAndNonConcreteCasesSpec
9595
SumOfProductWithTaggedObjectAndSingleNullarySpec
9696
SumOfProductWithTaggedObjectStyleSpec
97+
SumOfProductWithTypeParameterSpec
9798
TypeVariableSpec
9899
Moat
99100
Moat.Class

src/Moat/Pretty/Kotlin.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,14 @@ prettyApp t1 t2 =
224224
-- error is restricted
225225
prettyTaggedObject ::
226226
String ->
227+
[String] ->
227228
[Annotation] ->
229+
[Interface] ->
228230
[EnumCase] ->
229231
String ->
230232
SumOfProductEncodingOptions ->
231233
String
232-
prettyTaggedObject parentName anns cases indents SumOfProductEncodingOptions {..} =
234+
prettyTaggedObject parentName tyVars anns ifaces cases indents SumOfProductEncodingOptions {..} =
233235
intercalate
234236
"\n\n"
235237
( cases <&> \case
@@ -238,29 +240,35 @@ prettyTaggedObject parentName anns cases indents SumOfProductEncodingOptions {..
238240
++ prettyAnnotations (Just caseNm) indents anns
239241
++ indents
240242
++ "data class "
241-
++ toUpperFirst caseNm
243+
++ caseTypeHeader caseNm
242244
++ "(val "
243245
++ contentsFieldName
244246
++ ": "
245247
++ prettyMoatType caseTy
246248
++ ") : "
247-
++ parentName
249+
++ parentTypeHeader
248250
++ "()"
249251
EnumCase caseNm caseDoc [] ->
250252
prettyTypeDoc indents caseDoc []
251253
++ prettyAnnotations (Just caseNm) indents anns
252254
++ indents
253255
++ "data object "
254-
++ toUpperFirst caseNm
256+
++ caseTypeHeader caseNm
255257
++ " : "
256-
++ parentName
258+
++ parentTypeHeader
257259
++ "()"
258260
EnumCase caseNm _ _ ->
259261
error $
260262
"prettyTaggedObject: The data constructor "
261263
<> caseNm
262264
<> " can have zero or one concrete type constructor!"
263265
)
266+
where
267+
caseTypeHeader :: String -> String
268+
caseTypeHeader name = prettyMoatTypeHeader (toUpperFirst name) (addTyVarBounds tyVars ifaces)
269+
270+
parentTypeHeader :: String
271+
parentTypeHeader = prettyMoatTypeHeader parentName tyVars
264272

265273
prettyEnum ::
266274
() =>
@@ -325,7 +333,7 @@ prettyEnum doc anns ifaces name tyVars cases sop@SumOfProductEncodingOptions {..
325333
++ classTyp
326334
++ prettyInterfaces ifaces
327335
++ " {\n"
328-
++ prettyTaggedObject name anns cases indents sop
336+
++ prettyTaggedObject name tyVars anns ifaces cases indents sop
329337
++ "\n}"
330338
where
331339
isCEnum :: [EnumCase] -> Bool
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module SumOfProductWithTypeParameterSpec where
2+
3+
import Common
4+
import Data.List (stripPrefix)
5+
import Data.Maybe
6+
import Moat
7+
import Test.Hspec
8+
import Test.Hspec.Golden
9+
import Prelude hiding (Enum)
10+
11+
data CursorInput a
12+
= CursorInputNextPage (Maybe a)
13+
| CursorInputPreviousPage a
14+
15+
mobileGenWith
16+
( defaultOptions
17+
{ constructorModifier = \body -> fromMaybe body (stripPrefix "CursorInput" body)
18+
, dataAnnotations = [Serializable, SerialName]
19+
, dataProtocols = [OtherProtocol "CaseIterable", Hashable, Codable]
20+
, sumOfProductEncodingOptions =
21+
SumOfProductEncodingOptions
22+
{ encodingStyle = TaggedObjectStyle
23+
, sumAnnotations = [RawAnnotation "JsonClassDiscriminator(\"direction\")"]
24+
, contentsFieldName = "key"
25+
, tagFieldName = "direction"
26+
}
27+
}
28+
)
29+
''CursorInput
30+
31+
spec :: Spec
32+
spec =
33+
describe "stays golden" $ do
34+
let moduleName = "SumOfProductWithTypeParameterSpec"
35+
it "kotlin" $
36+
defaultGolden ("kotlin" <> moduleName) (showKotlin @(CursorInput _))
37+
it "swift" $
38+
defaultGolden ("swift" <> moduleName) (showSwift @(CursorInput _))

0 commit comments

Comments
 (0)