|
| 1 | +module Data.Foreign.Generic.EnumEncoding where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Control.Alt ((<|>)) |
| 6 | +import Data.Foreign (F, Foreign, ForeignError(..), fail, readString, toForeign) |
| 7 | +import Data.Generic.Rep (class Generic, Argument, Constructor(Constructor), NoArguments(NoArguments), Product, Rec, Sum(Inr, Inl), from, to) |
| 8 | +import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol) |
| 9 | +import Partial.Unsafe (unsafeCrashWith) |
| 10 | + |
| 11 | +type GenericEnumOptions = |
| 12 | + { constructorTagTransform :: String -> String |
| 13 | + } |
| 14 | + |
| 15 | +defaultGenericEnumOptions :: GenericEnumOptions |
| 16 | +defaultGenericEnumOptions = |
| 17 | + { constructorTagTransform: id |
| 18 | + } |
| 19 | + |
| 20 | +-- | A generic function to be used with "Enums", or sum types with only no-argument constructors. This is used for decoding from strings to one of the constructors, combined with the `constructorTagTransform` property of `SumEncoding`. |
| 21 | +genericDecodeEnum |
| 22 | + :: forall a rep |
| 23 | + . Generic a rep |
| 24 | + => GenericDecodeEnum rep |
| 25 | + => GenericEnumOptions |
| 26 | + -> Foreign |
| 27 | + -> F a |
| 28 | +genericDecodeEnum opts = map to <<< decodeEnum opts |
| 29 | + |
| 30 | +-- | A generic function to be used with "Enums", or sum types with only no-argument constructors. This is used for encoding to strings from one of the constructors, combined with the `constructorTagTransform` property of `SumEncoding`. |
| 31 | +-- | |
| 32 | +-- | For example: |
| 33 | +-- | |
| 34 | +-- | ```purescript |
| 35 | +-- | data Fruit = Apple | Banana | Frikandel |
| 36 | +-- | derive instance geFruit :: Generic Fruit _ |
| 37 | +-- | instance eFruit :: Encode Fruit where |
| 38 | +-- | encode = genericEncodeEnum defaultGenericEnumOptions |
| 39 | +genericEncodeEnum |
| 40 | + :: forall a rep |
| 41 | + . Generic a rep |
| 42 | + => GenericEncodeEnum rep |
| 43 | + => GenericEnumOptions |
| 44 | + -> a |
| 45 | + -> Foreign |
| 46 | +genericEncodeEnum opts = encodeEnum opts <<< from |
| 47 | + |
| 48 | +-- | A type class for type representations that can be used for decoding to an Enum. Only the sum and no-argument constructor instances are valid, while others provide a `Fail` constraint to fail in compilation. |
| 49 | +-- | |
| 50 | +-- | For example: |
| 51 | +-- | |
| 52 | +-- | ```purescript |
| 53 | +-- | data Fruit = Apple | Banana | Frikandel |
| 54 | +-- | derive instance geFruit :: Generic Fruit _ |
| 55 | +-- | instance dFruit :: Decode Fruit where |
| 56 | +-- | decode = genericDecodeEnum defaultGenericEnumOptions |
| 57 | +-- | ``` |
| 58 | +class GenericDecodeEnum a where |
| 59 | + decodeEnum :: GenericEnumOptions -> Foreign -> F a |
| 60 | + |
| 61 | +-- | A type class for type representations that can be used for encoding from an Enum. Only the sum and no-argument constructor instances are valid, while others provide a `Fail` constraint to fail in compilation. |
| 62 | +class GenericEncodeEnum a where |
| 63 | + encodeEnum :: GenericEnumOptions -> a -> Foreign |
| 64 | + |
| 65 | +instance sumGenericDecodeEnum |
| 66 | + :: (GenericDecodeEnum a, GenericDecodeEnum b) |
| 67 | + => GenericDecodeEnum (Sum a b) where |
| 68 | + decodeEnum opts f = Inl <$> decodeEnum opts f <|> Inr <$> decodeEnum opts f |
| 69 | + |
| 70 | +instance ctorNoArgsGenericDecodeEnum |
| 71 | + :: IsSymbol name |
| 72 | + => GenericDecodeEnum (Constructor name NoArguments) where |
| 73 | + decodeEnum {constructorTagTransform} f = do |
| 74 | + tag <- readString f |
| 75 | + unless (tag == ctorName) $ |
| 76 | + fail (ForeignError ("Expected " <> show ctorName <> " tag for unary constructor literal " <> ctorName)) |
| 77 | + pure $ Constructor NoArguments |
| 78 | + where |
| 79 | + ctorName = constructorTagTransform $ reflectSymbol (SProxy :: SProxy name) |
| 80 | + |
| 81 | +instance ctorArgumentGenericDecodeEnum |
| 82 | + :: Fail "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments." |
| 83 | + => GenericDecodeEnum (Constructor name (Argument a)) where |
| 84 | + decodeEnum _ _ = unsafeCrashWith "unreachable decodeEnum was reached." |
| 85 | + |
| 86 | +instance ctorProductGenericDecodeEnum |
| 87 | + :: Fail "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments." |
| 88 | + => GenericDecodeEnum (Constructor name (Product a b)) where |
| 89 | + decodeEnum _ _ = unsafeCrashWith "unreachable decodeEnum was reached." |
| 90 | + |
| 91 | +instance ctorRecGenericDecodeEnum |
| 92 | + :: Fail "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments." |
| 93 | + => GenericDecodeEnum (Constructor name (Rec a)) where |
| 94 | + decodeEnum _ _ = unsafeCrashWith "unreachable decodeEnum was reached." |
| 95 | + |
| 96 | +instance sumGenericEncodeEnum |
| 97 | + :: (GenericEncodeEnum a, GenericEncodeEnum b) |
| 98 | + => GenericEncodeEnum (Sum a b) where |
| 99 | + encodeEnum opts (Inl a) = encodeEnum opts a |
| 100 | + encodeEnum opts (Inr b) = encodeEnum opts b |
| 101 | + |
| 102 | +instance ctorNoArgsGenericEncodeEnum |
| 103 | + :: IsSymbol name |
| 104 | + => GenericEncodeEnum (Constructor name NoArguments) where |
| 105 | + encodeEnum {constructorTagTransform} _ = toForeign ctorName |
| 106 | + where |
| 107 | + ctorName = constructorTagTransform $ reflectSymbol (SProxy :: SProxy name) |
| 108 | + |
| 109 | +instance ctorArgumentGenericEncodeEnum |
| 110 | + :: Fail "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments." |
| 111 | + => GenericEncodeEnum (Constructor name (Argument a)) where |
| 112 | + encodeEnum _ _ = unsafeCrashWith "unreachable encodeEnum was reached." |
| 113 | + |
| 114 | +instance ctorProductGenericEncodeEnum |
| 115 | + :: Fail "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments." |
| 116 | + => GenericEncodeEnum (Constructor name (Product a b)) where |
| 117 | + encodeEnum _ _ = unsafeCrashWith "unreachable encodeEnum was reached." |
| 118 | + |
| 119 | +instance ctorRecGenericEncodeEnum |
| 120 | + :: Fail "genericEncode/DecodeEnum cannot be used on types that are not sums of constructors with no arguments." |
| 121 | + => GenericEncodeEnum (Constructor name (Rec a)) where |
| 122 | + encodeEnum _ _ = unsafeCrashWith "unreachable encodeEnum was reached." |
0 commit comments