Add simple load and store methods for Metadata
This commit is contained in:
parent
c3584df5dc
commit
ae7f0bdac4
3 changed files with 54 additions and 1 deletions
|
|
@ -9,12 +9,13 @@ description: Please see the README on GitHub at <https://github.com/Garmelon/mim
|
|||
github: Garmelon/mima-tools
|
||||
|
||||
extra-source-files:
|
||||
- README.md
|
||||
- README.md
|
||||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
- OddWord >= 1.0 && < 1.1
|
||||
- aeson
|
||||
- aeson-pretty
|
||||
- bytestring
|
||||
- containers
|
||||
- text
|
||||
|
|
|
|||
39
src/Mima/Vm/Storage.hs
Normal file
39
src/Mima/Vm/Storage.hs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
module Mima.Vm.Storage
|
||||
(
|
||||
-- * Methods for loading/storing Metadata
|
||||
loadMetadata
|
||||
, saveMetadata
|
||||
-- * Test methods
|
||||
, roundTripFile
|
||||
) where
|
||||
|
||||
import Data.Aeson
|
||||
import Data.Aeson.Encode.Pretty
|
||||
import qualified Data.ByteString.Lazy as BSL
|
||||
import qualified Data.Text as T
|
||||
import Mima.Run
|
||||
import Mima.Vm.Metadata
|
||||
|
||||
-- | Loads 'Metadata' from a given file path
|
||||
loadMetadata :: FilePath -> Run Metadata
|
||||
loadMetadata path = do
|
||||
file <- readFileBS path
|
||||
let decoded = eitherDecode (BSL.fromStrict file) :: Either String Metadata
|
||||
case decoded of
|
||||
Left msg -> throw (T.pack msg)
|
||||
Right metadata -> pure metadata
|
||||
|
||||
-- | Stores prettified 'Metadata' in a given file.
|
||||
saveMetadata :: FilePath -> Metadata -> Run ()
|
||||
saveMetadata path metadata = writeFileBS path (BSL.toStrict (encodePretty metadata))
|
||||
|
||||
-- | A garbage test method that resds the input file, parses it and writes the
|
||||
-- prettified result back in the output file.
|
||||
--
|
||||
-- Can be used with the example file:
|
||||
--
|
||||
-- > roundTripFile "test/files/SimpleMetadataFile.json" "/tmp/test.json"
|
||||
roundTripFile :: FilePath -- ^ The input file
|
||||
-> FilePath -- ^ The output file
|
||||
-> Run ()
|
||||
roundTripFile input output = loadMetadata input >>= saveMetadata output
|
||||
13
test/files/SimpleMetadataFile.json
Normal file
13
test/files/SimpleMetadataFile.json
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"global": {
|
||||
"some-data": "some value"
|
||||
},
|
||||
"local": [
|
||||
{
|
||||
"at": 20,
|
||||
"info": {
|
||||
"src-line": 200
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue