PermissionLevel: codec fixed.

master
Philippe Pittoli 2023-05-31 00:22:38 +02:00
parent 5e6edd2f7f
commit 5b08ba5b54
2 changed files with 26 additions and 35 deletions

View File

@ -4,30 +4,27 @@ module AlternativeMain where
import Prelude import Prelude
import Effect (Effect) import Effect (Effect)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Class.Console as Console import Effect.Class.Console as Console
import Data.Argonaut.Core as J import Data.Argonaut.Core as J
import Data.Codec.Argonaut as CA import Data.Codec.Argonaut as CA
import Data.Maybe (Maybe) import Data.Either (Either)
import Data.Argonaut.Decode as DAD
import Data.Either (Either(..))
import Data.Codec.Argonaut.Record as CAR import Data.Codec.Argonaut.Record as CAR
import Data.UInt (fromInt, toInt, UInt)
import Data.Argonaut.Parser as JSONParser import Data.Argonaut.Parser as JSONParser
import Data.Bifunctor (lmap) import Data.Bifunctor (lmap)
import Data.Tuple (Tuple(..))
import App.PermissionLevel as PermissionLevel import App.PermissionLevel as PermissionLevel
type SumType = { permission :: PermissionLevel.PermissionLevel } type Permission = { permission :: PermissionLevel.PermissionLevel }
codecSumType = CA.object "SumType" (CAR.record { permission: PermissionLevel.codec })
encode :: SumType -> String codecPermission :: CA.JsonCodec Permission
encode = J.stringify <<< CA.encode codecSumType codecPermission = CA.object "Permission" (CAR.record { permission: PermissionLevel.codec })
decode :: String -> Either String SumType encode :: Permission -> String
decode = parseDecodeJSON codecSumType encode = J.stringify <<< CA.encode codecPermission
decode :: String -> Either String Permission
decode = parseDecodeJSON codecPermission
parseDecodeJSON :: forall a. CA.JsonCodec a -> String -> Either String a parseDecodeJSON :: forall a. CA.JsonCodec a -> String -> Either String a
parseDecodeJSON codec str = do parseDecodeJSON codec str = do

View File

@ -1,13 +1,7 @@
module App.PermissionLevel where module App.PermissionLevel where
import Prelude
import Data.Codec.Argonaut as CA import Data.Codec.Argonaut as CA
import Data.Codec.Argonaut.Variant as CAV import Data.Maybe (Maybe(..))
import Data.Variant as V
import Data.Profunctor (dimap)
import Data.Either (Either(..))
import Type.Proxy (Proxy(..))
data PermissionLevel data PermissionLevel
= None = None
@ -15,22 +9,22 @@ data PermissionLevel
| Edit | Edit
| Admin | Admin
codec ∷ CA.JsonCodec PermissionLevel -- | Codec for just encoding a single value of type `PermissionLevel`
codec :: CA.JsonCodec PermissionLevel
codec = codec =
dimap toVariant fromVariant $ CAV.variantMatch { permission: Right CA.string } CA.prismaticCodec "PermissionLevel" from to CA.string
where where
toVariant = case _ of from :: String -> Maybe PermissionLevel
None → V.inj (Proxy ∷ _ "permission") "none" from = case _ of
Read → V.inj (Proxy ∷ _ "permission") "read" "none" -> Just None
Edit → V.inj (Proxy ∷ _ "permission") "edit" "read" -> Just Read
Admin → V.inj (Proxy ∷ _ "permission") "admin" "edit" -> Just Edit
"admin" -> Just Admin
_ -> Nothing
fromVariant = V.match to :: PermissionLevel -> String
{ permission: to = case _ of
\s -> case s of None -> "none"
"none" -> None Read -> "read"
"read" -> Read Edit -> "edit"
"edit" -> Edit Admin -> "admin"
"admin" -> Admin
_ -> None
}