Skip to content
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
8 changes: 6 additions & 2 deletions Sources/DynamoQueries/Builders/UpdateBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
@resultBuilder
public struct UpdateBuilder {
public static func buildBlock(_ components: UpdateAction...) -> [UpdateAction] {
Array(components)
public static func buildExpression(_ expression: UpdateAction) -> [UpdateAction] {
[expression]
}

public static func buildBlock(_ components: [UpdateAction]...) -> [UpdateAction] {
components.flatMap { $0 }
}

public static func buildOptional(_ component: [UpdateAction]?) -> [UpdateAction] {
Expand Down
69 changes: 69 additions & 0 deletions Tests/DynamoQueriesTests/QueryEquivalenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,75 @@ struct UpdateItemEquivalenceTests {
)
expectEquivalent(dslSoto, reference)
}

@Test("UpdateItem: an if includes its actions only when it holds", arguments: [true, false])
func updateItemIf(isFavorite: Bool) async throws {
let client = RecordingDynamoClient()
try await TrailRoute.update(partitionKey: "route-1") { column in
column.nameLower.set(to: "mist falls")
if isFavorite {
column.isFavorite.set(to: true)
}
column.createdAt.set(to: 1735776000.0)
}
.execute(using: client)
let captured = try #require(await client.lastUpdateInput(for: TrailRoute.self))
let dslSoto = captured.toSotoUpdateItemInput()

let reference = isFavorite
? DynamoDB.UpdateItemInput(
expressionAttributeValues: [
":nameLower": .s("mist falls"),
":isFavorite": .bool(true),
":createdAt": .n("1735776000.0"),
],
key: ["routeId": .s("route-1")],
tableName: "TrailRoutes",
updateExpression:
"SET nameLower = :nameLower, isFavorite = :isFavorite, createdAt = :createdAt"
)
: DynamoDB.UpdateItemInput(
expressionAttributeValues: [
":nameLower": .s("mist falls"),
":createdAt": .n("1735776000.0"),
],
key: ["routeId": .s("route-1")],
tableName: "TrailRoutes",
updateExpression: "SET nameLower = :nameLower, createdAt = :createdAt"
)
expectEquivalent(dslSoto, reference)
}

@Test("UpdateItem: if/else takes one branch's actions", arguments: [true, false])
func updateItemIfElse(isPrivate: Bool) async throws {
let client = RecordingDynamoClient()
try await TrailRoute.update(partitionKey: "route-1") { column in
if isPrivate {
column.isPrivate.set(to: true)
} else {
column.isPrivate.remove()
column.isFavorite.set(to: false)
}
}
.execute(using: client)
let captured = try #require(await client.lastUpdateInput(for: TrailRoute.self))
let dslSoto = captured.toSotoUpdateItemInput()

let reference = isPrivate
? DynamoDB.UpdateItemInput(
expressionAttributeValues: [":isPrivate": .bool(true)],
key: ["routeId": .s("route-1")],
tableName: "TrailRoutes",
updateExpression: "SET isPrivate = :isPrivate"
)
: DynamoDB.UpdateItemInput(
expressionAttributeValues: [":isFavorite": .bool(false)],
key: ["routeId": .s("route-1")],
tableName: "TrailRoutes",
updateExpression: "SET isFavorite = :isFavorite REMOVE isPrivate"
)
expectEquivalent(dslSoto, reference)
}
}

// MARK: - DeleteItem
Expand Down
Loading