1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 13:02:37 +03:00

Bit more nuanced of a monoid instance for Value

This commit is contained in:
Timothy Clem 2017-05-04 14:56:53 -07:00
parent 702198ec91
commit bea5d66f3f

View File

@ -9,15 +9,14 @@ module Renderer.JSON
import Alignment
import Data.Aeson (ToJSON, toJSON, encode, object, (.=))
import Data.Aeson as A hiding (json)
import Data.Aeson.Types (emptyArray)
import Data.Bifunctor.Join
import Data.Functor.Both
import Data.Record
import Data.These
import Data.Vector as Vector hiding (toList)
import Data.Vector as Vector
import Diff
import Info
import Prologue
import Prologue hiding ((++))
import qualified Data.Map as Map
import Source
import SplitDiff
@ -179,8 +178,12 @@ instance ToJSON a => ToJSON (File a) where
toJSON File{..} = object [ "filePath" .= filePath, "programNode" .= fileContent ]
instance Monoid Value where
mempty = emptyArray
mappend a b = A.Array $ Vector.fromList [a, b]
mempty = Null
mappend a b | Null <- b = A.Array (singleton a)
| Null <- a = A.Array (singleton b)
| A.Array b' <- b = A.Array (singleton a ++ b')
| A.Array a' <- a = A.Array (a' ++ singleton b)
| otherwise = A.Array (fromList [a, b])
instance StringConv Value ByteString where
strConv _ = toS . (<> "\n") . encode