Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Sources/Converse/ContentBlocks/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public struct JSON: Codable, Sendable {

public init(from data: Data) throws {
do {
print(String(decoding: data, as: UTF8.self))
self = try JSONDecoder().decode(JSON.self, from: data)
} catch {
throw BedrockLibraryError.decodingError("Failed to decode JSON: \(error)")
Expand Down
4 changes: 4 additions & 0 deletions Tests/BedrockServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ struct BedrockServiceTests {
let bedrock: BedrockService

init() async throws {
// this is a workaround for issue
// https://github.com/awslabs/aws-sdk-swift/issues/1984
CommonRuntimeKit.initialize()

self.bedrock = try await BedrockService(
bedrockClient: MockBedrockClient(),
bedrockRuntimeClient: MockBedrockRuntimeClient()
Expand Down
28 changes: 12 additions & 16 deletions Tests/Converse/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ struct JSONTests {
#expect(json["name"] == "Jane Doe")
#expect(json["age"] == 30)
#expect(json["isMember"] == true)
let t: String? = json["nonExistentKey"]
#expect(t == nil)
#expect(json["nonExistentKey"] == nil)
}

@Test("JSON getValue from [String:JSONValue]")
Expand All @@ -39,8 +38,7 @@ struct JSONTests {
#expect(json["name"] == "Jane Doe")
#expect(json["age"] == 30)
#expect(json["isMember"] == true)
let t: String? = json["nonExistentKey"]
#expect(t == nil)
#expect(json["nonExistentKey"] == nil)
}

@Test("JSON getValue nested")
Expand All @@ -50,17 +48,15 @@ struct JSONTests {
#expect(json["name"] == "Jane Doe")
#expect(json["age"] == 30)
#expect(json["isMember"] == true)
let t: String? = json["nonExistentKey"]
#expect(t == nil)
#expect(json["nonExistentKey"] == nil)
#expect(json["address"]?["street"] == "123 Main St")
#expect(json["address"]?["city"] == "Anytown")
#expect(json["address"]?["state"] == "CA")
#expect(json["address"]?["zip"] == 12345)
#expect(json["address"]?["isSomething"] == true)
let t2: String? = json["address"]?["nonExistentKey"]
#expect(t2 == nil)
#expect(json["nonExistentKey"] == nil)
}
//

@Test("JSON Subscript")
func jsonSubscript() async throws {
let json = try JSON(
Expand Down Expand Up @@ -92,8 +88,7 @@ struct JSONTests {
#expect(json["address"]?["state"] == "CA")
#expect(json["address"]?["zip"] == 12345)
#expect(json["address"]?["isSomething"] == true)
let t2: String? = json["address"]?["nonExistentKey"]
#expect(t2 == nil)
#expect(json["nonExistentKey"] == nil)
}

@Test("JSON String Initializer with Invalid String")
Expand All @@ -103,7 +98,6 @@ struct JSONTests {
"name": "Jane Doe",
"age": 30,
"isMember": true,

""" // Note: trailing comma and no closing brace, making this invalid
#expect(throws: BedrockLibraryError.self) {
let _ = try JSON(from: invalidJSONString)
Expand All @@ -114,24 +108,26 @@ struct JSONTests {
func emptyJSON() async throws {
#expect(throws: Never.self) {
let json = try JSON(from: "")
let t: String? = json["nonExistentKey"]
#expect(t == nil)
#expect(json["nonExistentKey"] == nil)
}
}

@Test("Nested JSONValue")
func nestedJSONValue() {
JSON(
let json = JSON(
with: JSONValue([
"name": JSONValue("Jane Doe"),
"age": JSONValue(30),
"isMember": JSONValue(true),
])
)

#expect(json["name"] == "Jane Doe")
#expect(json["age"] == 30)
#expect(json["isMember"] == true)
}
}

// Fixtures
extension JSONTests {
private func jsonFromString() throws -> JSON {
try JSON(
Expand Down