-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowWithItemVersion.swift
More file actions
77 lines (64 loc) · 2.58 KB
/
RowWithItemVersion.swift
File metadata and controls
77 lines (64 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//===----------------------------------------------------------------------===//
//
// This source file is part of the DynamoDBTables open source project
//
// This file is forked from
// https://github.com/amzn/smoke-dynamodb/tree/smoke-dynamodb-3.x/Sources/SmokeDynamoDB/RowWithItemVersion.swift
// Copyright 2018-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Licensed under Apache License v2.0
//
// Changes specified by
// https://github.com/swift-server-community/dynamo-db-tables/compare/9ab0e7a..main
// Copyright (c) 2024 the DynamoDBTables authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of DynamoDBTables authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// RowWithItemVersion.swift
// DynamoDBTables
//
import Foundation
public struct RowWithItemVersion<RowType: Sendable & Codable>: Sendable, Codable, CustomRowTypeIdentifier {
public static var rowTypeIdentifier: String? {
let rowTypeIdentity = getTypeRowIdentifier(type: RowType.self)
return "\(rowTypeIdentity)WithItemVersion"
}
enum CodingKeys: String, CodingKey {
case itemVersion = "ItemVersion"
}
public let itemVersion: Int
public let rowValue: RowType
public static func newItem(withVersion itemVersion: Int = 1,
withValue rowValue: RowType) -> RowWithItemVersion<RowType>
{
RowWithItemVersion<RowType>(itemVersion: itemVersion,
rowValue: rowValue)
}
public func createUpdatedItem(withVersion itemVersion: Int? = nil,
withValue newRowValue: RowType) -> RowWithItemVersion<RowType>
{
RowWithItemVersion<RowType>(itemVersion: itemVersion != nil ? itemVersion! : self.itemVersion + 1,
rowValue: newRowValue)
}
init(itemVersion: Int,
rowValue: RowType)
{
self.itemVersion = itemVersion
self.rowValue = rowValue
}
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.itemVersion = try values.decode(Int.self, forKey: .itemVersion)
self.rowValue = try RowType(from: decoder)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.itemVersion, forKey: .itemVersion)
try self.rowValue.encode(to: encoder)
}
}