Skip to content

Commit

Permalink
Merge pull request #1020 from Tristano8/support-transact-write-items-sum
Browse files Browse the repository at this point in the history
amazonka-dynamodb: TransactWriteItem sum type
  • Loading branch information
endgame authored Jan 15, 2025
2 parents 821bffe + 4c16a0b commit afdbce3
Show file tree
Hide file tree
Showing 154 changed files with 406 additions and 218 deletions.
2 changes: 2 additions & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
within:
- Amazonka.Auth.SSO
- Amazonka.Data.Body
- Amazonka.EC2.Internal
- Amazonka.EC2.Metadata
- Amazonka.Env
- Amazonka.S3.Internal
- Amazonka.Types
- Amazonka.Waiter
15 changes: 10 additions & 5 deletions configs/services/dynamodb.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
"extraDependencies": [
"aeson",
"amazonka-dynamodb-attributevalue",
"containers",
"hashable >=1.3.4.0 && <1.5",
"unordered-containers",
"vector"
"containers"
],
"typeModules": [
"Amazonka.DynamoDB.Types.AttributeValue",
"Amazonka.DynamoDB.Types.TransactWriteItem",
"Amazonka.DynamoDB.Types.WriteRequest"
],
"typeOverrides": {
Expand Down Expand Up @@ -40,11 +38,18 @@
"QueryOutput": {
"requiredFields": ["Items"]
},
"TransactWriteItem": {
"replacedBy": {
"name": "TransactWriteItem",
"underive": []
}
},
"WriteRequest": {
"replacedBy": {
"name": "WriteRequest",
"underive": []
}
}
}
},
"extraBootShapes": ["ConditionCheck", "Delete", "Put", "Update"]
}
9 changes: 1 addition & 8 deletions configs/services/dynamodbstreams.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
{
"libraryName": "amazonka-dynamodb-streams",
"extraDependencies": [
"aeson",
"amazonka-dynamodb-attributevalue",
"containers",
"hashable >=1.3.4.0 && <1.5",
"unordered-containers",
"vector"
],
"extraDependencies": ["amazonka-dynamodb-attributevalue"],
"typeModules": ["Amazonka.DynamoDBStreams.Types.AttributeValue"],
"typeOverrides": {
"AttributeValue": {
Expand Down
18 changes: 9 additions & 9 deletions gen/src/Gen/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ rewrite _version' _config' s' = do
Lit {} -> []

-- A 'Lens.Fold' over any type names that 't' will have to import.
importedTypes :: TypeOf t => Lens.Fold t Text
importedTypes :: (TypeOf t) => Lens.Fold t Text
importedTypes = Lens.to (typeNames . typeOf) . traverse
where
typeNames = \case
Expand All @@ -61,12 +61,12 @@ rewrite _version' _config' s' = do
TList1 t -> typeNames t
TMap k v -> typeNames k ++ typeNames v

-- Compute cuts that we will need to turn into @{-# SOURCE #-}@ imports.
-- Ignore cuts from a type to itself; they don't cause circular imports.
_cuts' = Set.filter (uncurry (/=)) $ breakLoops edges nodes
-- Compute imports for which we need to use @{-# SOURCE #-}@ pragmas.
-- Ignore imports from a type to itself; they don't cause circular imports.
_sourceImports' = Set.filter (uncurry (/=)) $ breakLoops edges nodes

_instance' <- serviceData (_service' ^. metadata) (_service' ^. retry)
pure $ Library {_version', _config', _service', _cuts', _instance'}
pure $ Library {_version', _config', _service', _sourceImports', _instance'}

deprecate :: Service f a b c -> Service f a b c
deprecate = operations %~ HashMap.filter (not . Lens.view opDeprecated)
Expand Down Expand Up @@ -137,7 +137,7 @@ type MemoR = StateT (HashMap Id Relation, HashSet (Id, Direction, Id)) (Either S
-- /Note:/ This currently doesn't operate over the free AST, since it's also
-- used by 'setDefaults'.
relations ::
Show a =>
(Show a) =>
HashMap Id (Operation Maybe (RefF b) c) ->
HashMap Id (ShapeF a) ->
Either String (HashMap Id Relation)
Expand Down Expand Up @@ -186,7 +186,7 @@ relations os ss = fst <$> State.execStateT (traverse go os) (mempty, mempty)

-- FIXME: Necessary to update the Relation?
solve ::
Traversable t =>
(Traversable t) =>
Config ->
t (Shape Prefixed) ->
t (Shape Solved)
Expand Down Expand Up @@ -231,7 +231,7 @@ separate os = State.runStateT (traverse go os)
_opOutput = Identity (o ^. opOutput . _Identity & refAnn .~ y)
}

remove :: HasRelation a => Direction -> Id -> MemoS a a
remove :: (HasRelation a) => Direction -> Id -> MemoS a a
remove d n = do
s <- State.get

Expand All @@ -251,7 +251,7 @@ separate os = State.runStateT (traverse go os)

breakLoops ::
forall node.
Ord node =>
(Ord node) =>
-- | Edge relation
(node -> [node]) ->
-- | Set of nodes to explore from
Expand Down
61 changes: 31 additions & 30 deletions gen/src/Gen/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import Gen.Types
operationImports :: Library -> Operation Identity SData a -> [NS]
operationImports l _o =
Set.toAscList . Set.fromList $
"qualified Amazonka.Request as Request" :
"qualified Amazonka.Response as Response" :
"qualified Amazonka.Core as Core" :
"qualified Amazonka.Core.Lens.Internal as Lens" :
"qualified Amazonka.Data as Data" :
"qualified Amazonka.Prelude as Prelude" :
l ^. typesNS :
l ^. operationModules
"qualified Amazonka.Request as Request"
: "qualified Amazonka.Response as Response"
: "qualified Amazonka.Core as Core"
: "qualified Amazonka.Core.Lens.Internal as Lens"
: "qualified Amazonka.Data as Data"
: "qualified Amazonka.Prelude as Prelude"
: l ^. typesNS
: l ^. operationModules

typeImports :: Library -> [NS]
typeImports l =
List.sort $
"qualified Amazonka.Core as Core" :
"qualified Amazonka.Core.Lens.Internal as Lens" :
"qualified Amazonka.Prelude as Prelude" :
signatureImport (l ^. signatureVersion) :
l ^. typeModules
"qualified Amazonka.Core as Core"
: "qualified Amazonka.Core.Lens.Internal as Lens"
: "qualified Amazonka.Prelude as Prelude"
: signatureImport (l ^. signatureVersion)
: l ^. typeModules

lensImports :: Library -> [NS]
lensImports l =
Expand All @@ -36,26 +36,27 @@ lensImports l =
sumImports :: Library -> [NS]
sumImports l =
List.sort $
"qualified Amazonka.Core as Core" :
"qualified Amazonka.Data as Data" :
"qualified Amazonka.Prelude as Prelude" :
l ^. typeModules
"qualified Amazonka.Core as Core"
: "qualified Amazonka.Data as Data"
: "qualified Amazonka.Prelude as Prelude"
: l ^. typeModules

productImports :: Library -> Prod -> [NS]
productImports l p =
List.sort $
"qualified Amazonka.Core as Core" :
"qualified Amazonka.Core.Lens.Internal as Lens" :
"qualified Amazonka.Data as Data" :
"qualified Amazonka.Prelude as Prelude" :
l ^. typeModules ++ productDependencies l p
"qualified Amazonka.Core as Core"
: "qualified Amazonka.Core.Lens.Internal as Lens"
: "qualified Amazonka.Data as Data"
: "qualified Amazonka.Prelude as Prelude"
: l ^. typeModules
++ productDependencies l p

productDependencies :: Library -> Prod -> [NS]
productDependencies l p =
Set.toList (Set.map buildImport moduleDependencies)
where
buildImport t
| (_prodName p, t) `elem` (l ^. cuts') = addSource t'
| (_prodName p, t) `elem` (l ^. sourceImports') = addSource t'
| otherwise = t'
where
t' = l ^. typesNS <> mkNS t
Expand All @@ -73,13 +74,13 @@ productDependencies l p =
waiterImports :: Library -> [NS]
waiterImports l =
List.sort $
"qualified Amazonka.Core as Core" :
"qualified Amazonka.Core.Lens.Internal as Lens" :
"qualified Amazonka.Data as Data" :
"qualified Amazonka.Prelude as Prelude" :
l ^. typesNS :
l ^. lensNS :
map (operationNS ns . _waitOpName) (l ^.. waiters . Lens.each)
"qualified Amazonka.Core as Core"
: "qualified Amazonka.Core.Lens.Internal as Lens"
: "qualified Amazonka.Data as Data"
: "qualified Amazonka.Prelude as Prelude"
: l ^. typesNS
: l ^. lensNS
: map (operationNS ns . _waitOpName) (l ^.. waiters . Lens.each)
where
ns = l ^. libraryNS

Expand Down
26 changes: 14 additions & 12 deletions gen/src/Gen/Tree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Text.EDE (Template)
import qualified Text.EDE as EDE

fold ::
MonadFail m =>
(MonadFail m) =>
-- | Directories
(FilePath -> m ()) ->
-- | Files
Expand Down Expand Up @@ -122,18 +122,20 @@ populate d Templates {..} l = (d :/) . Dir lib <$> layout
where
template = case s of
Prod _ p _
| _prodName p `elem` Set.map snd (l ^. cuts') ->
Just bootProductTemplate
| _prodName p `elem` Set.map snd (l ^. sourceImports') ->
Just bootProductTemplate
| _prodName p `elem` (l ^. extraBootShapes) ->
Just bootProductTemplate
_ -> Nothing

fixture :: Operation Identity SData a -> [DirTree (Either String Touch)]
fixture o =
[ touch (n <> "Response.proto") blankTemplate mempty,
touch (n <> ".yaml") fixtureRequestTemplate $
fromPairs
[ "method" .= (o ^. opHttp . method),
"endpointPrefix" .= (l ^. endpointPrefix)
]
fromPairs
[ "method" .= (o ^. opHttp . method),
"endpointPrefix" .= (l ^. endpointPrefix)
]
]
where
n = typeId (_opName o)
Expand Down Expand Up @@ -213,7 +215,7 @@ data Module a = Module
env :: Either String a
}

module' :: ToJSON a => Module a -> DirTree (Either String Rendered)
module' :: (ToJSON a) => Module a -> DirTree (Either String Rendered)
module' Module {..} =
render (FilePath.takeFileName (nsToPath name)) template $ do
x <- env >>= JSON.objectErr (show name)
Expand All @@ -227,7 +229,7 @@ module' Module {..} =
"templateName" .= templateName name
]

bootModule' :: ToJSON a => Module a -> DirTree (Either String Rendered)
bootModule' :: (ToJSON a) => Module a -> DirTree (Either String Rendered)
bootModule' Module {..} =
render (FilePath.takeFileName (nsToPath name) <> "-boot") template $ do
x <- env >>= JSON.objectErr (show name)
Expand All @@ -242,7 +244,7 @@ bootModule' Module {..} =
]

render ::
ToJSON a =>
(ToJSON a) =>
FilePath ->
Template ->
Either String a ->
Expand All @@ -253,10 +255,10 @@ render p tmpl a =
touch :: Text -> Template -> Aeson.Object -> DirTree (Either String Touch)
touch f tmpl env =
File (Text.unpack f) $
bimap id Left (EDE.eitherRender tmpl (Aeson.KeyMap.toHashMapText env))
bimap id Left (EDE.eitherRender tmpl (Aeson.KeyMap.toHashMapText env))

write :: DirTree (Either e b) -> DirTree (Either e (Either a b))
write = fmap (second Right)

fromPairs :: [Aeson.Types.Pair] -> Aeson.Types.Object
fromPairs :: [Aeson.Types.Pair] -> Aeson.Types.Object
fromPairs = Aeson.KeyMap.fromHashMapText . EDE.fromPairs
13 changes: 10 additions & 3 deletions gen/src/Gen/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ data Config = Config
_typeOverrides :: HashMap Id Override,
_ignoredWaiters :: HashSet Id,
_ignoredPaginators :: HashSet Id,
_extraDependencies :: [Text]
_extraDependencies :: [Text],
-- | Additional shape names for which we need to generate
-- @.hs-boot@ files. Handy when defining @typeModules@ that need
-- to import generated shapes (e.g., DynamoDB's
-- @TransactWriteItem@).
_extraBootShapes :: [Text]
}

$(Lens.makeClassy ''Config)
Expand All @@ -109,15 +114,16 @@ instance FromJSON Config where
<*> o .:? "ignoredWaiters" .!= mempty
<*> o .:? "ignoredPaginators" .!= mempty
<*> o .:? "extraDependencies" .!= mempty
<*> o .:? "extraBootShapes" .!= mempty

data Library = Library
{ _version' :: Version,
_config' :: Config,
_service' :: Service Identity SData SData WData,
-- | @(x, y)@ in cuts' means that:
-- | @(x, y)@ in sourceImports' means that:
-- * The module for @x@ should @import {-# SOURCE #-} y@, and
-- * We should generate a @.hs-boot@ for @y@.
_cuts' :: Set (Text, Text),
_sourceImports' :: Set (Text, Text),
_instance' :: Fun
}

Expand Down Expand Up @@ -154,6 +160,7 @@ instance ToJSON Library where
"exposedModules" .= List.sort (l ^. exposedModules),
"otherModules" .= List.sort (l ^. otherModules),
"extraDependencies" .= List.sort (l ^. extraDependencies),
"extraBootShapes" .= List.sort (l ^. extraBootShapes),
"operations"
.= List.sortOn _opName (l ^.. operations . Lens.each),
"shapes" .= List.sort (l ^.. shapes . Lens.each),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ library
, aeson ^>=1.5.0.0 || ^>=2.0 || ^>=2.1 || ^>=2.2
, amazonka-core ^>=2.0
, containers
, hashable >=1.3.4.0 && <1.5
, hashable >=1.3.4.0 && <1.6
, vector
2 changes: 2 additions & 0 deletions lib/amazonka/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Changed

- `amazonka-dynamodb`: `TransactWriteItem` is now a sum type (thanks @Tristano8).
[\#1020](https://github.com/brendanhay/amazonka/pull/1020)
- `amazonka-core`: Add regions: `Malaysia` (`ap-southeast-5`), `Thailand` (`ap-southeast-7`), `Calgary` (`ca-west-1`), and `TelAviv` (`il-central-1`)
[\#1017](https://github.com/brendanhay/amazonka/pull/1017)
- Depend on `microlens`, `microlens-pro` and `microlens-contra` instead of the full `lens` package (thanks @mankykitty).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,9 @@ library
Amazonka.DynamoDBStreams.Waiters

build-depends:
, aeson
, amazonka-core >=2.0 && <2.1
, amazonka-core >=2.0 && <2.1
, amazonka-dynamodb-attributevalue
, base >=4.12 && <5
, containers
, hashable >=1.3.4.0 && <1.5
, unordered-containers
, vector
, base >=4.12 && <5

test-suite amazonka-dynamodb-streams-test
type: exitcode-stdio-1.0
Expand Down
7 changes: 2 additions & 5 deletions lib/services/amazonka-dynamodb/amazonka-dynamodb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,10 @@ library

build-depends:
, aeson
, amazonka-core >=2.0 && <2.1
, amazonka-core >=2.0 && <2.1
, amazonka-dynamodb-attributevalue
, base >=4.12 && <5
, base >=4.12 && <5
, containers
, hashable >=1.3.4.0 && <1.5
, unordered-containers
, vector

test-suite amazonka-dynamodb-test
type: exitcode-stdio-1.0
Expand Down
Loading

0 comments on commit afdbce3

Please sign in to comment.