Skip to content

Commit 4b10997

Browse files
committed
OptionalStringCoding
1 parent 3a17b74 commit 4b10997

File tree

4 files changed

+114
-17
lines changed

4 files changed

+114
-17
lines changed

Sources/AWSLambdaEvents/SQS.swift

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public enum SQS {
3232

3333
public let messageId: String
3434
public let receiptHandle: String
35-
public let body: String?
35+
@OptionalStringCoding
36+
public var body: String?
3637
public let md5OfBody: String
3738
public let md5OfMessageAttributes: String?
3839
public let attributes: [String: String]
@@ -56,22 +57,6 @@ extension SQS.Message: Decodable {
5657
case eventSource
5758
case awsRegion
5859
}
59-
60-
public init(from decoder: Decoder) throws {
61-
let container = try decoder.container(keyedBy: CodingKeys.self)
62-
self.messageId = try container.decode(String.self, forKey: .messageId)
63-
self.receiptHandle = try container.decode(String.self, forKey: .receiptHandle)
64-
self.md5OfBody = try container.decode(String.self, forKey: .md5OfBody)
65-
self.md5OfMessageAttributes = try container.decodeIfPresent(String.self, forKey: .md5OfMessageAttributes)
66-
self.attributes = try container.decode([String: String].self, forKey: .attributes)
67-
self.messageAttributes = try container.decode([String: Attribute].self, forKey: .messageAttributes)
68-
self.eventSourceArn = try container.decode(String.self, forKey: .eventSourceArn)
69-
self.eventSource = try container.decode(String.self, forKey: .eventSource)
70-
self.awsRegion = try container.decode(AWSRegion.self, forKey: .awsRegion)
71-
72-
let body = try container.decode(String?.self, forKey: .body)
73-
self.body = body != "" ? body : nil
74-
}
7560
}
7661

7762
extension SQS.Message.Attribute: Equatable {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
@propertyWrapper
16+
public struct OptionalStringCoding: Decodable {
17+
public let wrappedValue: String?
18+
19+
public init(wrappedValue: String?) {
20+
self.wrappedValue = wrappedValue
21+
}
22+
23+
public init(from decoder: Decoder) throws {
24+
let container = try decoder.singleValueContainer()
25+
var maybeString = try container.decode(String?.self)
26+
if let string = maybeString, string.count == 0 {
27+
maybeString = nil
28+
}
29+
self.wrappedValue = maybeString
30+
}
31+
}
32+
33+
extension KeyedDecodingContainer {
34+
// This is used to override the default decoding behavior for OptionalStringCoding to allow a value to avoid a missing key Error
35+
public func decode(_ type: OptionalStringCoding.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> OptionalStringCoding {
36+
try decodeIfPresent(OptionalStringCoding.self, forKey: key) ?? OptionalStringCoding(wrappedValue: nil)
37+
}
38+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
@testable import AWSLambdaEvents
16+
import XCTest
17+
18+
class OptionStaringWrapperTests: XCTestCase {
19+
func testMissing() {
20+
struct TestEvent: Decodable {
21+
@OptionalStringCoding
22+
var value: String?
23+
24+
public enum CodingKeys: String, CodingKey {
25+
case value
26+
}
27+
}
28+
29+
let json = #"{}"#
30+
var event: TestEvent!
31+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
32+
XCTAssertNil(event.value)
33+
}
34+
35+
func testNull() {
36+
struct TestEvent: Decodable {
37+
@OptionalStringCoding
38+
var value: String?
39+
40+
public enum CodingKeys: String, CodingKey {
41+
case value
42+
}
43+
}
44+
45+
let json = #"{"value": null}"#
46+
var event: TestEvent!
47+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
48+
XCTAssertNil(event.value)
49+
}
50+
51+
func testEmpty() {
52+
struct TestEvent: Decodable {
53+
@OptionalStringCoding
54+
var value: String?
55+
}
56+
57+
let json = #"{"value": ""}"#
58+
var event: TestEvent!
59+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
60+
XCTAssertNil(event.value)
61+
}
62+
63+
func testValue() {
64+
struct TestEvent: Decodable {
65+
@OptionalStringCoding
66+
var value: String?
67+
}
68+
69+
let json = #"{"value": "foo"}"#
70+
var event: TestEvent!
71+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
72+
XCTAssertEqual(event.value, "foo")
73+
}
74+
}

0 commit comments

Comments
 (0)