when serializing messages, include json objects

This is done always, it's up to the comsumer to decide if it wants to
output the json objects or the messages.

Messages.JSON.finalize changed to not need a JSONOptions.
As far as I can see, this does not change its behavior,
since addErrorMessage appends to any list that's already there.

This commit was sponsored by Ethan Aubin.
This commit is contained in:
Joey Hess 2020-12-03 14:47:04 -04:00
parent 5a41e46bd4
commit e7f42e2ec7
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 28 additions and 18 deletions

View file

@ -29,26 +29,32 @@ outputMessage' jsonoutputter jsonbuilder msg = withMessageState $ \s -> case out
| otherwise -> liftIO $ flushed $ S.putStr msg | otherwise -> liftIO $ flushed $ S.putStr msg
JSONOutput _ -> void $ jsonoutputter jsonbuilder s JSONOutput _ -> void $ jsonoutputter jsonbuilder s
QuietOutput -> q QuietOutput -> q
SerializedOutput -> liftIO $ outputSerialized $ OutputMessage (decodeBS' msg) SerializedOutput -> do
liftIO $ outputSerialized $ OutputMessage msg
void $ jsonoutputter jsonbuilder s
-- Buffer changes to JSON until end is reached and then emit it. -- Buffer changes to JSON until end is reached and then emit it.
bufferJSON :: JSONBuilder -> MessageState -> Annex Bool bufferJSON :: JSONBuilder -> MessageState -> Annex Bool
bufferJSON jsonbuilder s = case outputType s of bufferJSON jsonbuilder s = case outputType s of
JSONOutput jsonoptions JSONOutput _ -> go (flushed . JSON.emit)
| endjson -> do SerializedOutput -> go (outputSerialized . JSONObject . JSON.encode)
_ -> return False
where
go emitter
| endjson = do
Annex.changeState $ \st -> Annex.changeState $ \st ->
st { Annex.output = s { jsonBuffer = Nothing } } st { Annex.output = s { jsonBuffer = Nothing } }
maybe noop (liftIO . flushed . JSON.emit . JSON.finalize jsonoptions) json maybe noop (liftIO . emitter . JSON.finalize) json
return True return True
| otherwise -> do | otherwise = do
Annex.changeState $ \st -> Annex.changeState $ \st ->
st { Annex.output = s { jsonBuffer = json } } st { Annex.output = s { jsonBuffer = json } }
return True return True
_ -> return False
where
(json, endjson) = case jsonbuilder i of (json, endjson) = case jsonbuilder i of
Nothing -> (jsonBuffer s, False) Nothing -> (jsonBuffer s, False)
(Just (j, e)) -> (Just j, e) (Just (j, e)) -> (Just j, e)
i = case jsonBuffer s of i = case jsonBuffer s of
Nothing -> Nothing Nothing -> Nothing
Just b -> Just (b, False) Just b -> Just (b, False)
@ -56,11 +62,14 @@ bufferJSON jsonbuilder s = case outputType s of
-- Immediately output JSON. -- Immediately output JSON.
outputJSON :: JSONBuilder -> MessageState -> Annex Bool outputJSON :: JSONBuilder -> MessageState -> Annex Bool
outputJSON jsonbuilder s = case outputType s of outputJSON jsonbuilder s = case outputType s of
JSONOutput _ -> do JSONOutput _ -> go (flushed . JSON.emit)
maybe noop (liftIO . flushed . JSON.emit) SerializedOutput -> go (outputSerialized . JSONObject . JSON.encode)
_ -> return False
where
go emitter = do
maybe noop (liftIO . emitter)
(fst <$> jsonbuilder Nothing) (fst <$> jsonbuilder Nothing)
return True return True
_ -> return False
outputError :: String -> Annex () outputError :: String -> Annex ()
outputError msg = withMessageState $ \s -> case (outputType s, jsonBuffer s) of outputError msg = withMessageState $ \s -> case (outputType s, jsonBuffer s) of

View file

@ -11,6 +11,7 @@ module Messages.JSON (
JSONBuilder, JSONBuilder,
JSONChunk(..), JSONChunk(..),
emit, emit,
encode,
none, none,
start, start,
end, end,
@ -38,7 +39,6 @@ import Data.Maybe
import Data.Monoid import Data.Monoid
import Prelude import Prelude
import Types.Messages
import Types.Command (SeekInput(..)) import Types.Command (SeekInput(..))
import Key import Key
import Utility.Metered import Utility.Metered
@ -82,12 +82,10 @@ end :: Bool -> JSONBuilder
end b (Just (o, _)) = Just (HM.insert "success" (toJSON' b) o, True) end b (Just (o, _)) = Just (HM.insert "success" (toJSON' b) o, True)
end _ Nothing = Nothing end _ Nothing = Nothing
finalize :: JSONOptions -> Object -> Object -- Always include error-messages field, even if empty,
finalize jsonoptions o -- to make the json be self-documenting.
-- Always include error-messages field, even if empty, finalize :: Object -> Object
-- to make the json be self-documenting. finalize o = addErrorMessage [] o
| jsonErrorMessages jsonoptions = addErrorMessage [] o
| otherwise = o
addErrorMessage :: [String] -> Object -> Object addErrorMessage :: [String] -> Object -> Object
addErrorMessage msg o = addErrorMessage msg o =

View file

@ -12,6 +12,8 @@ import Utility.Metered
import Control.Concurrent import Control.Concurrent
import System.Console.Regions (ConsoleRegion) import System.Console.Regions (ConsoleRegion)
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
data OutputType data OutputType
= NormalOutput = NormalOutput
@ -60,7 +62,8 @@ newMessageState = do
} }
data SerializedOutput data SerializedOutput
= OutputMessage String = OutputMessage S.ByteString
| OutputError String | OutputError String
| ProgressMeter (Maybe Integer) MeterState MeterState | ProgressMeter (Maybe Integer) MeterState MeterState
| JSONObject L.ByteString
deriving (Show, Read) deriving (Show, Read)