Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit 6a5ae74

Browse files
authored
Support unions of more than 2 elements correctly (#308)
1 parent d3459db commit 6a5ae74

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

adapter/protobuf/mu-protobuf.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ executable test-protobuf
6363

6464
hs-source-dirs: test
6565
default-language: Haskell2010
66-
ghc-options: -Wall -fprint-explicit-foralls
66+
ghc-options: -Wall -fprint-explicit-foralls -fprint-potential-instances

adapter/protobuf/test/ProtoBuf.hs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,18 @@ data MPerson
5555
deriving (FromSchema ExampleSchema "person")
5656

5757
newtype MFoo
58-
= MFoo { fooChoice :: Either Int32 T.Text }
58+
= MFoo { fooChoice :: MFooChoice }
5959
deriving (Eq, Show, Generic)
6060
deriving (ToSchema ExampleSchema "Foo")
6161
deriving (FromSchema ExampleSchema "Foo")
6262

63+
data MFooChoice
64+
= FooInt Int32
65+
| FooString T.Text
66+
| FooOtherInt Int32
67+
| FooYetAnotherInt Int32
68+
deriving (Eq, Show, Generic)
69+
6370
data MAddress
6471
= MAddress { postcode :: T.Text
6572
, country :: T.Text }
@@ -75,11 +82,11 @@ examplePerson1 = MPerson "Pythonio" "van Gogh"
7582
30 Male
7683
exampleAddress [1,2,3]
7784
(M.fromList [("hola", 1), ("hello", 2), ("hallo", 3)])
78-
(Just $ MFoo $ Right "blah")
85+
(Just $ MFoo $ FooString "blah")
7986
examplePerson2 = MPerson "Cuarenta" "Siete"
8087
0 NB
8188
exampleAddress [] M.empty
82-
(Just $ MFoo $ Left 3)
89+
(Just $ MFoo $ FooInt 3)
8390

8491
main :: IO ()
8592
main = do -- Obtain the filenames

adapter/protobuf/test/protobuf/example.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ message Foo {
2626
oneof fooChoice {
2727
int32 foo_int = 1;
2828
string foo_string = 2;
29+
int32 foo_other_int = 3;
30+
int32 foo_yet_another_int = 4;
2931
}
3032
}

core/schema/src/Mu/Schema/Class.hs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -394,30 +394,30 @@ instance {-# OVERLAPS #-} (GFromSchemaFieldTypeUnion sch (a ': b ': rest) v)
394394
=> GFromSchemaFieldTypeUnion sch (a ': b ': rest) (M1 i t v) where
395395
fromSchemaFieldTypeUnion x = M1 (fromSchemaFieldTypeUnion x)
396396

397-
instance (GToSchemaFieldTypeWrap sch t v, GToSchemaFieldTypeUnion sch ts vs)
397+
instance {-# OVERLAPPABLE #-} (GToSchemaFieldTypeWrap sch t v, GToSchemaFieldTypeUnion sch ts vs)
398398
=> GToSchemaFieldTypeUnion sch (t ': ts) (v :+: vs) where
399399
toSchemaFieldTypeUnion (L1 x) = Z (toSchemaFieldTypeW x)
400400
toSchemaFieldTypeUnion (R1 r) = S (toSchemaFieldTypeUnion r)
401-
instance (GFromSchemaFieldTypeWrap sch t v, GFromSchemaFieldTypeUnion sch ts vs)
401+
instance {-# OVERLAPPABLE #-} (GFromSchemaFieldTypeWrap sch t v, GFromSchemaFieldTypeUnion sch ts vs)
402402
=> GFromSchemaFieldTypeUnion sch (t ': ts) (v :+: vs) where
403403
fromSchemaFieldTypeUnion (Z x) = L1 (fromSchemaFieldTypeW x)
404404
fromSchemaFieldTypeUnion (S r) = R1 (fromSchemaFieldTypeUnion r)
405405
-- Weird nested instance produced by GHC
406-
instance ( GToSchemaFieldTypeWrap sch t1 v1
407-
, GToSchemaFieldTypeWrap sch t2 v2
408-
, GToSchemaFieldTypeUnion sch ts vs )
409-
=> GToSchemaFieldTypeUnion sch (t1 ': t2 ': ts) ((v1 :+: v2) :+: vs) where
410-
toSchemaFieldTypeUnion (L1 (L1 x)) = Z (toSchemaFieldTypeW x)
411-
toSchemaFieldTypeUnion (L1 (R1 x)) = S (Z (toSchemaFieldTypeW x))
412-
toSchemaFieldTypeUnion (R1 r) = S (S (toSchemaFieldTypeUnion r))
413-
instance ( GFromSchemaFieldTypeWrap sch t1 v1
414-
, GFromSchemaFieldTypeWrap sch t2 v2
415-
, GFromSchemaFieldTypeUnion sch ts vs )
416-
=> GFromSchemaFieldTypeUnion sch (t1 ': t2 ': ts) ((v1 :+: v2) :+: vs) where
417-
fromSchemaFieldTypeUnion (Z x) = L1 (L1 (fromSchemaFieldTypeW x))
418-
fromSchemaFieldTypeUnion (S (Z x)) = L1 (R1 (fromSchemaFieldTypeW x))
419-
fromSchemaFieldTypeUnion (S (S r)) = R1 (fromSchemaFieldTypeUnion r)
420-
406+
instance {-# OVERLAPS #-} ( GToSchemaFieldTypeUnion sch (t ': ts) (v1 :+: (v2 :+: vs)) )
407+
=> GToSchemaFieldTypeUnion sch (t ': ts) ((v1 :+: v2) :+: vs) where
408+
toSchemaFieldTypeUnion (L1 (L1 x))
409+
= toSchemaFieldTypeUnion @_ @_ @sch @(t ': ts) @(v1 :+: (v2 :+: vs)) (L1 x)
410+
toSchemaFieldTypeUnion (L1 (R1 x))
411+
= toSchemaFieldTypeUnion @_ @_ @sch @(t ': ts) @(v1 :+: (v2 :+: vs)) (R1 (L1 x))
412+
toSchemaFieldTypeUnion (R1 r)
413+
= toSchemaFieldTypeUnion @_ @_ @sch @(t ': ts) @(v1 :+: (v2 :+: vs)) (R1 (R1 r))
414+
instance {-# OVERLAPS #-} ( GFromSchemaFieldTypeUnion sch (t ': ts) (v1 :+: (v2 :+: vs)) )
415+
=> GFromSchemaFieldTypeUnion sch (t ': ts) ((v1 :+: v2) :+: vs) where
416+
fromSchemaFieldTypeUnion t
417+
= case fromSchemaFieldTypeUnion @_ @_ @sch @(t ': ts) @(v1 :+: (v2 :+: vs)) t of
418+
L1 x -> L1 (L1 x)
419+
R1 (L1 x) -> L1 (R1 x)
420+
R1 (R1 x) -> R1 x
421421

422422
-- ---------------
423423
-- ENUMERATIONS --

0 commit comments

Comments
 (0)