Skip to content

Commit

Permalink
Merge pull request #34 from ccap/testing
Browse files Browse the repository at this point in the history
Fix import deduplication

Reviewed-by: jasonw
  • Loading branch information
aij authored Jun 28, 2019
2 parents 5b7c9ee + 157fc7b commit 5e216cb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 28 deletions.
8 changes: 7 additions & 1 deletion runtests
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e -x

pulp test
pulp build

case $1 in
ccap)
Expand Down Expand Up @@ -31,3 +31,9 @@ for i in samples/*.tmpl; do

# TODO: Test compilation of generated output.
done

# We can at least test compile this one for now

./compile -p Test.Generated -m purs -o test/generated samples/SelfContained.tmpl

pulp test
19 changes: 6 additions & 13 deletions samples/County.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ module County /* <scala package="gov.wicourts.codegen"> <purs modulePrefix="Ccap
type AssessFacilityId: wrap Int
type SomethingMoney: wrap Decimal

type Bunnies: wrap Int
<purs
t="Test.Support.Newint.Newint"
decode="Data.Newtype.wrap"
encode="Data.Newtype.unwrap">

type County: {
countyNo: Domains.CountyNoT
countyNoX: CountyNo
Expand Down Expand Up @@ -66,17 +72,4 @@ module County /* <scala package="gov.wicourts.codegen"> <purs modulePrefix="Ccap
| Etc
]

// A date, encoded per RFC 3339.
type Date: wrap String
<validations maxLength="32">
<scala
t="org.joda.time.LocalDate"
decode="Decoder.date"
encode="gov.wicourts.common.LocalDateOps.sortableDateFormat.print">
<purs
t="Ccap.Common.DateTime.Date.Date"
decode="Ccap.Common.DateTime.Date.decodeIsoMadison"
encode="Ccap.Common.DateTime.Date.inIso8601ExtendedDateFormat">

type Mydate: wrap Date
}
54 changes: 54 additions & 0 deletions samples/SelfContained.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// A self-contained example that we can compile without additional
// dependencies.
module SelfContained {
type CountyNo: Int
type CountyName: String <validations maxLength="32">
type OtherCountyName: wrap String <validations maxLength="32">
type AssessFacilityId: wrap Int
type SomethingMoney: wrap Decimal

// We can wrap a type in a pre-existing newtype
type Bunnies: wrap Int
<purs
t="Test.Support.Newint"
decode="Ccap.Codegen.Runtime.decodeNewtype"
encode="Data.Newtype.unwrap">

type County: {
countyNo: CountyNo
countyName: CountyName
assessFacilityId: Maybe AssessFacilityId
soapPort: Int
}

type Big: {
a1: Int
b1: String
c1: Decimal
d1: Boolean
a2: Int
b2: String
c2: Decimal
d2: Boolean
a3: Int
b3: String
c3: Decimal
d3: Boolean
a4: Int
b4: String
c4: Decimal
d4: Boolean
}

type Response: {
counties: Array County
}

type CountyEnum: [
| Adams
| Ashland
| Baron
| Etc
]

}
21 changes: 8 additions & 13 deletions src/Ccap/Codegen/Purescript.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import Control.Monad.Writer (class MonadTell, Writer, runWriter, tell)
import Data.Array ((:))
import Data.Array as Array
import Data.Array.NonEmpty as NonEmptyArray
import Data.Filterable (compact)
import Data.Foldable (any, intercalate)
import Data.Foldable (intercalate)
import Data.Function (on)
import Data.Maybe (Maybe(..), fromMaybe, isNothing, maybe)
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.String (Pattern(..))
import Data.String as String
import Data.Traversable (for, for_, traverse)
Expand Down Expand Up @@ -57,16 +56,12 @@ mergeImports :: Array PsImport -> Array PsImport
mergeImports imps =
let
sorted = Array.sortBy ((compare `on` _.mod) <> (compare `on` _.alias)) imps
grouped = Array.groupBy (\a b -> a.mod == b.mod && b.alias == b.alias) sorted
in grouped <#> \group -> do
let typs = map _.typ group
{ mod: (NonEmptyArray.head group).mod
, alias: (NonEmptyArray.head group).alias
, typ:
if any isNothing typs
then Nothing
else Just $ intercalate ", " (Array.nub <<< compact <<< NonEmptyArray.toArray $ typs)
}
grouped = Array.groupBy (\a b -> a.mod == b.mod && a.alias == b.alias) sorted
in grouped <#> \group ->
(NonEmptyArray.head group)
{ typ = traverse _.typ group <#> NonEmptyArray.toArray
>>> Array.sort >>> Array.nub >>> intercalate ", "
}

outputSpec :: String -> Array Module -> OutputSpec
outputSpec defaultModulePrefix modules =
Expand Down
5 changes: 4 additions & 1 deletion src/Ccap/Codegen/Runtime.purs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@ codec_custom
-> Codec a t
codec_custom decode encode = composeCodec { decode, encode }

decodeNewtype :: forall t a. Newtype t a => a -> Either String t
decodeNewtype a = Right $ wrap a

codec_newtype
:: forall t a b
. Newtype t b
=> Codec a b
-> Codec a t
codec_newtype = composeCodec
{ decode: Right <<< wrap
{ decode: decodeNewtype
, encode: unwrap
}
7 changes: 7 additions & 0 deletions test/Support.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Test.Support where

import Data.Newtype

newtype Newint = Newint Int

derive instance newtypeNewint :: Newtype Newint _

0 comments on commit 5e216cb

Please sign in to comment.