Commit 5fc26764 authored by Sophie Herold's avatar Sophie Herold 🌼

Adds "did you mean" for YamSql fields

parent 8f5bde5d
Pipeline #701 failed with stage
in 6 minutes and 28 seconds
v0.10.0.0 (WIP)
- Adds support for reading setup.yml from STDIN
- Adds "did you mean" for YamSql fields
- Changes to better support SEQUENCEs
v0.9.0.0
- Fixes DELETE BASE before schema definitions are read (#64)
......
......@@ -97,7 +97,8 @@ library
text >=1.2 && <1.3,
transformers >=0.5 && <0.6,
unordered-containers >=0.2 && <0.3,
yaml >=0.8 && <0.9
yaml >=0.8 && <0.9,
edit-distance
ghc-options:
-Wall
......
......@@ -73,8 +73,8 @@ class (Typeable a, ToSqlCode a, Show a) =>
data SqlObj a b where
SqlObj
:: (SqlObjType a, SqlIdContent b)
=> a -- ^ sqlObjType
-> b -- ^ sqlObjId
=> a -- sqlObjType
-> b -- sqlObjId
-> SqlObj a b
sqlObjType :: SqlObj a b -> a
......
......@@ -23,11 +23,12 @@ import Data.Aeson.Types
import Data.Char
import Data.Data
import Data.HashMap.Strict (keys)
import Data.List
import Data.List ((\\), minimumBy)
import qualified Data.Text as T
import Data.Yaml
import GHC.Generics
import System.IO
import Text.EditDistance (defaultEditCosts, levenshteinDistance)
import Database.HamSql.Internal.Utils
......@@ -74,13 +75,25 @@ parseYamSql xs = do
return $
if null diff
then parsed
else throw $ YamsqlException $ "Found unknown keys: " <> tshow diff
else throw $
YamsqlException $
"Found unknown YamSql fields: " <>
T.concat (map (explain (keysOfData parsed)) diff)
where
keysOfData u =
"tag" : map (snakeify . removeFirstPart) (constrFields (toConstr u))
keysOfValue :: Value -> [String]
keysOfValue (Object ys) = map T.unpack $ keys ys
keysOfValue _ = err "HAMSQL-UNEXPECTED 3"
explainMissing :: [String] -> String -> Text
explainMissing ys x =
"\n - " <> tshow x <> " (did you mean " <> tshow (closestString x ys) <>
"?)"
closestString :: String -> [String] -> String
closestString x = minimumBy (\y y' -> compare (dist y) (dist y'))
where
dist = levenshteinDistance defaultEditCosts x
toYamSqlJson
:: (Generic a, GToJSON Zero (Rep a))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment