Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use field label modifier on deprecated #90

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Moat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import Control.Monad.Except
#if MIN_VERSION_mtl(2,3,0)
import Control.Monad.Trans
#endif
import qualified Data.Bifunctor as Bifunctor
import Data.Bool (bool)
import qualified Data.Char as Char
import Data.Foldable (foldl', foldlM, foldr')
Expand Down Expand Up @@ -982,8 +983,8 @@ mkNewtype o@Options {..} typName doc instTys ts = \case
} -> do
fieldDocs <- lift $ mapM (getDocWith o) fieldNames
fields <- zipFields o fieldNames constructorFields fieldDocs
deprecatedFieldExps <- lift [e|deprecatedFields|]
matchProxy =<< lift (structExp typName doc instTys dataInterfaces dataProtocols dataAnnotations fields deprecatedFieldExps ts makeBase)
deprecatedFieldExp <- lift $ mkDeprecatedField o deprecatedFields
matchProxy =<< lift (structExp typName doc instTys dataInterfaces dataProtocols dataAnnotations fields deprecatedFieldExp ts makeBase)
ConstructorInfo
{ constructorFields = [field]
} -> do
Expand Down Expand Up @@ -1035,9 +1036,13 @@ mkProd o@Options {..} typName parentDoc instTys ts = \case
} -> do
fieldDocs <- lift $ mapM (getDocWith o) fieldNames
fields <- zipFields o fieldNames constructorFields fieldDocs
deprecatedFieldExp <- lift [e|deprecatedFields|]
deprecatedFieldExp <- lift $ mkDeprecatedField o deprecatedFields
matchProxy =<< lift (structExp typName parentDoc instTys dataInterfaces dataProtocols dataAnnotations fields deprecatedFieldExp ts makeBase)

mkDeprecatedField :: Options -> [(String, Maybe String)] -> Q Exp
mkDeprecatedField options deprecatedFields =
let modifiedFields = Bifunctor.first (modifyFieldName options) <$> deprecatedFields in [e|modifiedFields|]

-- | 'strictFields' are required to exist in the record and are always included.
-- 'omitFields' will remove any remaining fields if they are 'Discard'ed.
zipFields :: Options -> [Name] -> [Type] -> [Maybe String] -> MoatM [Exp]
Expand Down Expand Up @@ -1123,12 +1128,15 @@ getFreeTyVar = \case
SigT (VarT name) _kind -> Just name
_ -> Nothing

modifyFieldName :: Options -> String -> String
modifyFieldName options = onHeadWith (lowerFirstField options) . fieldLabelModifier options

-- make a struct field pretty
prettyField :: Options -> Name -> Type -> Maybe String -> Exp
prettyField Options {..} name ty doc =
prettyField options name ty doc =
RecConE
'Field
[ ('fieldName, stringE (onHeadWith lowerFirstField (fieldLabelModifier (nameStr name))))
[ ('fieldName, stringE (modifyFieldName options (nameStr name)))
, ('fieldType, toMoatTypeEPoly ty)
, ('fieldDoc, prettyDoc doc)
]
Expand Down
11 changes: 7 additions & 4 deletions test/DeprecatedFieldSpec.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
module DeprecatedFieldSpec where

import Common
import Data.List (stripPrefix)
import Data.Maybe
import Moat
import Test.Hspec
import Test.Hspec.Golden

data Data = Data
{ field0 :: Int
, field1 :: Maybe Int
{ testField0 :: Int
, testField1 :: Maybe Int
}

mobileGenWith
( defaultOptions
{ fieldsRequiredByClients = ["field0", "field1"]
{ fieldsRequiredByClients = ["testField0", "testField1"]
, omitFields = const Discard
, deprecatedFields = [("field1", Just "Deprecated since build 500")]
, deprecatedFields = [("testField1", Just "Deprecated since build 500")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't obvious, but do we require testField1 to exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If required fields are included we check testField1 is one of the required fields if deprecated. We don't check that it isn't a part of omitFields though it might make sense to add that (I can do it as part of another PR)

, fieldLabelModifier = \s -> fromMaybe s (stripPrefix "test" s)
}
)
''Data
Expand Down
Loading