1
1
mirror of https://github.com/github/semantic.git synced 2025-01-04 13:34:31 +03:00

Define BlobPair as a newtype.

This commit is contained in:
Rob Rix 2019-10-17 05:16:23 -04:00
parent 0759211f51
commit 2604e94052
No known key found for this signature in database
GPG Key ID: F188A01508EA1CF7
4 changed files with 12 additions and 11 deletions

View File

@ -10,7 +10,7 @@ module Control.Effect.Parse
import Control.Effect.Carrier import Control.Effect.Carrier
import Control.Effect.Error import Control.Effect.Error
import Control.Exception (SomeException) import Control.Exception (SomeException)
import Data.Bifunctor.Join import Data.Bitraversable
import Data.Blob import Data.Blob
import Data.Language import Data.Language
import qualified Data.Map as Map import qualified Data.Map as Map
@ -56,5 +56,5 @@ parsePairWith
-> BlobPair -- ^ The blob pair to parse. -> BlobPair -- ^ The blob pair to parse.
-> m a -> m a
parsePairWith parsers with blobPair = case Map.lookup (languageForBlobPair blobPair) parsers of parsePairWith parsers with blobPair = case Map.lookup (languageForBlobPair blobPair) parsers of
Just (SomeParser parser) -> traverse (parse parser) blobPair >>= with . runJoin Just (SomeParser parser) -> bitraverse (parse parser) (parse parser) (getBlobPair blobPair) >>= with
_ -> noLanguageForBlob (pathForBlobPair blobPair) _ -> noLanguageForBlob (pathForBlobPair blobPair)

View File

@ -13,7 +13,7 @@ module Data.Blob
, nullBlob , nullBlob
, sourceBlob , sourceBlob
, noLanguageForBlob , noLanguageForBlob
, type BlobPair , BlobPair(..)
, pattern Diffing , pattern Diffing
, pattern Inserting , pattern Inserting
, pattern Deleting , pattern Deleting
@ -101,7 +101,8 @@ noLanguageForBlob blobPath = throwError (SomeException (NoLanguageForBlob blobPa
-- | Represents a blobs suitable for diffing which can be either a blob to -- | Represents a blobs suitable for diffing which can be either a blob to
-- delete, a blob to insert, or a pair of blobs to diff. -- delete, a blob to insert, or a pair of blobs to diff.
type BlobPair = Join These Blob newtype BlobPair = BlobPair { getBlobPair :: These Blob Blob }
deriving (Eq, Show)
instance FromJSON BlobPair where instance FromJSON BlobPair where
parseJSON = withObject "BlobPair" $ \o -> do parseJSON = withObject "BlobPair" $ \o -> do
@ -114,13 +115,13 @@ instance FromJSON BlobPair where
_ -> Prelude.fail "Expected object with 'before' and/or 'after' keys only" _ -> Prelude.fail "Expected object with 'before' and/or 'after' keys only"
pattern Diffing :: Blob -> Blob -> BlobPair pattern Diffing :: Blob -> Blob -> BlobPair
pattern Diffing a b = Join (These a b) pattern Diffing a b = BlobPair (These a b)
pattern Inserting :: Blob -> BlobPair pattern Inserting :: Blob -> BlobPair
pattern Inserting a = Join (That a) pattern Inserting a = BlobPair (That a)
pattern Deleting :: Blob -> BlobPair pattern Deleting :: Blob -> BlobPair
pattern Deleting b = Join (This b) pattern Deleting b = BlobPair (This b)
{-# COMPLETE Diffing, Inserting, Deleting #-} {-# COMPLETE Diffing, Inserting, Deleting #-}
@ -151,7 +152,7 @@ languageTagForBlobPair pair = showLanguage (languageForBlobPair pair)
where showLanguage = pure . (,) "language" . show where showLanguage = pure . (,) "language" . show
pathKeyForBlobPair :: BlobPair -> FilePath pathKeyForBlobPair :: BlobPair -> FilePath
pathKeyForBlobPair blobs = case bimap blobPath blobPath (runJoin blobs) of pathKeyForBlobPair blobs = case bimap blobPath blobPath (getBlobPair blobs) of
This before -> before This before -> before
That after -> after That after -> after
These before after | before == after -> after These before after | before == after -> after

View File

@ -56,8 +56,8 @@ newtype JSONStat = JSONStat { jsonStatBlobs :: BlobPair }
deriving (Eq, Show) deriving (Eq, Show)
instance ToJSON JSONStat where instance ToJSON JSONStat where
toJSON JSONStat{..} = object ("path" .= pathKeyForBlobPair jsonStatBlobs : toJSONFields (these Delete Insert Replace (runJoin jsonStatBlobs))) toJSON JSONStat{..} = object ("path" .= pathKeyForBlobPair jsonStatBlobs : toJSONFields (these Delete Insert Replace (getBlobPair jsonStatBlobs)))
toEncoding JSONStat{..} = pairs (fold ("path" .= pathKeyForBlobPair jsonStatBlobs : toJSONFields (these Delete Insert Replace (runJoin jsonStatBlobs)))) toEncoding JSONStat{..} = pairs (fold ("path" .= pathKeyForBlobPair jsonStatBlobs : toJSONFields (these Delete Insert Replace (getBlobPair jsonStatBlobs))))
-- | Render a term to a value representing its JSON. -- | Render a term to a value representing its JSON.
renderJSONTerm :: ToJSON a => Blob -> a -> JSON "trees" SomeJSON renderJSONTerm :: ToJSON a => Blob -> a -> JSON "trees" SomeJSON

View File

@ -184,7 +184,7 @@ decoratingDiffWith
-> BlobPair -- ^ The blob pair to parse. -> BlobPair -- ^ The blob pair to parse.
-> m output -> m output
decoratingDiffWith parsers decorate render blobPair = parsePairWith parsers (render <=< diffTerms blobPair . bimap (decorate blobL) (decorate blobR)) blobPair where decoratingDiffWith parsers decorate render blobPair = parsePairWith parsers (render <=< diffTerms blobPair . bimap (decorate blobL) (decorate blobR)) blobPair where
(blobL, blobR) = fromThese errorBlob errorBlob (runJoin blobPair) (blobL, blobR) = fromThese errorBlob errorBlob (getBlobPair blobPair)
errorBlob = Prelude.error "evaluating blob on absent side" errorBlob = Prelude.error "evaluating blob on absent side"
diffTerms :: (DiffTerms term, Member Telemetry sig, Carrier sig m, MonadIO m) diffTerms :: (DiffTerms term, Member Telemetry sig, Carrier sig m, MonadIO m)