A custom encoder / decoder for DynamoDB
attribute types.
This will allow you to convert Encodable
types to appropriate values appropriate for passing data into DynamoDB
. Most encodables
will get converted to [String: DynamoDB.AttributeValue]
which is typealiased to DynamoAttributeDict
. The encoder can
also encode lists of encodables to [[String: DynamoDB.AttributeValue]]
which is typealiased to DynamoEncodedArray
. Or encode a single value as DynamoDB.AttributeValue
.
import DynamoCoder
struct Person: Codable, Equatable {
struct Name: Codable, Equatable {
let first: String
let last: String
}
var name: Name
var age: Int
var height: Double
}
let andrew = Person(name: Name(first: "Andrew", last: "Jackson"), age: 30, height: 72.5)
// Encode as a `DynamoAttributeDict`.
let encoded = try DynamoEncoder().encode(andrew)
/* encoded to.
[
"name": DynamoDB.AttributeValue(m: ["first": DynamoDB.AttributeValue(s: "Andrew"),
"last": DynamoDB.AttributeValue(s: "Jackson")]),
"age": DynamoDB.AttributeValue(n: "30"),
"height": DynamoDB.AttributeValue(n: "72.5")
]
*/
let decoded = try DynamoDecoder().decode(Person.self, from: encoded)
assert(decoded == andrew) // true
The custom encoder also has the capability to convert an encodable type to a single DynamoDB.AttributeValue
.
let age: Int = 30
let attribute = DynamoEncoder().convert(age)
/* encoded to.
DynamoDB.AttributeValue(n: "30")
*/
link |
Stars: 0 |
Last commit: 4 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics