I recently converted most of the server side code to use lenses and had bunch of naming conflicts all over, mostly relating to newtypes representing things like name of ship or star. I devised a naming scheme, that I’m going to follow from now on.
For example, if I have Star defined in models
as following:
Star json
name StarName
starSystemId StarSystemId
spectralType SpectralType
luminosityClass LuminosityClass
deriving Show Read Eq
And I have following piece of code to read it and generate database schema and entities:
share [ mkPersist sqlSettings { mpsGenerateLenses = True }
, mkMigrate "migrateAll"
]
$(persistFileWith lowerCaseSettings "config/models")
Then I define StarName
newtype as following:
newtype StarName = MkStarName { _unStarName :: Text }
deriving (Show, Read, Eq)
makeLenses ``StarName
makeWrapped ``StarName
This generates a lens: unStarName
.
I ended up in this scheme, because the first part will generate me a record that is roughly as:
data Star = Star
{ _starName :: StarName
, _starStarSystemId :: Key StarSystem
, _starSpectralType :: SpectralType
, _starLuminosityClass LuminosityClass
}
deriving (Show, Read, Eq)
It will also generate value constructors used to modify and filter data in database: StarName
, StarStarSystemId
, StarSpectralType
and StarLuminosityClass
. These value constructors are the reason why I had to name value constructor of StarName
to MkStarName
.
mpsGenerateLenses = True
setting will generate lenses for Star
: starName
, starStarSystemId
, starSpectralType
, starLuminosityClass
.
This way there’s least amount of naming conflicts and I don’t have to keep using qualified imports or aliases.