Skip to content

Commit

Permalink
Merge pull request #5 from noppoMan/flat-dictionary
Browse files Browse the repository at this point in the history
add serializeToFlatDictionary to create valid query string for services that use query service protocol
  • Loading branch information
noppoMan authored Jul 11, 2017
2 parents 6389a5c + 65a012d commit e0f34a3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/AWSSDKSwiftCore/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ extension AWSClient {
}

case .query:
var dict = try ctx.input.serializeToDictionary()
var dict = try ctx.input.serializeToFlatDictionary()
dict["Action"] = operationName
dict["Version"] = apiVersion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,48 @@ extension DictionarySerializable {
}
return serialized
}

func serializeToFlatDictionary() throws -> [String: Any] {
func flatten(dictionary: [String: Any]) -> [String: Any] {
var flatted: [String: Any] = [:]

func destructiveFlatten(dictionary: [String: Any]) {
for (key, value) in dictionary {
switch value {
case let value as [String: Any]:
for (key2, value2) in flatten(dictionary: value) {
switch value2 {
case let value2 as [String: Any]:
destructiveFlatten(dictionary: value2)

case let values as [Any]: // TODO: values<Element> might be dictionary...
for iterator in values.enumerated() {
flatted["\(key).member.\(iterator.offset+1)"] = iterator.element
}

default:
flatted["\(key).\(key2)"] = value2
}
}

case let values as [Any]: // TODO: values<Element> might be dictionary...
for iterator in values.enumerated() {
flatted["\(key).member.\(iterator.offset+1)"] = iterator.element
}

default:
flatted[key] = value
}
}
}

destructiveFlatten(dictionary: dictionary)

return flatted
}

return flatten(dictionary: try self.serializeToDictionary())
}
}

public typealias DictionaryConvertible = DictionarySerializable & DictionaryInitializable
32 changes: 31 additions & 1 deletion Tests/AWSSDKSwiftCoreTests/SerializableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,40 @@ typealias Serializable = DictionarySerializable & XMLNodeSerializable
class SerializableTests: XCTestCase {

struct B: Serializable {
public static var parsingHints: [AWSShapeProperty] = [
AWSShapeProperty(label: "a", required: true, type: .string),
AWSShapeProperty(label: "b", required: false, type: .list),
AWSShapeProperty(label: "c", required: true, type: .list)
]

let a = "1"
let b = [1, 2]
let c = ["key": "value"]
}

struct C: Serializable {
public static var parsingHints: [AWSShapeProperty] = [
AWSShapeProperty(label: "value", required: true, type: .string)
]

let value = "hello"
}

struct D: Serializable {
public static var parsingHints: [AWSShapeProperty] = [
AWSShapeProperty(label: "value", required: true, type: .string)
]

let value = "world"
}

struct A: Serializable {
public static var parsingHints: [AWSShapeProperty] = [
AWSShapeProperty(label: "structure", required: true, type: .structure),
AWSShapeProperty(label: "structures", required: false, type: .list),
AWSShapeProperty(label: "array", required: true, type: .list)
]

let structure = B()
let structures: [Serializable] = [C(), D()]
let array = ["foo", "bar"]
Expand All @@ -37,7 +57,8 @@ class SerializableTests: XCTestCase {
static var allTests : [(String, (SerializableTests) -> () throws -> Void)] {
return [
("testSerializeToXML", testSerializeToXML),
("testSerializeToDictionaryAndJSON", testSerializeToDictionaryAndJSON)
("testSerializeToDictionaryAndJSON", testSerializeToDictionaryAndJSON),
("testSerializeToFlatDictionary", testSerializeToFlatDictionary)
]
}

Expand All @@ -57,4 +78,13 @@ class SerializableTests: XCTestCase {
XCTAssertEqual(dict.count, jsonObect.count)
}

func testSerializeToFlatDictionary() {
let dict = try! A().serializeToFlatDictionary()
XCTAssertEqual(dict.count, 8)
XCTAssertEqual(dict["structure.a"] as? String, "1")
XCTAssertEqual(dict["structure.c.key"] as? String, "value")
XCTAssertEqual(dict["array.member.1"] as? String, "foo")
XCTAssertEqual(dict["array.member.2"] as? String, "bar")
}

}

0 comments on commit e0f34a3

Please sign in to comment.