@@ -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
@@ -140,8 +144,20 @@ data SqlF literal a
140144 | Select (SelectR a )
141145 | Parens a
142146
147+ type AnyDirPath = E.Either (Pt.AbsDir Pt.Unsandboxed ) (Pt.RelDir Pt.Unsandboxed )
148+
149+ printAnyDirPath :: AnyDirPath -> String
150+ printAnyDirPath = E .either Pt .unsafePrintPath Pt .unsafePrintPath
151+
152+ parseAnyDirPath :: forall m . Applicative m => (forall a . String -> m a ) -> String -> m AnyDirPath
153+ parseAnyDirPath fail = Pt .parsePath
154+ (pure ∘ E.Right )
155+ (pure ∘ E.Left )
156+ (const $ fail " incorrect directory path" )
157+ (const $ fail " incorrect directory path" )
158+
143159data SqlDeclF a
144- = Import String
160+ = Import AnyDirPath
145161 | FunctionDecl (FunctionDeclR a )
146162
147163newtype SqlModuleF a =
@@ -504,8 +520,8 @@ printSqlDeclF = case _ of
504520 <> " (" <> F .intercalate " , " (append " :" ∘ ID .printIdent <$> args) <> " ) BEGIN "
505521 <> body
506522 <> " END"
507- Import s →
508- " IMPORT " <> ID .printIdent s
523+ Import path →
524+ " IMPORT " <> ID .printIdent (printAnyDirPath path)
509525
510526printSqlQueryF ∷ Algebra SqlQueryF String
511527printSqlQueryF (Query decls expr) = F .intercalate " ; " $ L .snoc (printSqlDeclF <$> decls) expr
@@ -590,9 +606,9 @@ encodeJsonSqlDeclF = case _ of
590606 J .~> " args" J .:= args
591607 J .~> " body" J .:= body
592608 J .~> J .jsonEmptyObject
593- Import s →
609+ Import path →
594610 " tag" J .:= " import"
595- J .~> " value" J .:= s
611+ J .~> " value" J .:= printAnyDirPath path
596612 J .~> J .jsonEmptyObject
597613
598614encodeJsonSqlQueryF ∷ Algebra SqlQueryF J.Json
@@ -714,7 +730,8 @@ decodeJsonSqlDeclF = J.decodeJson >=> \obj → do
714730
715731 decodeImport obj = do
716732 v ← obj J ..? " value"
717- pure $ Import v
733+ path ← parseAnyDirPath E.Left v
734+ pure $ Import path
718735
719736decodeJsonSqlQueryF ∷ CoalgebraM (E.Either String ) SqlQueryF J.Json
720737decodeJsonSqlQueryF = J .decodeJson >=> \obj → do
@@ -879,7 +896,10 @@ genFunctionDecl n = do
879896 pure $ FunctionDecl { ident, args, body: n - 1 }
880897
881898genImport ∷ ∀ a . Gen.Gen (SqlDeclF a )
882- genImport = Import <$> genIdent
899+ genImport = map Import
900+ $ Gen .oneOf
901+ $ (Pt .unsandbox >>> E.Left <$> PtGen .genAbsDirPath)
902+ :| [Pt .unsandbox >>> E.Right <$> PtGen .genRelDirPath]
883903
884904genIdent ∷ Gen.Gen String
885905genIdent = do
0 commit comments