diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-06-06 09:25:05 +0200 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-06-06 09:36:09 +0200 |
commit | 4003a863256352a43161eba44abecf986e76092c (patch) | |
tree | c0feccf6442cdac7abbd3f560d765efb072a4471 | |
parent | bfb98d7675515394e1b9a0417bfafc83d775611c (diff) |
add naive schema comparison
-rw-r--r-- | autotypes/src/AutoTypes.hs | 3 | ||||
-rw-r--r-- | backend/app/Main.hs | 30 | ||||
-rw-r--r-- | default.nix | 1 |
3 files changed, 20 insertions, 14 deletions
diff --git a/autotypes/src/AutoTypes.hs b/autotypes/src/AutoTypes.hs index c5a43d1..826d6c6 100644 --- a/autotypes/src/AutoTypes.hs +++ b/autotypes/src/AutoTypes.hs @@ -4,7 +4,6 @@ module AutoTypes ) where -import Debug.Trace import qualified AutoTypes.Unify as U import Data.Aeson (Value, decodeFileStrict', encode) import Data.Maybe (fromJust) @@ -17,7 +16,7 @@ autoTypes fp fps = autoTypes' <$> go fp <*> mapM go (fp : fps) autoTypes' :: Value -> [Value] -> U.T autoTypes' t' ts' = - let types = map U.fromJson (Debug.Trace.traceShowId (t' : ts')) + let types = map U.fromJson (t' : ts') in head ( foldr1 (\ls rs -> (concat [U.unify1 l r | l <- ls, r <- rs])) diff --git a/backend/app/Main.hs b/backend/app/Main.hs index 6742ad2..d26259b 100644 --- a/backend/app/Main.hs +++ b/backend/app/Main.hs @@ -69,7 +69,7 @@ data Commit = Commit data Collection = Collection { path :: FilePath, files :: [FilePath], - schema :: Schema + schema :: U.T } deriving (Show) @@ -130,10 +130,10 @@ initRepo root ref = do (value : values) <- do liftIO $ Q.withStore root ref do 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 let schemaVersion = - case lastMay cs of + case headMay cs of Nothing -> Version 1 0 0 Just c' -> let Version major' minor' patch' = c'.schemaVersion @@ -159,8 +159,8 @@ initRepo root ref = do cs compareSchemas :: - M.Map String Schema -> - M.Map String Schema -> + M.Map String U.T -> + M.Map String U.T -> Maybe SchemaDifference compareSchemas schemas' schemas = maximumMay @@ -174,19 +174,24 @@ compareSchemas schemas' schemas = schemas' schemas where - compareSchemas' Nothing (Just _) = Just Patch - compareSchemas' (Just _) Nothing = Just Patch + compareSchemas' Nothing Nothing = Nothing + compareSchemas' Nothing (Just _) = Just Minor + compareSchemas' (Just _) Nothing = Just Major compareSchemas' (Just schema') (Just schema) = compareSchema schema' schema --- TODO -compareSchema :: Schema -> Schema -> Maybe SchemaDifference -compareSchema schema' schema = Nothing +compareSchema :: U.T -> U.T -> Maybe SchemaDifference +compareSchema (U.Object kts') (U.Object kts) = compareSchemas kts' kts +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 = Major | Minor | Patch - deriving (Eq, Ord) + deriving (Show, Eq, Ord) main :: IO () main = do @@ -202,7 +207,8 @@ main = do Right (SchemaJson path) -> do repo <- atomically (readTMVar repoT) 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 q <- fromString @Q.Query . LB.toString diff --git a/default.nix b/default.nix index afea1a3..901ca45 100644 --- a/default.nix +++ b/default.nix @@ -32,6 +32,7 @@ rec { ]; buildInputs = [ haskellPackages.astore + haskellPackages.autotypes haskellPackages.cabal-install haskellPackages.ormolu (pkgs.writeScriptBin "reload" '' |