Skip to content

Commit 5e216cb

Browse files
authored
Merge pull request #34 from ccap/testing
Fix import deduplication Reviewed-by: jasonw
2 parents 5b7c9ee + 157fc7b commit 5e216cb

6 files changed

Lines changed: 86 additions & 28 deletions

File tree

runtests

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e -x
44

5-
pulp test
5+
pulp build
66

77
case $1 in
88
ccap)
@@ -31,3 +31,9 @@ for i in samples/*.tmpl; do
3131

3232
# TODO: Test compilation of generated output.
3333
done
34+
35+
# We can at least test compile this one for now
36+
37+
./compile -p Test.Generated -m purs -o test/generated samples/SelfContained.tmpl
38+
39+
pulp test

samples/County.tmpl

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ module County /* <scala package="gov.wicourts.codegen"> <purs modulePrefix="Ccap
2828
type AssessFacilityId: wrap Int
2929
type SomethingMoney: wrap Decimal
3030

31+
type Bunnies: wrap Int
32+
<purs
33+
t="Test.Support.Newint.Newint"
34+
decode="Data.Newtype.wrap"
35+
encode="Data.Newtype.unwrap">
36+
3137
type County: {
3238
countyNo: Domains.CountyNoT
3339
countyNoX: CountyNo
@@ -66,17 +72,4 @@ module County /* <scala package="gov.wicourts.codegen"> <purs modulePrefix="Ccap
6672
| Etc
6773
]
6874

69-
// A date, encoded per RFC 3339.
70-
type Date: wrap String
71-
<validations maxLength="32">
72-
<scala
73-
t="org.joda.time.LocalDate"
74-
decode="Decoder.date"
75-
encode="gov.wicourts.common.LocalDateOps.sortableDateFormat.print">
76-
<purs
77-
t="Ccap.Common.DateTime.Date.Date"
78-
decode="Ccap.Common.DateTime.Date.decodeIsoMadison"
79-
encode="Ccap.Common.DateTime.Date.inIso8601ExtendedDateFormat">
80-
81-
type Mydate: wrap Date
8275
}

samples/SelfContained.tmpl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// A self-contained example that we can compile without additional
2+
// dependencies.
3+
module SelfContained {
4+
type CountyNo: Int
5+
type CountyName: String <validations maxLength="32">
6+
type OtherCountyName: wrap String <validations maxLength="32">
7+
type AssessFacilityId: wrap Int
8+
type SomethingMoney: wrap Decimal
9+
10+
// We can wrap a type in a pre-existing newtype
11+
type Bunnies: wrap Int
12+
<purs
13+
t="Test.Support.Newint"
14+
decode="Ccap.Codegen.Runtime.decodeNewtype"
15+
encode="Data.Newtype.unwrap">
16+
17+
type County: {
18+
countyNo: CountyNo
19+
countyName: CountyName
20+
assessFacilityId: Maybe AssessFacilityId
21+
soapPort: Int
22+
}
23+
24+
type Big: {
25+
a1: Int
26+
b1: String
27+
c1: Decimal
28+
d1: Boolean
29+
a2: Int
30+
b2: String
31+
c2: Decimal
32+
d2: Boolean
33+
a3: Int
34+
b3: String
35+
c3: Decimal
36+
d3: Boolean
37+
a4: Int
38+
b4: String
39+
c4: Decimal
40+
d4: Boolean
41+
}
42+
43+
type Response: {
44+
counties: Array County
45+
}
46+
47+
type CountyEnum: [
48+
| Adams
49+
| Ashland
50+
| Baron
51+
| Etc
52+
]
53+
54+
}

src/Ccap/Codegen/Purescript.purs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import Control.Monad.Writer (class MonadTell, Writer, runWriter, tell)
1313
import Data.Array ((:))
1414
import Data.Array as Array
1515
import Data.Array.NonEmpty as NonEmptyArray
16-
import Data.Filterable (compact)
17-
import Data.Foldable (any, intercalate)
16+
import Data.Foldable (intercalate)
1817
import Data.Function (on)
19-
import Data.Maybe (Maybe(..), fromMaybe, isNothing, maybe)
18+
import Data.Maybe (Maybe(..), fromMaybe, maybe)
2019
import Data.String (Pattern(..))
2120
import Data.String as String
2221
import Data.Traversable (for, for_, traverse)
@@ -57,16 +56,12 @@ mergeImports :: Array PsImport -> Array PsImport
5756
mergeImports imps =
5857
let
5958
sorted = Array.sortBy ((compare `on` _.mod) <> (compare `on` _.alias)) imps
60-
grouped = Array.groupBy (\a b -> a.mod == b.mod && b.alias == b.alias) sorted
61-
in grouped <#> \group -> do
62-
let typs = map _.typ group
63-
{ mod: (NonEmptyArray.head group).mod
64-
, alias: (NonEmptyArray.head group).alias
65-
, typ:
66-
if any isNothing typs
67-
then Nothing
68-
else Just $ intercalate ", " (Array.nub <<< compact <<< NonEmptyArray.toArray $ typs)
69-
}
59+
grouped = Array.groupBy (\a b -> a.mod == b.mod && a.alias == b.alias) sorted
60+
in grouped <#> \group ->
61+
(NonEmptyArray.head group)
62+
{ typ = traverse _.typ group <#> NonEmptyArray.toArray
63+
>>> Array.sort >>> Array.nub >>> intercalate ", "
64+
}
7065

7166
outputSpec :: String -> Array Module -> OutputSpec
7267
outputSpec defaultModulePrefix modules =

src/Ccap/Codegen/Runtime.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ codec_custom
9999
-> Codec a t
100100
codec_custom decode encode = composeCodec { decode, encode }
101101

102+
decodeNewtype :: forall t a. Newtype t a => a -> Either String t
103+
decodeNewtype a = Right $ wrap a
104+
102105
codec_newtype
103106
:: forall t a b
104107
. Newtype t b
105108
=> Codec a b
106109
-> Codec a t
107110
codec_newtype = composeCodec
108-
{ decode: Right <<< wrap
111+
{ decode: decodeNewtype
109112
, encode: unwrap
110113
}

test/Support.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Test.Support where
2+
3+
import Data.Newtype
4+
5+
newtype Newint = Newint Int
6+
7+
derive instance newtypeNewint :: Newtype Newint _

0 commit comments

Comments
 (0)