@@ -11,6 +11,8 @@ module SqlSquared.Signature
11
11
, SqlDeclF (..)
12
12
, SqlQueryF (..)
13
13
, SqlModuleF (..)
14
+ , AnyDirPath
15
+ , parseAnyDirPath
14
16
, printSqlF
15
17
, printSqlDeclF
16
18
, printSqlQueryF
@@ -58,6 +60,8 @@ import Data.Maybe (Maybe(..))
58
60
import Data.Monoid (mempty )
59
61
import Data.Newtype (class Newtype )
60
62
import Data.NonEmpty ((:|))
63
+ import Data.Path.Pathy as Pt
64
+ import Data.Path.Pathy.Gen as PtGen
61
65
import Data.Ord (class Ord1 , compare1 )
62
66
import Data.String as S
63
67
import Data.String.Gen as GenS
@@ -138,8 +142,20 @@ data SqlF literal a
138
142
| Select (SelectR a )
139
143
| Parens a
140
144
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
+
141
157
data SqlDeclF a
142
- = Import String
158
+ = Import AnyDirPath
143
159
| FunctionDecl (FunctionDeclR a )
144
160
145
161
newtype SqlModuleF a =
@@ -502,8 +518,8 @@ printSqlDeclF = case _ of
502
518
<> " (" <> F .intercalate " , " (append " :" ∘ ID .printIdent <$> args) <> " ) BEGIN "
503
519
<> body
504
520
<> " END"
505
- Import s →
506
- " IMPORT " <> ID .printIdent s
521
+ Import path →
522
+ " IMPORT " <> ID .printIdent (printAnyDirPath path)
507
523
508
524
printSqlQueryF ∷ Algebra SqlQueryF String
509
525
printSqlQueryF (Query decls expr) = F .intercalate " ; " $ L .snoc (printSqlDeclF <$> decls) expr
@@ -588,9 +604,9 @@ encodeJsonSqlDeclF = case _ of
588
604
J .~> " args" J .:= args
589
605
J .~> " body" J .:= body
590
606
J .~> J .jsonEmptyObject
591
- Import s →
607
+ Import path →
592
608
" tag" J .:= " import"
593
- J .~> " value" J .:= s
609
+ J .~> " value" J .:= printAnyDirPath path
594
610
J .~> J .jsonEmptyObject
595
611
596
612
encodeJsonSqlQueryF ∷ Algebra SqlQueryF J.Json
@@ -712,7 +728,8 @@ decodeJsonSqlDeclF = J.decodeJson >=> \obj → do
712
728
713
729
decodeImport obj = do
714
730
v ← obj J ..? " value"
715
- pure $ Import v
731
+ path ← parseAnyDirPath E.Left v
732
+ pure $ Import path
716
733
717
734
decodeJsonSqlQueryF ∷ CoalgebraM (E.Either String ) SqlQueryF J.Json
718
735
decodeJsonSqlQueryF = J .decodeJson >=> \obj → do
@@ -761,16 +778,16 @@ genSqlF genLiteral n
761
778
, genSelect n
762
779
]
763
780
764
- genSqlDeclF ∷ ∀ m . Gen.MonadGen m ⇒ CoalgebraM m SqlDeclF Int
781
+ genSqlDeclF ∷ ∀ m . Gen.MonadGen m ⇒ MonadRec m ⇒ CoalgebraM m SqlDeclF Int
765
782
genSqlDeclF n =
766
783
Gen .oneOf $ genImport :|
767
784
[ genFunctionDecl n
768
785
]
769
786
770
- genSqlQueryF ∷ ∀ m . Gen.MonadGen m ⇒ CoalgebraM m SqlQueryF Int
787
+ genSqlQueryF ∷ ∀ m . Gen.MonadGen m ⇒ MonadRec m ⇒ CoalgebraM m SqlQueryF Int
771
788
genSqlQueryF n = Query <$> genDecls n <*> pure n
772
789
773
- genSqlModuleF ∷ ∀ m . Gen.MonadGen m ⇒ CoalgebraM m SqlModuleF Int
790
+ genSqlModuleF ∷ ∀ m . Gen.MonadGen m ⇒ MonadRec m ⇒ CoalgebraM m SqlModuleF Int
774
791
genSqlModuleF n = Module <$> genDecls n
775
792
776
793
genSetLiteral ∷ ∀ m l . Gen.MonadGen m ⇒ CoalgebraM m (SqlF l ) Int
@@ -878,16 +895,19 @@ genFunctionDecl n = do
878
895
args ← L .foldM foldFn L.Nil $ L .range 0 len
879
896
pure $ FunctionDecl { ident, args, body: n - 1 }
880
897
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]
883
903
884
904
genIdent ∷ ∀ m . Gen.MonadGen m ⇒ m String
885
905
genIdent = do
886
906
start ← Gen .elements $ " a" :| S .split (S.Pattern " " ) " bcdefghijklmnopqrstuvwxyz"
887
907
body ← map (Int .toStringAs Int .hexadecimal) (Gen .chooseInt 0 100000 )
888
908
pure $ start <> body
889
909
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 ))
891
911
genDecls n = do
892
912
let
893
913
foldFn acc _ = do
0 commit comments