@@ -11,6 +11,8 @@ module SqlSquared.Signature
1111 , SqlDeclF (..)
1212 , SqlQueryF (..)
1313 , SqlModuleF (..)
14+ , AnyDirPath
15+ , parseAnyDirPath
1416 , printSqlF
1517 , printSqlDeclF
1618 , printSqlQueryF
@@ -58,6 +60,8 @@ import Data.Maybe (Maybe(..))
5860import Data.Monoid (mempty )
5961import Data.Newtype (class Newtype )
6062import Data.NonEmpty ((:|))
63+ import Data.Path.Pathy as Pt
64+ import Data.Path.Pathy.Gen as PtGen
6165import Data.Ord (class Ord1 , compare1 )
6266import Data.String as S
6367import Data.String.Gen as GenS
@@ -138,8 +142,20 @@ data SqlF literal a
138142 | Select (SelectR a )
139143 | Parens a
140144
145+ type AnyDirPath = E.Either (Pt.AbsDir Pt.Unsandboxed ) (Pt.RelDir Pt.Unsandboxed )
146+
147+ printAnyDirPath :: AnyDirPath -> String
148+ printAnyDirPath = E .either Pt .unsafePrintPath Pt .unsafePrintPath
149+
150+ parseAnyDirPath :: forall m . Applicative m => (forall a . String -> m a ) -> String -> m AnyDirPath
151+ parseAnyDirPath fail = Pt .parsePath
152+ (pure ∘ E.Right )
153+ (pure ∘ E.Left )
154+ (const $ fail " incorrect directory path" )
155+ (const $ fail " incorrect directory path" )
156+
141157data SqlDeclF a
142- = Import String
158+ = Import AnyDirPath
143159 | FunctionDecl (FunctionDeclR a )
144160
145161newtype SqlModuleF a =
@@ -502,8 +518,8 @@ printSqlDeclF = case _ of
502518 <> " (" <> F .intercalate " , " (append " :" ∘ ID .printIdent <$> args) <> " ) BEGIN "
503519 <> body
504520 <> " END"
505- Import s →
506- " IMPORT " <> ID .printIdent s
521+ Import path →
522+ " IMPORT " <> ID .printIdent (printAnyDirPath path)
507523
508524printSqlQueryF ∷ Algebra SqlQueryF String
509525printSqlQueryF (Query decls expr) = F .intercalate " ; " $ L .snoc (printSqlDeclF <$> decls) expr
@@ -588,9 +604,9 @@ encodeJsonSqlDeclF = case _ of
588604 J .~> " args" J .:= args
589605 J .~> " body" J .:= body
590606 J .~> J .jsonEmptyObject
591- Import s →
607+ Import path →
592608 " tag" J .:= " import"
593- J .~> " value" J .:= s
609+ J .~> " value" J .:= printAnyDirPath path
594610 J .~> J .jsonEmptyObject
595611
596612encodeJsonSqlQueryF ∷ Algebra SqlQueryF J.Json
@@ -712,7 +728,8 @@ decodeJsonSqlDeclF = J.decodeJson >=> \obj → do
712728
713729 decodeImport obj = do
714730 v ← obj J ..? " value"
715- pure $ Import v
731+ path ← parseAnyDirPath E.Left v
732+ pure $ Import path
716733
717734decodeJsonSqlQueryF ∷ CoalgebraM (E.Either String ) SqlQueryF J.Json
718735decodeJsonSqlQueryF = J .decodeJson >=> \obj → do
@@ -761,16 +778,16 @@ genSqlF genLiteral n
761778 , genSelect n
762779 ]
763780
764- genSqlDeclF ∷ ∀ m . Gen.MonadGen m ⇒ CoalgebraM m SqlDeclF Int
781+ genSqlDeclF ∷ ∀ m . Gen.MonadGen m ⇒ MonadRec m ⇒ CoalgebraM m SqlDeclF Int
765782genSqlDeclF n =
766783 Gen .oneOf $ genImport :|
767784 [ genFunctionDecl n
768785 ]
769786
770- genSqlQueryF ∷ ∀ m . Gen.MonadGen m ⇒ CoalgebraM m SqlQueryF Int
787+ genSqlQueryF ∷ ∀ m . Gen.MonadGen m ⇒ MonadRec m ⇒ CoalgebraM m SqlQueryF Int
771788genSqlQueryF n = Query <$> genDecls n <*> pure n
772789
773- genSqlModuleF ∷ ∀ m . Gen.MonadGen m ⇒ CoalgebraM m SqlModuleF Int
790+ genSqlModuleF ∷ ∀ m . Gen.MonadGen m ⇒ MonadRec m ⇒ CoalgebraM m SqlModuleF Int
774791genSqlModuleF n = Module <$> genDecls n
775792
776793genSetLiteral ∷ ∀ m l . Gen.MonadGen m ⇒ CoalgebraM m (SqlF l ) Int
@@ -878,16 +895,19 @@ genFunctionDecl n = do
878895 args ← L .foldM foldFn L.Nil $ L .range 0 len
879896 pure $ FunctionDecl { ident, args, body: n - 1 }
880897
881- genImport ∷ ∀ m a . Gen.MonadGen m ⇒ m (SqlDeclF a )
882- genImport = Import <$> genIdent
898+ genImport ∷ ∀ m a . Gen.MonadGen m ⇒ MonadRec m ⇒ m (SqlDeclF a )
899+ genImport = map Import
900+ $ Gen .oneOf
901+ $ (Pt .unsandbox >>> E.Left <$> PtGen .genAbsDirPath)
902+ :| [Pt .unsandbox >>> E.Right <$> PtGen .genRelDirPath]
883903
884904genIdent ∷ ∀ m . Gen.MonadGen m ⇒ m String
885905genIdent = do
886906 start ← Gen .elements $ " a" :| S .split (S.Pattern " " ) " bcdefghijklmnopqrstuvwxyz"
887907 body ← map (Int .toStringAs Int .hexadecimal) (Gen .chooseInt 0 100000 )
888908 pure $ start <> body
889909
890- genDecls ∷ ∀ m . Gen.MonadGen m ⇒ Int → m (L.List (SqlDeclF Int ))
910+ genDecls ∷ ∀ m . Gen.MonadGen m ⇒ MonadRec m ⇒ Int → m (L.List (SqlDeclF Int ))
891911genDecls n = do
892912 let
893913 foldFn acc _ = do
0 commit comments