Swiftpack.co - m-housh/DynamoCoder as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by m-housh.
m-housh/DynamoCoder 0.1.3
A custom Encoder / Decoder for DynamoDB attributes.
⭐️ 0
🕓 4 years ago
.package(url: "https://github.com/m-housh/DynamoCoder.git", from: "0.1.3")

DynamoCoder

MacOS Linux codecov

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.

Usage


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")

*/

GitHub

link
Stars: 0
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics