Skip to content

Commit

Permalink
Merge pull request #83 from ccap/next
Browse files Browse the repository at this point in the history
Code gen overhaul
  • Loading branch information
pbrant authored Jan 20, 2022
2 parents 30e8865 + bc9057a commit da60cd9
Show file tree
Hide file tree
Showing 54 changed files with 4,187 additions and 1,781 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
/test/generated/*
package-lock.json
yarn.lock
.metals
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trailingComma: es5
bracketSpacing: true
tabWidth: 2
singleQuote: true
insertPragma: true
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"devDependencies": {
"spago": "^0.13.0",
"dotenv-cli": "^3.0.0",
"purescript": "^0.13.5",
"purescript": "^0.13.8",
"purescript-language-server": "^0.12.7",
"purescript-psa": "^0.7.3",
"purty": "^4.5.2",
"purty": "6.2.0",
"rimraf": "^2.6.2"
}
}
92 changes: 45 additions & 47 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@


let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.13.5-20191215/packages.dhall sha256:fdc5d54cd54213000100fbc13c90dce01668a73fe528d8662430558df3411bee
https://github.com/purescript/package-sets/releases/download/psc-0.13.8/packages.dhall sha256:0e95ec11604dc8afc1b129c4d405dcc17290ce56d7d0665a0ff15617e32bbf03

let overrides = {=}

let additions =
{ boxes =
{ dependencies =
[ "profunctor"
, "prelude"
, "stringutils"
, "generics-rep"
, "strings"
]
, repo = "https://github.com/cdepillabout/purescript-boxes.git"
, version = "v2.0.1"
}
, postgresql-client =
{ dependencies =
[ "aff"
, "argonaut"
, "arrays"
, "assert"
, "bifunctors"
, "bytestrings"
, "console"
, "datetime"
, "decimals"
, "effect"
, "either"
, "exceptions"
, "foldable-traversable"
, "foreign"
, "foreign-generic"
, "foreign-object"
, "js-date"
, "lists"
, "maybe"
, "newtype"
, "nullable"
, "prelude"
, "psci-support"
, "test-unit"
, "transformers"
, "tuples"
]
, repo = "https://github.com/pbrant/purescript-postgresql-client.git"
, version = "7188cd732eb7981910915fb5e8ab8b158abda95b" -- #update-dependencies
}
}
{ boxes =
{ dependencies =
[ "profunctor"
, "prelude"
, "stringutils"
, "generics-rep"
, "strings"
]
, repo = "https://github.com/cdepillabout/purescript-boxes.git"
, version = "v2.0.1"
}
, postgresql-client =
{ dependencies =
[ "aff"
, "argonaut"
, "arrays"
, "assert"
, "bifunctors"
, "bytestrings"
, "console"
, "datetime"
, "decimals"
, "effect"
, "either"
, "exceptions"
, "foldable-traversable"
, "foreign"
, "foreign-generic"
, "foreign-object"
, "js-date"
, "lists"
, "maybe"
, "newtype"
, "nullable"
, "prelude"
, "psci-support"
, "test-unit"
, "transformers"
, "tuples"
]
, repo = "https://github.com/pbrant/purescript-postgresql-client.git"
, version = "7188cd732eb7981910915fb5e8ab8b158abda95b"
}
}

in upstream // overrides // additions
6 changes: 6 additions & 0 deletions samples/errors/Cycle1.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scala: test.Cycle1
purs: Test.Cycle1

import Cycle2

type X: Int
6 changes: 6 additions & 0 deletions samples/errors/Cycle2.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scala: test.Cycle2
purs: Test.Cycle2

import Cycle3

type X: Int
6 changes: 6 additions & 0 deletions samples/errors/Cycle3.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scala: test.Cycle3
purs: Test.Cycle3

import Cycle1

type X: Int
4 changes: 4 additions & 0 deletions samples/errors/Imported.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
scala: test.Imported
purs: Test.Imported

type X: Int
21 changes: 21 additions & 0 deletions samples/errors/TypeErrors.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
scala: test.TypeErrors
purs: Test.TypeErrors

import Imported

type Dup1: Int
type Dup1: String

type Rec: {
dup1: Int
dup1: String
}

type Sum: [
| Dup1
| Dup1
]

type X: Y
type Z: Imported.Q
type Q: Bar.M
3 changes: 2 additions & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ You can edit this file as you like.
{ name = "ccap-codegen"
, dependencies =
[ "argonaut-codecs"
, "console"
, "boxes"
, "console"
, "debug"
, "effect"
, "filterable"
Expand All @@ -22,6 +22,7 @@ You can edit this file as you like.
, "strings"
, "transformers"
, "typelevel-prelude"
, "validation"
, "yargs"
]
, packages = ./packages.dhall
Expand Down
20 changes: 10 additions & 10 deletions src/Ccap/Codegen/Annotations.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ module Ccap.Codegen.Annotations
) where

import Prelude
import Ccap.Codegen.Types (Annotation(..), Annotations, AnnotationParam(..))
import Ccap.Codegen.Cst as Cst
import Control.Alt ((<|>))
import Data.Array as Array
import Data.Maybe (Maybe)

fieldWithOptParamValue :: String -> String -> Annotations -> Maybe (Maybe String)
fieldWithOptParamValue :: String -> String -> Array Cst.Annotation -> Maybe (Maybe String)
fieldWithOptParamValue annotKey paramKey annots = do
Annotation _ _ params <- Array.find (\(Annotation n _ _) -> n == annotKey) annots
AnnotationParam _ _ v <- Array.find (\(AnnotationParam n _ _) -> n == paramKey) params
Cst.Annotation _ _ params <- Array.find (\(Cst.Annotation n _ _) -> n == annotKey) annots
Cst.AnnotationParam _ _ v <- Array.find (\(Cst.AnnotationParam n _ _) -> n == paramKey) params
pure v

field :: String -> String -> Annotations -> Maybe String
field :: String -> String -> Array Cst.Annotation -> Maybe String
field annotKey paramKey = join <<< fieldWithOptParamValue annotKey paramKey

getWrapOpts :: String -> Annotations -> Maybe { typ :: String, decode :: String, encode :: String }
getWrapOpts :: String -> Array Cst.Annotation -> Maybe { typ :: String, decode :: String, encode :: String }
getWrapOpts lang an =
let
f n = field lang n an
Expand All @@ -33,14 +33,14 @@ getWrapOpts lang an =
encode <- f "encode" <|> pure ""
pure { typ, decode, encode }

getMaxLength :: Annotations -> Maybe String
getMaxLength :: Array Cst.Annotation -> Maybe String
getMaxLength = field "validations" "maxLength"

getMinLength :: Annotations -> Maybe String
getMinLength :: Array Cst.Annotation -> Maybe String
getMinLength = field "validations" "minLength"

getMaxSize :: Annotations -> Maybe String
getMaxSize :: Array Cst.Annotation -> Maybe String
getMaxSize = field "validations" "maxSize"

getIsPositive :: Annotations -> Maybe Unit
getIsPositive :: Array Cst.Annotation -> Maybe Unit
getIsPositive annots = fieldWithOptParamValue "validations" "positive" annots $> unit
150 changes: 150 additions & 0 deletions src/Ccap/Codegen/Ast.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
module Ccap.Codegen.Ast
( Constructor(..)
, Module(..)
, RecordProp(..)
, ScalaDecoderType(..)
, TRef
, TopType(..)
, Type(..)
, TypeDecl(..)
, TypeOrParam(..)
, isRecord
, noArgConstructorNames
, typeDeclName
, typeDeclTopType
) where

import Prelude
import Ccap.Codegen.Cst as Cst
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Maybe (Maybe(..))
import Data.Traversable (traverse)
import Data.Tuple (Tuple)

newtype Module
= Module
{ types :: NonEmptyArray TypeDecl
, imports :: Array Module
, exports :: Cst.Exports
, name :: Cst.ModuleName
}

newtype TypeDecl
= TypeDecl
{ name :: String
, topType :: TopType
, annots :: Array Cst.Annotation
, isPrimary :: Boolean
, params :: Array Cst.TypeParam
, scalaDecoderType :: Maybe ScalaDecoderType
}

data TopType
= Type Type
| Wrap Type
| Record (NonEmptyArray RecordProp)
| Sum (NonEmptyArray Constructor)

data Type
= Primitive Cst.Primitive
| Ref TRef
| Array TypeOrParam
| Option TypeOrParam

data TypeOrParam
= TType Type
| TParam Cst.TypeParam

type TRef
= { decl :: Maybe (Tuple Module TypeDecl)
, typ :: String
, params :: Array TypeOrParam
, isPrimaryRef :: Boolean
}

type RecordProp
= { name :: String
, typ :: TypeOrParam
, annots :: Array Cst.Annotation
}

data Constructor
= NoArg Cst.ConstructorName
| WithArgs Cst.ConstructorName (NonEmptyArray TypeOrParam)

isRecord :: TopType -> Boolean
isRecord = case _ of
Record _ -> true
_ -> false

-- | Get the type name of a type declaration.
typeDeclName :: TypeDecl -> String
typeDeclName (TypeDecl { name }) = name

-- | Get the top most type of a type declaration.
typeDeclTopType :: TypeDecl -> TopType
typeDeclTopType (TypeDecl { topType }) = topType

noArgConstructorNames :: NonEmptyArray Constructor -> Maybe (NonEmptyArray String)
noArgConstructorNames =
traverse
( case _ of
NoArg (Cst.ConstructorName n) -> Just n
WithArgs _ _ -> Nothing
)

data ScalaDecoderType
= Field
| Form

-- Instances here to avoid cluttering the above
derive instance eqScalaDecoderType :: Eq ScalaDecoderType

derive instance genericScalaDecoderType :: Generic ScalaDecoderType _

instance showScalaDecoderType :: Show ScalaDecoderType where
show t = genericShow t

derive instance eqModule :: Eq Module

derive instance genericModule :: Generic Module _

instance showModule :: Show Module where
show t = genericShow t

derive instance eqTypeOrParam :: Eq TypeOrParam

derive instance genericTypeOrParam :: Generic TypeOrParam _

instance showTypeOrParam :: Show TypeOrParam where
show t = genericShow t

derive instance eqConstructor :: Eq Constructor

derive instance genericConstructor :: Generic Constructor _

instance showConstructor :: Show Constructor where
show t = genericShow t

derive instance eqType :: Eq Type

derive instance genericType :: Generic Type _

instance showType :: Show Type where
show t = genericShow t

derive instance eqTopType :: Eq TopType

derive instance genericTopType :: Generic TopType _

instance showTopType :: Show TopType where
show = genericShow

derive instance eqTypeDecl :: Eq TypeDecl

derive instance genericTypeDecl :: Generic TypeDecl _

instance showTypeDecl :: Show TypeDecl where
show = genericShow
Loading

0 comments on commit da60cd9

Please sign in to comment.