Skip to content

Commit

Permalink
Merge pull request #85 from jtaylorX2/aliased-types
Browse files Browse the repository at this point in the history
Don't generated type definitions for aliased types
  • Loading branch information
pbrant authored Mar 17, 2022
2 parents 5e684c7 + 04a19a7 commit 91d4b82
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Ccap/Codegen/Database.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Data.Array as Array
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NonEmptyArray
import Data.Either (note)
import Data.Foldable (any)
import Data.Maybe (Maybe(..), isNothing, maybe)
import Data.Monoid (guard)
import Database.PostgreSQL (Connection, PGError(..))
Expand All @@ -27,6 +28,14 @@ type Domain
, charMaxLen :: Maybe Int
}

type AliasedType
= { name :: String
, type :: Cst.Type
}

aliasedTypes :: Array AliasedType
aliasedTypes = [ { name: "CaseNoT", type: Cst.Ref emptyPos { mod: Just (Cst.ModuleRef "CaseNoSupport"), typ: "CaseNo", params: [] } } ]

rowToDomain :: Row3 String String (Maybe Int) -> Domain
rowToDomain (Row3 domainName dataType charMaxLen) =
{ domainName
Expand All @@ -52,7 +61,14 @@ domainModule pool scalaPkg pursPkg =
results <- query conn (Query sql) Row0
let
types = Array.sortWith Cst.typeDeclName $ domainTypeDecl <<< rowToDomain <$> results
nelTypes <- except ((note (ConversionError "Expected at least one type")) (NonEmptyArray.fromArray types))

typesWithoutAlias =
Array.filter
( \(Cst.TypeDecl typeDecl) ->
not (any (\aliasedType -> typeDecl.name == aliasedType.name) aliasedTypes)
)
types
nelTypes <- except ((note (ConversionError "Expected at least one type")) (NonEmptyArray.fromArray typesWithoutAlias))
pure
{ types: nelTypes
, imports: types >>= tableImports # Array.nub # Array.sort
Expand Down Expand Up @@ -160,9 +176,9 @@ dbRecordProp col@{ columnName, domainName, dataType, isNullable } =
{ name: columnName, typ: Cst.TType optioned, annots, position: emptyPos }

domainRef :: String -> Cst.Type
domainRef = case _ of
"CaseNoT" -> Cst.Ref emptyPos { mod: Just (Cst.ModuleRef "CaseNoSupport"), typ: "CaseNo", params: [] }
name -> Cst.Ref emptyPos { mod: Just (Cst.ModuleRef "Domains"), typ: name, params: [] }
domainRef domainName = case Array.find (\aliasedType -> aliasedType.name == domainName) aliasedTypes of
Just aliasedType -> aliasedType.type
Nothing -> Cst.Ref emptyPos { mod: Just (Cst.ModuleRef "Domains"), typ: domainName, params: [] }

dbType :: String -> Cst.Type
dbType dataType = case dataType of
Expand Down

0 comments on commit 91d4b82

Please sign in to comment.