Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding fails in 0.10, succeeds in 0.9.0.1. #320

Closed
semaj opened this issue Dec 7, 2015 · 1 comment
Closed

Decoding fails in 0.10, succeeds in 0.9.0.1. #320

semaj opened this issue Dec 7, 2015 · 1 comment

Comments

@semaj
Copy link

semaj commented Dec 7, 2015

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

import Data.Aeson
import Data.Char
import Data.List.Split
import GHC.Generics
import Data.ByteString.Lazy.UTF8 (fromString)

data CommandType = CGET | CPUT deriving (Show, Generic)
data Command = Command {
  ctype :: CommandType,
  cterm :: Int,
  creator :: !String,
  ckey :: !String,
  cvalue :: !String
} deriving (Show, Generic)

instance ToJSON Command
instance FromJSON Command
instance ToJSON CommandType
instance FromJSON CommandType

data MessType = GET | PUT | OK | FAIL | REDIRECT | RAFT deriving (Read, Eq)

instance Show MessType where
  show GET = "get"
  show PUT = "put"
  show OK = "ok"
  show FAIL = "fail"
  show REDIRECT = "redirect"
  show RAFT = "RAFT"

data Message = Message {
  src :: !String,
  dst :: !String,
  leader :: !String,
  messType :: !MessType,
  mid :: !String,
  key :: Maybe String,
  value :: Maybe String,
  rmess :: Maybe RMessage
} deriving (Show)

instance FromJSON Message where
  parseJSON = withObject "message" $ \o -> do
    src     <- o .: "src"
    dst     <- o .: "dst"
    leader  <- o .: "leader"
    rawType <- o .: "type"
    mid     <- o .: "MID"
    key     <- o .:? "key" -- might not be there
    value   <- o .:? "value" -- might not be there
    rmess   <- o .:? "rmess" -- will be here if messType == AE or RV or...
    let messType = (read $ map toUpper rawType) :: MessType
    return Message{..}

instance ToJSON Message where
  toJSON Message{..} = object [
    "src"    .= src,
    "dst"    .= dst,
    "leader" .= leader,
    "type"   .= (show messType),
    "MID"    .= mid,
    "key"    .= key, -- Aeson encodes these maybes as "null"
    "value"  .= value,
    "rmess"  .= rmess ]

data RMessage = AE {
  term :: Int,
  leaderId :: !String,
  prevLogIndex :: Int,
  prevLogTerm :: Int,
  entries :: [Command],
  leaderCommit :: Int
} | AER {
  term :: Int,
  success :: Bool
} | RV {
  term :: Int,
  candidateId :: !String,
  lastLogIndex :: Int,
  lastLogTerm :: Int
} | RVR {
  term :: Int,
  voteGranted :: Bool
} deriving (Generic, Show)

instance ToJSON RMessage
instance FromJSON RMessage

main :: IO ()
main = do
  let s = "{\"rmess\":{\"tag\":\"RV\",\"lastLogTerm\":0,\"term\":5,\"lastLogIndex\":0,\"candidateId\":\"0003\"},\"value\":null,\"dst\":\"FFFF\",\"leader\":\"FFFF\",\"key\":null,\"src\":\"0003\",\"MID\":\"398951\",\"type\":\"RAFT\"}\n"
  let splitt = splitOn "\n" s
  let fsMessages = map fromString splitt
  let mMessages = map decode fsMessages :: [Maybe Message]
  putStrLn $ show mMessages

Using aeson 0.9.0.1 I get [Just ..., Nothing] and with aeson 0.10.0.0 I get [Nothing, Nothing]. Am I doing something wrong?

@tolysz
Copy link
Contributor

tolysz commented Dec 15, 2015

This is sorted by: #326
Simply one patter got lost in the upgrade; api does not change

@bos bos closed this as completed Jan 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants