add naive schema comparison

This commit is contained in:
Alexander Foremny
2024-06-06 09:25:05 +02:00
parent bfb98d7675
commit 4003a86325
3 changed files with 20 additions and 14 deletions

View File

@@ -4,7 +4,6 @@ module AutoTypes
) )
where where
import Debug.Trace
import qualified AutoTypes.Unify as U import qualified AutoTypes.Unify as U
import Data.Aeson (Value, decodeFileStrict', encode) import Data.Aeson (Value, decodeFileStrict', encode)
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
@@ -17,7 +16,7 @@ autoTypes fp fps = autoTypes' <$> go fp <*> mapM go (fp : fps)
autoTypes' :: Value -> [Value] -> U.T autoTypes' :: Value -> [Value] -> U.T
autoTypes' t' ts' = autoTypes' t' ts' =
let types = map U.fromJson (Debug.Trace.traceShowId (t' : ts')) let types = map U.fromJson (t' : ts')
in head in head
( foldr1 ( foldr1
(\ls rs -> (concat [U.unify1 l r | l <- ls, r <- rs])) (\ls rs -> (concat [U.unify1 l r | l <- ls, r <- rs]))

View File

@@ -69,7 +69,7 @@ data Commit = Commit
data Collection = Collection data Collection = Collection
{ path :: FilePath, { path :: FilePath,
files :: [FilePath], files :: [FilePath],
schema :: Schema schema :: U.T
} }
deriving (Show) deriving (Show)
@@ -130,10 +130,10 @@ initRepo root ref = do
(value : values) <- do (value : values) <- do
liftIO $ Q.withStore root ref do liftIO $ Q.withStore root ref do
mapM (Q.withCommit cid . Q.readFile) (file : files) mapM (Q.withCommit cid . Q.readFile) (file : files)
let schema = fromAutoTypes path $ U.autoTypes' value values let schema = U.autoTypes' value values
pure $ Collection path files schema pure $ Collection path files schema
let schemaVersion = let schemaVersion =
case lastMay cs of case headMay cs of
Nothing -> Version 1 0 0 Nothing -> Version 1 0 0
Just c' -> Just c' ->
let Version major' minor' patch' = c'.schemaVersion let Version major' minor' patch' = c'.schemaVersion
@@ -159,8 +159,8 @@ initRepo root ref = do
cs cs
compareSchemas :: compareSchemas ::
M.Map String Schema -> M.Map String U.T ->
M.Map String Schema -> M.Map String U.T ->
Maybe SchemaDifference Maybe SchemaDifference
compareSchemas schemas' schemas = compareSchemas schemas' schemas =
maximumMay maximumMay
@@ -174,19 +174,24 @@ compareSchemas schemas' schemas =
schemas' schemas'
schemas schemas
where where
compareSchemas' Nothing (Just _) = Just Patch compareSchemas' Nothing Nothing = Nothing
compareSchemas' (Just _) Nothing = Just Patch compareSchemas' Nothing (Just _) = Just Minor
compareSchemas' (Just _) Nothing = Just Major
compareSchemas' (Just schema') (Just schema) = compareSchema schema' schema compareSchemas' (Just schema') (Just schema) = compareSchema schema' schema
-- TODO compareSchema :: U.T -> U.T -> Maybe SchemaDifference
compareSchema :: Schema -> Schema -> Maybe SchemaDifference compareSchema (U.Object kts') (U.Object kts) = compareSchemas kts' kts
compareSchema schema' schema = Nothing compareSchema t' t
| t' == t = Nothing
| t' `elem` (U.unify1 t' t) = Just Patch
| t `elem` U.unify1 t' t = Just Minor
| otherwise = Just Major
data SchemaDifference data SchemaDifference
= Major = Major
| Minor | Minor
| Patch | Patch
deriving (Eq, Ord) deriving (Show, Eq, Ord)
main :: IO () main :: IO ()
main = do main = do
@@ -202,7 +207,8 @@ main = do
Right (SchemaJson path) -> do Right (SchemaJson path) -> do
repo <- atomically (readTMVar repoT) repo <- atomically (readTMVar repoT)
let [c] = filter ((== path) . (.path)) (last repo.commits).collections let [c] = filter ((== path) . (.path)) (last repo.commits).collections
respond $ W.responseLBS W.status200 [] (J.encode c.schema) respond . W.responseLBS W.status200 [] $
J.encode (fromAutoTypes path c.schema)
Right Query -> do Right Query -> do
q <- q <-
fromString @Q.Query . LB.toString fromString @Q.Query . LB.toString

View File

@@ -32,6 +32,7 @@ rec {
]; ];
buildInputs = [ buildInputs = [
haskellPackages.astore haskellPackages.astore
haskellPackages.autotypes
haskellPackages.cabal-install haskellPackages.cabal-install
haskellPackages.ormolu haskellPackages.ormolu
(pkgs.writeScriptBin "reload" '' (pkgs.writeScriptBin "reload" ''