Skip to content

Commit

Permalink
feat(codegen): use newtype instead of data for single field records
Browse files Browse the repository at this point in the history
  • Loading branch information
vst committed Jun 1, 2024
1 parent 05c2cea commit 738fe11
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Postmap/Gencode/Haskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ mkRecordDataType config@Config {..} record@Record {..} = do
title = fromMaybe (unRecordName recordName) recordTitle
table = [i|"#{unTableSchemaName recordTableSchema}"."#{unTableName recordTableName}"|] :: T.Text
fields = T.intercalate "\n , " $ fmap snd iFlds
hasMultipleFields = length iFlds > 1
modules'' = mapMaybe (filterMaybe (not . T.null) . T.dropEnd 1 . T.dropWhileEnd (/= '.') . fst) iFlds
modules' = ["Data.Maybe" | not (all fieldNotNullable recordFields)] <> modules''
modules =
Expand All @@ -199,7 +200,6 @@ mkRecordDataType config@Config {..} record@Record {..} = do
{-\# LANGUAGE TypeOperators \#-}
{-\# OPTIONS_GHC -Wno-unrecognised-pragmas \#-}
{-\# HLINT ignore "Avoid restricted alias" \#-}
{-\# HLINT ignore "Use newtype instead of data" \#-}

-- | This module provides for /#{title}/ record definition, its database mapping and other related definitions.
module #{configModuleName}.Records.#{cnsName} where
Expand All @@ -213,7 +213,7 @@ import Prelude (($))


-- | Data type representing the /#{title}/ record backed by database table @#{table}@.#{maybe "" (\x -> "\n--\n" <> x <> "\n") docs}
data #{hkdName} f = #{cnsName}
#{if hasMultipleFields then "data" else "newtype" :: T.Text} #{hkdName} f = #{cnsName}
{ #{fields}
}
deriving stock (GHC.Generics.Generic)
Expand Down Expand Up @@ -276,7 +276,11 @@ mkRecordDataTypeField _config record@Record {..} field@Field {..} = do
fType'' = if fieldNotNullable then fType' else [i|(Data.Maybe.Maybe #{fType'})|]
fType = if isArr then [i|[#{fType''}]|] else fType''
fDesc = fromMaybe "" docs
in pure (fType', [i|#{fName} :: !(Rel8.Column f #{fType})#{fDesc}|])
ffType =
if length recordFields > 1
then [i|!(Rel8.Column f #{fType})|]
else [i|Rel8.Column f #{fType}|] :: T.Text
in pure (fType', [i|#{fName} :: #{ffType}#{fDesc}|])


mkRecordJsonField :: Record -> Field -> T.Text
Expand Down

0 comments on commit 738fe11

Please sign in to comment.