add "input" field to json output

The use case of this field is mostly to support -J combined with --json.
When that is implemented, a user will be able to look at the field to
determine which of the requests they have sent it corresponds to.

The field typically has a single value in its list, but in some cases
mutliple values (eg 2 command-line params) are combined together and the
list will have more.

Note that json parsing was already non-strict, so old git-annex metadata
--json --batch can be fed json produced by the new git-annex and will
not stumble over the new field.
This commit is contained in:
Joey Hess 2020-09-15 16:22:44 -04:00
parent 2a3c2b1843
commit fcf5d11c63
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
11 changed files with 66 additions and 60 deletions

View file

@ -1,6 +1,6 @@
{- git-annex command-line JSON output and input
-
- Copyright 2011-2018 Joey Hess <id@joeyh.name>
- Copyright 2011-2020 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
@ -39,6 +39,7 @@ import Data.Monoid
import Prelude
import Types.Messages
import Types.Command (SeekInput(..))
import Key
import Utility.Metered
import Utility.Percentage
@ -64,8 +65,8 @@ type JSONBuilder = Maybe (Object, Bool) -> Maybe (Object, Bool)
none :: JSONBuilder
none = id
start :: String -> Maybe RawFilePath -> Maybe Key -> JSONBuilder
start command file key _ = case j of
start :: String -> Maybe RawFilePath -> Maybe Key -> SeekInput -> JSONBuilder
start command file key si _ = case j of
Object o -> Just (o, False)
_ -> Nothing
where
@ -74,6 +75,7 @@ start command file key _ = case j of
, itemKey = key
, itemFile = fromRawFilePath <$> file
, itemAdded = Nothing
, itemSeekInput = si
}
end :: Bool -> JSONBuilder
@ -176,6 +178,7 @@ data JSONActionItem a = JSONActionItem
, itemKey :: Maybe Key
, itemFile :: Maybe FilePath
, itemAdded :: Maybe a -- for additional fields added by `add`
, itemSeekInput :: SeekInput
}
deriving (Show)
@ -183,10 +186,11 @@ instance ToJSON' (JSONActionItem a) where
toJSON' i = object $ catMaybes
[ Just $ "command" .= itemCommand i
, case itemKey i of
Nothing -> Nothing
Just k -> Just $ "key" .= toJSON' k
Nothing -> Nothing
, Just $ "file" .= toJSON' (itemFile i)
-- itemAdded is not included; must be added later by 'add'
, Just $ "input" .= fromSeekInput (itemSeekInput i)
]
instance FromJSON a => FromJSON (JSONActionItem a) where
@ -195,6 +199,7 @@ instance FromJSON a => FromJSON (JSONActionItem a) where
<*> (maybe (return Nothing) parseJSON =<< (v .:? "key"))
<*> (v .:? "file")
<*> parseadded
<*> pure (SeekInput [])
where
parseadded = (Just <$> parseJSON (Object v)) <|> return Nothing
parseJSON _ = mempty